Crate ambient_element
source ·Expand description
Element is a React-inspired virtual tree library for the Ambient runtime.
It is backed by the Ambient ECS; the virtual tree is converted into a real tree of entities and components. When the tree is updated, it is compared to the previous tree, and only the differences are applied to the ECS. This can be used for UI, as well as any other tree-like data structure that you want to be able to update efficiently.
Idioms
By convention, most ElementComponents define an el
method that returns an Element of that type. This el
takes the properties to make it easy to both construct the component and instantiate it as an Element.
In addition to this, ElementComponentExt adds an el
method to all ElementComponents that converts them to
an Element.
This means that an ElementComponent that looks like this
#[element_component]
fn MyComponent(hooks: &mut Hooks, a: u32, b: String) -> Element {
// ...
}
can be instantiated as an Element using either of these methods:
MyComponent { a: 42, b: "hello".to_string() }.el()
or
MyComponent::el(42, "hello".to_string())
Passing data in
To pass data into the root of an Element tree, pass the data into its properties when constructing it and/or update the root of the tree using ElementTree::migrate_root.
To receive data from an Element tree, we recommend you use messaging. This includes sending messages to the server and/or
standard messaging channels in Rust (e.g. std::sync::mpsc::channel
). We do not generally recommend trying to send data
out of the tree directly, as this can be difficult to reason about.
Macros
- Shorthand for
let x = x.to_owned();
Structs
- A rendered ElementComponent instance.
- A tree of instantiated Elements.
- Wrap multiple Elements in a flat hierarchy.
- Hooks are a way to hook into the state and lifecycle of an Element.
- Memoize the ElementComponent, such that it is only re-rendered if the component changes.
- A shareable element tree. This is used to share the same tree between multiple contexts.
- Wraps the inner Element. This is useful for introducing an intermediate component node in a tree.
Traits
- A trait for types that can be converted to
Any
and can also be cloned. - The base trait for all element components. These are similar to React components.
- A convenience trait for converting an ElementComponent into an Element.
- Contains the name of the type implementing ElementComponent.
Functions
- The systems required to drive ElementTrees.
- Element: The identifier of the
Element
that controls this entity. - The element tree state for an entity.
- Element unmanaged children: If this is set, the user is expected to manage the children of the
Element
themselves. - Initialize the components for the module
- Render the given tree underneath
id
.
Type Definitions
- The return type of a function passed to Hooks::use_spawn. This function is called when the Element is unmounted/despawned; use it to clean up any resources.
- Helper type for a callback that sets some value.
Attribute Macros
- Helper macro to implement a
ElementComponent
with a pure free function.