Crate ambient_api::prelude::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
- Consume a context of type
T
provided further up the tree. - Element: The identifier of the
Element
that controls this entity. - Element unmanaged children: If this is set, the user is expected to manage the children of the
Element
themselves. - Provide a value which is accessible to all children further down the tree.
- Run a function for its side effects each time a dependency changes.
- use_entity_component
guest
Use a component from an entity in the ECS. - use_entity_concept
guest
Use a concept from an entity in the ECS, and update its state if required. - Executes a function each frame.
- Run
cb
everyseconds
seconds. - Run
cb
everyduration
, passing in the current value ofdependencies
. - A computation that runs once on spawn, and when
dependencies
change. The computation is not re-run if thedependencies
are the same - that is, the computation is memoized. - Register a function to be called when a ModuleMessage is received.
- Send the
Enter
message when thisElement
is mounted, and theExit
message when it is unmounted. - use_query
guest
Query the ECS for entities with the given components each frame and return the results. - Provides internally mutable state that is preserved between re-renders.
- Provides a function that, when called, will cause this Element to be re-rendered.
- use_resource
guest
Use a resource from the ECS, and update its state if required. - Register a function to be called when a RuntimeMessage is received.
- Execute a function when the Element is mounted/rendered for the first time.
- Preserve state between rerenders.
- The same as use_state, but with a function that returns the initial value.
Type Aliases
- Helper type for a callback that sets some value.
Attribute Macros
- Helper macro to implement a
ElementComponent
with a pure free function.