16 releases

0.3.1 Oct 25, 2023
0.3.1-rc.1 Oct 24, 2023
0.3.0-rc.2 Sep 29, 2023
0.2.1 May 6, 2023

#234 in Data structures

Download history 6/week @ 2024-01-01 34/week @ 2024-01-08 36/week @ 2024-01-15 8/week @ 2024-01-22 16/week @ 2024-01-29 35/week @ 2024-02-05 35/week @ 2024-02-12 74/week @ 2024-02-19 70/week @ 2024-02-26 48/week @ 2024-03-04 65/week @ 2024-03-11 71/week @ 2024-03-18 102/week @ 2024-03-25 112/week @ 2024-04-01 47/week @ 2024-04-08 55/week @ 2024-04-15

332 downloads per month
Used in 19 crates (11 directly)

MIT/Apache

135KB
3K SLoC

Ambient Element

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.

The Element module is closely modelled on React with Hooks. React Components are called ElementComponents here (since Component is already used by the ECS). There are a couple of concepts to keep track of here:

  • Entities (React: DOM node) are just normal ECS entities, and they're kind of the "result" of all the work this module is doing.
  • Element (React: Element). This is essentially a description of an Entity or a ElementComponent. You can set normal ECS Components on it (for instance .set(translation(), ..)). An Element can have children, which will be translated to a children() component on the Entity.
  • ElementComponent (React: Component) is basically a function which takes some user inputs + the hooks (local state of an instance) and produces an Element tree from this. ElementComponents can wrap each other, so for instance an outer ElementComponent can return an Element which the inner ElementComponent only adds a Component to. In this case, there's only one Element, even though there are two ElementComponents. (i.e. there's not a 1:1 correspondence between ElementComponents and Elements).
  • ElementInstance (React: Instance) is an instance of an Element (remember, Elements are just descriptions). It has a reference to the Entity it "owns". When using Hooks, the state is stored on the ElementInstance.
  • ElementTree is a tree of ElementInstances.

More info:

Dependencies

~6–52MB
~808K SLoC