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

Traits

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 a component from an entity in the ECS.
  • Use a concept from an entity in the ECS, and update its state if required.
  • Executes a function each frame.
  • Run cb every seconds seconds.
  • Run cb every duration, passing in the current value of dependencies.
  • A computation that runs once on spawn, and when dependencies change. The computation is not re-run if the dependencies 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 this Element is mounted, and the Exit message when it is unmounted.
  • 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 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

  • The return type of a function passed to 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.