Skip to main content

Elements

Struct Elements 

Source
pub struct Elements { /* private fields */ }
Expand description

A list of component descriptions for declarative tree building.

Elements is what view functions return and what the framework reconciles against the existing component tree. Build one with the element! macro or the imperative API:

// With the element! macro (preferred):
fn my_view(state: &MyState) -> Elements {
    element! {
        "Hello"
        #(if state.loading {
            Spinner(key: "spinner", label: "Loading...")
        })
    }
}

// Imperative API:
fn my_view(state: &MyState) -> Elements {
    let mut els = Elements::new();
    els.add(Text::unstyled("Hello"));
    if state.loading {
        els.add(Spinner::new("Loading...")).key("spinner");
    }
    els
}

Implementations§

Source§

impl Elements

Source

pub fn new() -> Self

Create an empty element list.

Examples found in repository?
examples/lifecycle.rs (line 109)
108fn task_view(state: &AppState) -> Elements {
109    let mut els = Elements::new();
110
111    els.add(Text::styled(
112        format!("Tasks ({})", state.tasks.len()),
113        Style::default()
114            .fg(Color::White)
115            .add_modifier(Modifier::BOLD),
116    ));
117
118    for task in &state.tasks {
119        els.add(StatusLog::new(task)).key(task.clone());
120    }
121
122    if state.processing {
123        els.add(Spinner::new("Processing...")).key("spinner");
124    }
125
126    els.add(Text::styled("---", Style::default().fg(Color::DarkGray)));
127
128    els
129}
Source

pub fn add<C: Component>(&mut self, component: C) -> ElementHandle<'_>

Add a component to the list.

Returns an ElementHandle that can be used to set a key for stable identity across rebuilds.

Examples found in repository?
examples/lifecycle.rs (lines 111-116)
108fn task_view(state: &AppState) -> Elements {
109    let mut els = Elements::new();
110
111    els.add(Text::styled(
112        format!("Tasks ({})", state.tasks.len()),
113        Style::default()
114            .fg(Color::White)
115            .add_modifier(Modifier::BOLD),
116    ));
117
118    for task in &state.tasks {
119        els.add(StatusLog::new(task)).key(task.clone());
120    }
121
122    if state.processing {
123        els.add(Spinner::new("Processing...")).key("spinner");
124    }
125
126    els.add(Text::styled("---", Style::default().fg(Color::DarkGray)));
127
128    els
129}
Source

pub fn add_with_children<C: Component>( &mut self, component: C, children: Elements, ) -> ElementHandle<'_>

Add a component with nested children.

The component is created first, then children are built as its descendants. The component’s children() method receives these as the slot parameter.

Source

pub fn group(&mut self, children: Elements) -> ElementHandle<'_>

Add a VStack wrapper around the given children.

Equivalent to add_with_children(VStack, children).

Source

pub fn hstack(&mut self, children: Elements) -> ElementHandle<'_>

Add an HStack wrapper around the given children.

Children default to WidthConstraint::Fill. Use .width(WidthConstraint::Fixed(n)) for fixed-width children.

Source

pub fn splice(&mut self, other: Elements)

Splice another Elements list into this one.

All entries from other are appended to this list. This is used by the element! macro’s #(expr) syntax to interpolate pre-built Elements inline.

Source§

impl Elements

Source

pub fn is_empty(&self) -> bool

Whether this element list is empty.

Trait Implementations§

Source§

impl<C: Component> AddTo<Elements> for ComponentWithSlot<C>

Source§

type Handle<'a> = ElementHandle<'a>

Handle returned after adding. Supports .key() / .width() chaining.
Source§

fn add_to(self, els: &mut Elements) -> ElementHandle<'_>

Add this value to the collector, returning a handle for chaining .key() and .width().
Source§

impl AddTo<Elements> for String

String → Elements: creates a Text component with the string as content.

This powers the element! string literal sugar — "hello" becomes a Text component. The same AddTo dispatch also works inside data children contexts (e.g., Text { "hello" }) via the Into<TextChild> blanket impl.

Source§

type Handle<'a> = ElementHandle<'a>

Handle returned after adding. Supports .key() / .width() chaining.
Source§

fn add_to(self, els: &mut Elements) -> ElementHandle<'_>

Add this value to the collector, returning a handle for chaining .key() and .width().
Source§

impl Default for Elements

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl SpliceInto<Elements> for Elements

Source§

fn splice_into(self, collector: &mut Elements)

Splice all entries from this value into the collector.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T, V> AddTo<DataChildren<T>> for V
where V: Into<T>,

Source§

type Handle<'a> = DataHandle where T: 'a

Handle returned after adding. Supports .key() / .width() chaining.
Source§

fn add_to(self, collector: &mut DataChildren<T>) -> DataHandle

Add this value to the collector, returning a handle for chaining .key() and .width().
Source§

impl<C> AddTo<Elements> for C
where C: Component,

Source§

type Handle<'a> = ElementHandle<'a>

Handle returned after adding. Supports .key() / .width() chaining.
Source§

fn add_to(self, els: &mut Elements) -> ElementHandle<'_>

Add this value to the collector, returning a handle for chaining .key() and .width().
Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.