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