Attribute Macro ambient_element::element_component

source ·
#[element_component]
Expand description

Helper macro to implement a ElementComponent with a pure free function.

Rewrites a fn Component(&mut Hooks, ...Args) -> Element to a struct Component and an implementation of ElementComponent for that Component, where the trait’s render function corresponds to this function.

Example:

pub fn FancyText(
    hooks: &mut ambient_element::Hooks,
    /// The message to display
    msg: String,
    alpha: f32,
) -> ambient_element::Element {
    Text::el(msg)
}

becomes:

#[derive(std::clone::Clone, std::fmt::Debug)]
pub struct FancyText {
    /// The message to display
    pub msg: String,
    pub alpha: f32,
}
impl ambient_element::ElementComponent for FancyText {
    fn render(self: Box<Self>, hooks: &mut ambient_element::Hooks) -> ambient_element::Element {
        let Self { msg, alpha } = *self;
        {
            Text::el(msg)
        }
    }
}