intuitive/components/
embed.rs

1use crate::{component, components::Any as AnyComponent, element::Any as AnyElement};
2
3pub enum Content {
4  Component(AnyComponent),
5  Element(AnyElement),
6}
7
8impl From<AnyComponent> for Content {
9  fn from(component: AnyComponent) -> Self {
10    Self::Component(component)
11  }
12}
13
14impl From<AnyElement> for Content {
15  fn from(element: AnyElement) -> Self {
16    Self::Element(element)
17  }
18}
19
20impl Default for Content {
21  fn default() -> Self {
22    Self::Element(AnyElement::default())
23  }
24}
25
26/// A component that renders an [`element::Any`] or a [`component::Any`].
27///
28/// This is often needed when rendering children. More generally, `Embed` is
29/// useful when you have a variable that contains an [`element::Any`] or
30/// a [`component::Any`] and you want to insert it into a [`render!`] call.
31///
32/// For example,
33/// ```rust
34/// # use intuitive::{component, components::{children::Children, HStack, VStack, Empty, Embed}, render};
35/// #
36/// #[component(Centered)]
37/// pub fn render(children: Children<1>) {
38///   render! {
39///     VStack() {
40///       Empty()
41///       HStack() {
42///         Empty()
43///         Embed(content: children[0].clone())
44///         Empty()
45///       }
46///       Empty()
47///     }
48///   }
49/// }
50/// ```
51///
52/// [`component::Any`]: struct.Any.html
53/// [`element::Any`]: ../element/struct.Any.html
54/// [`render!`]: ../macro.render.html
55#[component(Embed)]
56pub fn render(content: Content) -> AnyElement {
57  match content {
58    Content::Component(component) => component.render(),
59    Content::Element(element) => Clone::clone(element),
60  }
61}