1# Taffy
2
3[](https://github.com/DioxusLabs/taffy/actions/workflows/ci.yml)
4[](https://crates.io/crates/taffy)
5[](https://docs.rs/taffy)
6
7Taffy is a flexible, high-performance, cross-platform UI layout library written in [Rust](https://www.rust-lang.org).
8
9It currently implements the CSS **Block**, **Flexbox** and **CSS Grid** layout algorithms. Support for other paradigms is planned. For more information on this and other future development plans see the [roadmap issue](https://github.com/DioxusLabs/taffy/issues/345).
10
11This crate is a collaborative, cross-team project, and is designed to be used as a dependency for other UI and GUI libraries.
12Right now, it powers:
13
14- [Servo](https://github.com/servo/servo): an alternative web browser
15- [Blitz](https://github.com/DioxusLabs/blitz): a radically modular web engine
16- [Bevy](https://bevyengine.org/): an ergonomic, ECS-first Rust game engine
17- [iocraft](https://github.com/ccbrown/iocraft): crafting beautiful interfaces for the terminal
18- The [Lapce](https://lapce.dev/) text editor via the [Floem](https://github.com/lapce/floem) UI framework
19- The [Zed](https://zed.dev/) text editor via the [GPUI](https://github.com/zed-industries/zed/tree/main/crates/gpui) UI framework
20
21## Usage
22
23```rust
24use taffy::prelude::*;
25
26// First create an instance of TaffyTree
27let mut tree : TaffyTree<()> = TaffyTree::new();
28
29// Create a tree of nodes using `TaffyTree.new_leaf` and `TaffyTree.new_with_children`.
30// These functions both return a node id which can be used to refer to that node
31// The Style struct is used to specify styling information
32let header_node = tree
33 .new_leaf(
34 Style {
35 size: Size { width: length(800.0), height: length(100.0) },
36 ..Default::default()
37 },
38 ).unwrap();
39
40let body_node = tree
41 .new_leaf(
42 Style {
43 size: Size { width: length(800.0), height: auto() },
44 flex_grow: 1.0,
45 ..Default::default()
46 },
47 ).unwrap();
48
49let root_node = tree
50 .new_with_children(
51 Style {
52 flex_direction: FlexDirection::Column,
53 size: Size { width: length(800.0), height: length(600.0) },
54 ..Default::default()
55 },
56 &[header_node, body_node],
57 )
58 .unwrap();
59
60// Call compute_layout on the root of your tree to run the layout algorithm
61tree.compute_layout(root_node, Size::MAX_CONTENT).unwrap();
62
63// Inspect the computed layout using `TaffyTree.layout`
64assert_eq!(tree.layout(root_node).unwrap().size.width, 800.0);
65assert_eq!(tree.layout(root_node).unwrap().size.height, 600.0);
66assert_eq!(tree.layout(header_node).unwrap().size.width, 800.0);
67assert_eq!(tree.layout(header_node).unwrap().size.height, 100.0);
68assert_eq!(tree.layout(body_node).unwrap().size.width, 800.0);
69assert_eq!(tree.layout(body_node).unwrap().size.height, 500.0); // This value was not set explicitly, but was computed by Taffy
70
71```
72
73## Bindings to other languages
74
75- Python via [stretchable](https://github.com/mortencombat/stretchable)
76- [WIP C bindings](https://github.com/DioxusLabs/taffy/pull/404)
77- [WIP WASM bindings](https://github.com/DioxusLabs/taffy/pull/394)
78
79## Learning Resources
80
81Taffy implements the Flexbox and CSS Grid specifications faithfully, so documentation designed for the web should translate cleanly to Taffy's implementation. For reference documentation on individual style properties we recommend the MDN documentation (for example [this page](https://developer.mozilla.org/en-US/docs/Web/CSS/width) on the `width` property). Such pages can usually be found by searching for "MDN property-name" using a search engine.
82
83If you are interested in guide-level documentation on CSS layout, then we recommend the following resources:
84
85### Flexbox
86
87- [Flexbox Froggy](https://flexboxfroggy.com/). This is an interactive tutorial/game that allows you to learn the essential parts of Flexbox in a fun engaging way.
88- [A Complete Guide To Flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) by CSS Tricks. This is detailed guide with illustrations and comprehensive written explanation of the different Flexbox properties and how they work.
89
90### CSS Grid
91
92- [CSS Grid Garden](https://cssgridgarden.com/). This is an interactive tutorial/game that allows you to learn the essential parts of CSS Grid in a fun engaging way.
93- [A Complete Guide To CSS Grid](https://css-tricks.com/snippets/css/complete-guide-grid/) by CSS Tricks. This is detailed guide with illustrations and comprehensive written explanation of the different CSS Grid properties and how they work.
94
95## Benchmarks (vs. [Yoga](https://github.com/facebook/yoga))
96
97- Run on a 2021 MacBook Pro with M1 Pro processor using [criterion](https://github.com/bheisler/criterion.rs)
98- The benchmarks measure layout computation only. They do not measure tree creation.
99- Yoga benchmarks were run via the [yoga](https://github.com/bschwind/yoga-rs) crate (Rust bindings)
100- Most popular websites seem to have between 3,000 and 10,000 nodes (although they also require text layout, which neither yoga nor taffy implement).
101
102Note that the table below contains multiple different units (milliseconds vs. microseconds)
103
104| Benchmark | Node Count | Depth | Yoga ([ba27f9d]) | Taffy ([71027a8]) |
105| --- | --- | --- | --- | --- |
106| yoga 'huge nested' | 1,000 | 3 | 364.60 µs | 329.04 µs |
107| yoga 'huge nested' | 10,000 | 4 | 4.1988 ms | 4.3486 ms |
108| yoga 'huge nested' | 100,000 | 5 | 45.804 ms | 38.559 ms |
109| big trees (wide) | 1,000 | 1 | 737.77 µs | 505.99 µs |
110| big trees (wide) | 10,000 | 1 | 7.1007 ms | 8.3395 ms |
111| big trees (wide) | 100,000 | 1 | 135.78 ms | 247.42 ms |
112| big trees (deep) | 4,000 | 12 | 2.2333 ms | 1.7400 ms |
113| big trees (deep) | 10,000 | 14 | 5.9477 ms | 4.4445 ms |
114| big trees (deep) | 100,000 | 17 | 76.755 ms | 63.778 ms |
115| super deep | 1,000 | 1,000 | 555.32 µs | 472.85 µs |
116
117[ba27f9d]: https://github.com/facebook/yoga/commit/ba27f9d1ecfa7518019845b84b035d3d4a2a6658
118[71027a8]: https://github.com/DioxusLabs/taffy/commit/71027a8de03b343e120852b84bb7dca9fb4651c5
119
120## Contributions
121
122[Contributions welcome](https://github.com/DioxusLabs/taffy/blob/main/CONTRIBUTING.md):
123if you'd like to use, improve or build `taffy`, feel free to join the conversation, open an [issue](https://github.com/DioxusLabs/taffy/issues) or submit a [PR](https://github.com/DioxusLabs/taffy/pulls).
124If you have questions about how to use `taffy`, open a [discussion](https://github.com/DioxusLabs/taffy/discussions) so we can answer your questions in a way that others can find.
README.md
Taffy

Taffy is a flexible, high-performance, cross-platform UI layout library written in Rust.
It currently implements the CSS Block, Flexbox and CSS Grid layout algorithms. Support for other paradigms is planned. For more information on this and other future development plans see the roadmap issue.
This crate is a collaborative, cross-team project, and is designed to be used as a dependency for other UI and GUI libraries.
Right now, it powers:
- Servo: an alternative web browser
- Blitz: a radically modular web engine
- Bevy: an ergonomic, ECS-first Rust game engine
- iocraft: crafting beautiful interfaces for the terminal
- The Lapce text editor via the Floem UI framework
- The Zed text editor via the GPUI UI framework
Usage
use taffy::prelude::*;
// First create an instance of TaffyTree
let mut tree : TaffyTree<()> = TaffyTree::new();
// Create a tree of nodes using `TaffyTree.new_leaf` and `TaffyTree.new_with_children`.
// These functions both return a node id which can be used to refer to that node
// The Style struct is used to specify styling information
let header_node = tree
.new_leaf(
Style {
size: Size { width: length(800.0), height: length(100.0) },
..Default::default()
},
).unwrap();
let body_node = tree
.new_leaf(
Style {
size: Size { width: length(800.0), height: auto() },
flex_grow: 1.0,
..Default::default()
},
).unwrap();
let root_node = tree
.new_with_children(
Style {
flex_direction: FlexDirection::Column,
size: Size { width: length(800.0), height: length(600.0) },
..Default::default()
},
&[header_node, body_node],
)
.unwrap();
// Call compute_layout on the root of your tree to run the layout algorithm
tree.compute_layout(root_node, Size::MAX_CONTENT).unwrap();
// Inspect the computed layout using `TaffyTree.layout`
assert_eq!(tree.layout(root_node).unwrap().size.width, 800.0);
assert_eq!(tree.layout(root_node).unwrap().size.height, 600.0);
assert_eq!(tree.layout(header_node).unwrap().size.width, 800.0);
assert_eq!(tree.layout(header_node).unwrap().size.height, 100.0);
assert_eq!(tree.layout(body_node).unwrap().size.width, 800.0);
assert_eq!(tree.layout(body_node).unwrap().size.height, 500.0); // This value was not set explicitly, but was computed by Taffy
Bindings to other languages
Learning Resources
Taffy implements the Flexbox and CSS Grid specifications faithfully, so documentation designed for the web should translate cleanly to Taffy's implementation. For reference documentation on individual style properties we recommend the MDN documentation (for example this page on the width property). Such pages can usually be found by searching for "MDN property-name" using a search engine.
If you are interested in guide-level documentation on CSS layout, then we recommend the following resources:
Flexbox
- Flexbox Froggy. This is an interactive tutorial/game that allows you to learn the essential parts of Flexbox in a fun engaging way.
- A Complete Guide To Flexbox by CSS Tricks. This is detailed guide with illustrations and comprehensive written explanation of the different Flexbox properties and how they work.
CSS Grid
- CSS Grid Garden. This is an interactive tutorial/game that allows you to learn the essential parts of CSS Grid in a fun engaging way.
- A Complete Guide To CSS Grid by CSS Tricks. This is detailed guide with illustrations and comprehensive written explanation of the different CSS Grid properties and how they work.
Benchmarks (vs. Yoga)
- Run on a 2021 MacBook Pro with M1 Pro processor using criterion
- The benchmarks measure layout computation only. They do not measure tree creation.
- Yoga benchmarks were run via the yoga crate (Rust bindings)
- Most popular websites seem to have between 3,000 and 10,000 nodes (although they also require text layout, which neither yoga nor taffy implement).
Note that the table below contains multiple different units (milliseconds vs. microseconds)
| Benchmark | Node Count | Depth | Yoga (ba27f9d) | Taffy (71027a8) |
| yoga 'huge nested' | 1,000 | 3 | 364.60 µs | 329.04 µs |
| yoga 'huge nested' | 10,000 | 4 | 4.1988 ms | 4.3486 ms |
| yoga 'huge nested' | 100,000 | 5 | 45.804 ms | 38.559 ms |
| big trees (wide) | 1,000 | 1 | 737.77 µs | 505.99 µs |
| big trees (wide) | 10,000 | 1 | 7.1007 ms | 8.3395 ms |
| big trees (wide) | 100,000 | 1 | 135.78 ms | 247.42 ms |
| big trees (deep) | 4,000 | 12 | 2.2333 ms | 1.7400 ms |
| big trees (deep) | 10,000 | 14 | 5.9477 ms | 4.4445 ms |
| big trees (deep) | 100,000 | 17 | 76.755 ms | 63.778 ms |
| super deep | 1,000 | 1,000 | 555.32 µs | 472.85 µs |
Contributions
Contributions welcome:
if you'd like to use, improve or build taffy, feel free to join the conversation, open an issue or submit a PR.
If you have questions about how to use taffy, open a discussion so we can answer your questions in a way that others can find.