taffy-wasm 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +178 -0
- package/package.json +26 -0
- package/taffy_wasm.d.ts +1142 -0
- package/taffy_wasm.js +2413 -0
- package/taffy_wasm_bg.wasm +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# Taffy WebAssembly Bindings
|
|
2
|
+
|
|
3
|
+
> **High-performance Flexbox and CSS Grid layout for JavaScript/TypeScript, powered by Rust and WebAssembly.**
|
|
4
|
+
|
|
5
|
+
  
|
|
6
|
+
|
|
7
|
+
**Taffy** is a generic, high-performance UI layout library written in Rust. This package (`taffy-js`) provides WebAssembly bindings, allowing you to use Taffy's standards-compliant Flexbox and Grid algorithms directly in your web or Node.js applications with near-native performance.
|
|
8
|
+
|
|
9
|
+
## ✨ Features
|
|
10
|
+
|
|
11
|
+
- **🚀 High Performance**: Leverages the speed of Rust and WebAssembly for complex layout computations.
|
|
12
|
+
- **📦 Tiny Footprint**: Highly optimized WASM binary size.
|
|
13
|
+
- **🎨 Modern Layouts**: Full support for **Flexbox** and **CSS Grid** specifications.
|
|
14
|
+
- **🛠 Framework Agnostic**: Use it with React, Vue, Svelte, vanilla JS, or even in Node.js for server-side layout calculation.
|
|
15
|
+
- **🔒 Type-Safe**: Fully typed API with TypeScript definitions included.
|
|
16
|
+
|
|
17
|
+
## 📦 Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install taffy-js
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
> **Note**: This package relies on WebAssembly. Ensure your runtime (modern browser or Node.js) supports WASM.
|
|
24
|
+
|
|
25
|
+
## 🚀 Usage
|
|
26
|
+
|
|
27
|
+
Here is a complete example showing how to create a layout tree, style nodes, and compute results.
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import init, {
|
|
31
|
+
new_leaf,
|
|
32
|
+
new_with_children,
|
|
33
|
+
compute_layout,
|
|
34
|
+
get_layout,
|
|
35
|
+
Display,
|
|
36
|
+
FlexDirection,
|
|
37
|
+
AlignItems,
|
|
38
|
+
JustifyContent,
|
|
39
|
+
Style,
|
|
40
|
+
} from "taffy-js";
|
|
41
|
+
|
|
42
|
+
async function run() {
|
|
43
|
+
// 1. Initialize the WASM module
|
|
44
|
+
await init();
|
|
45
|
+
|
|
46
|
+
// 2. Define Styles
|
|
47
|
+
// Styles match CSS properties. You can mix specific units like Pixels, Percent, or Auto.
|
|
48
|
+
const boxStyle: Style = {
|
|
49
|
+
display: Display.Flex,
|
|
50
|
+
width: { value: 100, unit: "Pixels" },
|
|
51
|
+
height: { value: 100, unit: "Pixels" },
|
|
52
|
+
justify_content: JustifyContent.Center,
|
|
53
|
+
align_items: AlignItems.Center,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const rootStyle: Style = {
|
|
57
|
+
display: Display.Flex,
|
|
58
|
+
// layout 500x500 container
|
|
59
|
+
width: { value: 500, unit: "Pixels" },
|
|
60
|
+
height: { value: 500, unit: "Pixels" },
|
|
61
|
+
flex_direction: FlexDirection.Row,
|
|
62
|
+
justify_content: JustifyContent.SpaceAround,
|
|
63
|
+
align_items: AlignItems.Center,
|
|
64
|
+
gap: {
|
|
65
|
+
width: { value: 20, unit: "Pixels" },
|
|
66
|
+
height: { value: 0, unit: "Pixels" },
|
|
67
|
+
}, // Example of potential gap usage if supported
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// 3. Create Tree Nodes
|
|
71
|
+
// Leaf nodes
|
|
72
|
+
const child1 = new_leaf(boxStyle);
|
|
73
|
+
const child2 = new_leaf(boxStyle);
|
|
74
|
+
|
|
75
|
+
// Root node with children
|
|
76
|
+
const root = new_with_children(rootStyle, [child1, child2]);
|
|
77
|
+
|
|
78
|
+
// 4. Compute Layout
|
|
79
|
+
// You can pass available space constraints (width/height).
|
|
80
|
+
// Passing values corresponds to "Definite" size, null corresponds to "MaxContent/Available".
|
|
81
|
+
compute_layout(root, {
|
|
82
|
+
width: 500,
|
|
83
|
+
height: 500,
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// 5. Retrieve Results
|
|
87
|
+
const rootLayout = get_layout(root);
|
|
88
|
+
const child1Layout = get_layout(child1);
|
|
89
|
+
const child2Layout = get_layout(child2);
|
|
90
|
+
|
|
91
|
+
console.log("Root:", rootLayout); // { x: 0, y: 0, width: 500, height: 500 }
|
|
92
|
+
console.log("Child 1:", child1Layout); // { x: ..., y: ..., width: 100, height: 100 }
|
|
93
|
+
console.log("Child 2:", child2Layout); // { x: ..., y: ..., width: 100, height: 100 }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
run();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## 📐 Architecture
|
|
100
|
+
|
|
101
|
+
`taffy-js` acts as a thin wrapper around the [Taffy](https://github.com/DioxusLabs/taffy) Rust crate.
|
|
102
|
+
|
|
103
|
+
1. **Node Tree**: You build a flat tree of nodes in the WASM memory space using integer IDs (`u32`).
|
|
104
|
+
2. **Style Transfer**: Styles are serialized from JS objects to Rust structs via `serde-wasm-bindgen`.
|
|
105
|
+
3. **Computation**: Rust runs the layout algorithms (Flexbox/Grid).
|
|
106
|
+
4. **Readout**: You query the final computed geometry (x, y, width, height) back to JS.
|
|
107
|
+
|
|
108
|
+
## 📚 API Reference
|
|
109
|
+
|
|
110
|
+
### Lifecycle
|
|
111
|
+
|
|
112
|
+
| Function | Description |
|
|
113
|
+
| :------- | :------------------------------------------------------------------------------ |
|
|
114
|
+
| `init()` | Initializes the WASM module. Must be awaited before calling any other function. |
|
|
115
|
+
| `free()` | (Optional) Manually free memory if required by your specific WASM loader setup. |
|
|
116
|
+
|
|
117
|
+
### Node Management
|
|
118
|
+
|
|
119
|
+
| Function | Signature | Description |
|
|
120
|
+
| :------------------ | :--------------------------------------------- | :-------------------------------------- |
|
|
121
|
+
| `new_leaf` | `(style: Style) -> number` | Creates a leaf node (no children). |
|
|
122
|
+
| `new_with_children` | `(style: Style, children: number[]) -> number` | Creates a node containing child nodes. |
|
|
123
|
+
| `add_child` | `(parent: number, child: number) -> void` | Appends a child to a parent. |
|
|
124
|
+
| `remove_child` | `(parent: number, child: number) -> void` | Removes a specific child from a parent. |
|
|
125
|
+
| `set_children` | `(parent: number, children: number[]) -> void` | Replaces all children of a node. |
|
|
126
|
+
| `remove_node` | `(node: number) -> void` | Deletes a node and frees its memory. |
|
|
127
|
+
|
|
128
|
+
### Layout & Style
|
|
129
|
+
|
|
130
|
+
| Function | Signature | Description |
|
|
131
|
+
| :--------------- | :---------------------------------------------- | :-------------------------------------------------------------------------- |
|
|
132
|
+
| `set_style` | `(node: number, style: Style) -> void` | Updates the style properties of a node. |
|
|
133
|
+
| `compute_layout` | `(root: number, space: AvailableSpace) -> void` | Triggers the layout calculation algorithm. |
|
|
134
|
+
| `get_layout` | `(node: number) -> Layout` | Returns `{x, y, width, height}` for a node. |
|
|
135
|
+
| `mark_dirty` | `(node: number) -> void` | Manually validates a node (usually handled automatically by style setters). |
|
|
136
|
+
|
|
137
|
+
### Type Definitions
|
|
138
|
+
|
|
139
|
+
#### `Style`
|
|
140
|
+
|
|
141
|
+
A comprehensive object mirroring CSS properties.
|
|
142
|
+
|
|
143
|
+
- **Display**: `Display.Flex`, `Display.Grid`, `Display.None`
|
|
144
|
+
- **Dimensions**: `{ value: number, unit: "Pixels" | "Percent" | "Auto" }`
|
|
145
|
+
- **Flexbox**: `flex_direction`, `justify_content`, `align_items`, `flex_wrap`, etc.
|
|
146
|
+
- **Grid**: `grid_template_rows`, `grid_template_columns`, etc.
|
|
147
|
+
|
|
148
|
+
#### `AvailableSpace`
|
|
149
|
+
|
|
150
|
+
Used when triggering computation.
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
interface AvailableSpace {
|
|
154
|
+
width: number | null; // null = unlimited/content-based
|
|
155
|
+
height: number | null; // null = unlimited/content-based
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## 🛠 Building from Source
|
|
160
|
+
|
|
161
|
+
If you want to contribute or build the WASM binary yourself:
|
|
162
|
+
|
|
163
|
+
1. **Prerequisites**: Install Rust and `wasm-pack`.
|
|
164
|
+
```bash
|
|
165
|
+
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
|
|
166
|
+
```
|
|
167
|
+
2. **Build**:
|
|
168
|
+
```bash
|
|
169
|
+
npm install
|
|
170
|
+
npm run build
|
|
171
|
+
```
|
|
172
|
+
The artifacts will be generated in the `pkg/` directory.
|
|
173
|
+
|
|
174
|
+
## 📄 License
|
|
175
|
+
|
|
176
|
+
MIT License © 2024 ByteLand Technology
|
|
177
|
+
|
|
178
|
+
This project wraps [Taffy](https://github.com/DioxusLabs/taffy), which is also MIT licensed.
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "taffy-wasm",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"collaborators": [
|
|
5
|
+
"Alice Cecile <alice.i.cecile@gmail.com>",
|
|
6
|
+
"Johnathan Kelley <jkelleyrtp@gmail.com>",
|
|
7
|
+
"Nico Burns <nico@nicoburns.com>"
|
|
8
|
+
],
|
|
9
|
+
"description": "WebAssembly bindings for Taffy layout library",
|
|
10
|
+
"version": "0.9.2",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/DioxusLabs/taffy"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"taffy_wasm_bg.wasm",
|
|
18
|
+
"taffy_wasm.js",
|
|
19
|
+
"taffy_wasm.d.ts"
|
|
20
|
+
],
|
|
21
|
+
"main": "taffy_wasm.js",
|
|
22
|
+
"types": "taffy_wasm.d.ts",
|
|
23
|
+
"sideEffects": [
|
|
24
|
+
"./snippets/*"
|
|
25
|
+
]
|
|
26
|
+
}
|