taffy-layout 0.1.0
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 +262 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/package.json +81 -0
- package/pkg/README.md +262 -0
- package/pkg/package-lock.json +13 -0
- package/pkg/package.json +24 -0
- package/pkg/taffy_wasm.d.ts +2480 -0
- package/pkg/taffy_wasm.js +3504 -0
- package/pkg/taffy_wasm_bg.wasm +0 -0
- package/pkg/taffy_wasm_bg.wasm.d.ts +150 -0
package/pkg/README.md
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# Taffy Layout
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/taffy-layout)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
High-performance WebAssembly bindings for the [Taffy](https://github.com/DioxusLabs/taffy) layout engine, bringing CSS Flexbox and Grid layout algorithms to JavaScript with near-native performance.
|
|
7
|
+
|
|
8
|
+
## ✨ Features
|
|
9
|
+
|
|
10
|
+
- **🚀 High Performance**: WebAssembly-powered layout calculations
|
|
11
|
+
- **📦 Complete CSS Support**: Full Flexbox and CSS Grid implementation
|
|
12
|
+
- **🔧 Custom Measurement**: Support for custom text/content measurement callbacks
|
|
13
|
+
- **📝 TypeScript Ready**: Complete type definitions included
|
|
14
|
+
- **🌳 Tree-Based API**: Efficient tree structure for complex layouts
|
|
15
|
+
- **💡 Familiar API**: CSS-like property names and values
|
|
16
|
+
|
|
17
|
+
## 📦 Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install taffy-layout
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## 🚀 Quick Start
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import {
|
|
27
|
+
loadTaffy,
|
|
28
|
+
TaffyTree,
|
|
29
|
+
Style,
|
|
30
|
+
Display,
|
|
31
|
+
FlexDirection,
|
|
32
|
+
AlignItems,
|
|
33
|
+
} from "taffy-layout";
|
|
34
|
+
|
|
35
|
+
// Initialize WebAssembly module
|
|
36
|
+
await loadTaffy();
|
|
37
|
+
|
|
38
|
+
// Create a layout tree
|
|
39
|
+
const tree = new TaffyTree();
|
|
40
|
+
|
|
41
|
+
// Create container style
|
|
42
|
+
const containerStyle = new Style();
|
|
43
|
+
containerStyle.display = Display.Flex;
|
|
44
|
+
containerStyle.flexDirection = FlexDirection.Column;
|
|
45
|
+
containerStyle.alignItems = AlignItems.Center;
|
|
46
|
+
containerStyle.size = { width: 300, height: 200 };
|
|
47
|
+
containerStyle.padding = { left: 10, right: 10, top: 10, bottom: 10 };
|
|
48
|
+
|
|
49
|
+
// Create child styles
|
|
50
|
+
const childStyle = new Style();
|
|
51
|
+
childStyle.flexGrow = 1;
|
|
52
|
+
childStyle.size = { width: "100%", height: "auto" };
|
|
53
|
+
|
|
54
|
+
// Create nodes
|
|
55
|
+
const child1 = tree.newLeaf(childStyle);
|
|
56
|
+
const child2 = tree.newLeaf(childStyle);
|
|
57
|
+
const container = tree.newWithChildren(
|
|
58
|
+
containerStyle,
|
|
59
|
+
BigUint64Array.from([child1, child2])
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
// Compute layout
|
|
63
|
+
tree.computeLayout(container, { width: 300, height: 200 });
|
|
64
|
+
|
|
65
|
+
// Read computed layouts
|
|
66
|
+
const containerLayout = tree.getLayout(container);
|
|
67
|
+
const child1Layout = tree.getLayout(child1);
|
|
68
|
+
const child2Layout = tree.getLayout(child2);
|
|
69
|
+
|
|
70
|
+
console.log(`Container: ${containerLayout.width}x${containerLayout.height}`);
|
|
71
|
+
console.log(
|
|
72
|
+
`Child 1: ${child1Layout.width}x${child1Layout.height} at (${child1Layout.x}, ${child1Layout.y})`
|
|
73
|
+
);
|
|
74
|
+
console.log(
|
|
75
|
+
`Child 2: ${child2Layout.width}x${child2Layout.height} at (${child2Layout.x}, ${child2Layout.y})`
|
|
76
|
+
);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## 📖 API Reference
|
|
80
|
+
|
|
81
|
+
### TaffyTree
|
|
82
|
+
|
|
83
|
+
The main class for managing layout trees.
|
|
84
|
+
|
|
85
|
+
[View Documentation](./docs/classes/TaffyTree.md)
|
|
86
|
+
|
|
87
|
+
### Style
|
|
88
|
+
|
|
89
|
+
Configuration object for node layout properties.
|
|
90
|
+
|
|
91
|
+
[View Documentation](./docs/classes/Style.md)
|
|
92
|
+
|
|
93
|
+
### Layout
|
|
94
|
+
|
|
95
|
+
Read-only computed layout result.
|
|
96
|
+
|
|
97
|
+
[View Documentation](./docs/classes/Layout.md)
|
|
98
|
+
|
|
99
|
+
### Enums
|
|
100
|
+
|
|
101
|
+
[View Documentation](./docs/index.md#enumerations)
|
|
102
|
+
|
|
103
|
+
### Types
|
|
104
|
+
|
|
105
|
+
[View Documentation](./docs/index.md#type-aliases)
|
|
106
|
+
|
|
107
|
+
## 📐 Custom Text Measurement
|
|
108
|
+
|
|
109
|
+
For text nodes or other content that needs dynamic measurement:
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
const tree = new TaffyTree();
|
|
113
|
+
const textStyle = new Style();
|
|
114
|
+
const rootNode = tree.newLeaf(new Style());
|
|
115
|
+
const measureTextWidth = (text: string) => text.length * 8;
|
|
116
|
+
const measureTextHeight = (text: string, width: number) => 20;
|
|
117
|
+
|
|
118
|
+
const textNode = tree.newLeafWithContext(textStyle, { text: "Hello, World!" });
|
|
119
|
+
|
|
120
|
+
tree.computeLayoutWithMeasure(
|
|
121
|
+
rootNode,
|
|
122
|
+
{ width: 800, height: "max-content" },
|
|
123
|
+
(known, available, node, context, style) => {
|
|
124
|
+
if (context?.text) {
|
|
125
|
+
// Your text measurement logic here
|
|
126
|
+
const width = measureTextWidth(context.text);
|
|
127
|
+
const height = measureTextHeight(context.text, available.width as number);
|
|
128
|
+
return { width, height };
|
|
129
|
+
}
|
|
130
|
+
return { width: 0, height: 0 };
|
|
131
|
+
}
|
|
132
|
+
);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## 🔧 Error Handling
|
|
136
|
+
|
|
137
|
+
Methods that can fail throw a `TaffyError` as a JavaScript exception. Use try-catch to handle errors:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
try {
|
|
141
|
+
const tree = new TaffyTree();
|
|
142
|
+
const style = new Style();
|
|
143
|
+
const nodeId = tree.newLeaf(style);
|
|
144
|
+
console.log("Created node:", nodeId);
|
|
145
|
+
} catch (e) {
|
|
146
|
+
if (e instanceof TaffyError) {
|
|
147
|
+
console.error("Error:", e.message);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## 🌐 Browser Support
|
|
153
|
+
|
|
154
|
+
Taffy Layout works in all modern browsers that support WebAssembly:
|
|
155
|
+
|
|
156
|
+
- Chrome 57+
|
|
157
|
+
- Firefox 52+
|
|
158
|
+
- Safari 11+
|
|
159
|
+
- Edge 16+
|
|
160
|
+
|
|
161
|
+
## 📚 Examples
|
|
162
|
+
|
|
163
|
+
### Flexbox Row Layout
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
const rowStyle = new Style();
|
|
167
|
+
rowStyle.display = Display.Flex;
|
|
168
|
+
rowStyle.flexDirection = FlexDirection.Row;
|
|
169
|
+
rowStyle.justifyContent = JustifyContent.SpaceBetween;
|
|
170
|
+
rowStyle.gap = { width: 10, height: 0 };
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### CSS Grid Layout
|
|
174
|
+
|
|
175
|
+
```typescript
|
|
176
|
+
import { Style, Display, GridAutoFlow } from "taffy-layout";
|
|
177
|
+
|
|
178
|
+
const gridStyle = new Style();
|
|
179
|
+
gridStyle.display = Display.Grid;
|
|
180
|
+
gridStyle.gridAutoFlow = GridAutoFlow.Row;
|
|
181
|
+
gridStyle.gap = { width: 10, height: 10 };
|
|
182
|
+
|
|
183
|
+
// Grid item placement
|
|
184
|
+
const itemStyle = new Style();
|
|
185
|
+
itemStyle.gridRow = { start: 1, end: 3 }; // Spans 2 rows
|
|
186
|
+
itemStyle.gridColumn = { start: 1, end: { span: 2 } }; // Spans 2 columns
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### Grid Template Areas
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
const gridStyle = new Style();
|
|
193
|
+
gridStyle.display = Display.Grid;
|
|
194
|
+
gridStyle.gridTemplateAreas = [
|
|
195
|
+
{ name: "header", rowStart: 1, rowEnd: 2, columnStart: 1, columnEnd: 4 },
|
|
196
|
+
{ name: "sidebar", rowStart: 2, rowEnd: 4, columnStart: 1, columnEnd: 2 },
|
|
197
|
+
{ name: "main", rowStart: 2, rowEnd: 4, columnStart: 2, columnEnd: 4 },
|
|
198
|
+
{ name: "footer", rowStart: 4, rowEnd: 5, columnStart: 1, columnEnd: 4 },
|
|
199
|
+
];
|
|
200
|
+
|
|
201
|
+
// Named grid lines
|
|
202
|
+
gridStyle.gridTemplateRowNames = [
|
|
203
|
+
["header-start"],
|
|
204
|
+
["header-end", "content-start"],
|
|
205
|
+
["content-end", "footer-start"],
|
|
206
|
+
["footer-end"],
|
|
207
|
+
];
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Absolute Positioning
|
|
211
|
+
|
|
212
|
+
```typescript
|
|
213
|
+
const absoluteStyle = new Style();
|
|
214
|
+
absoluteStyle.position = Position.Absolute;
|
|
215
|
+
absoluteStyle.inset = { left: 10, top: 10, right: "auto", bottom: "auto" };
|
|
216
|
+
absoluteStyle.size = { width: 100, height: 50 };
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Percentage Sizing
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
const percentStyle = new Style();
|
|
223
|
+
percentStyle.size = {
|
|
224
|
+
width: "50%", // 50% of parent
|
|
225
|
+
height: "100%", // 100% of parent
|
|
226
|
+
};
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Block Layout with Replaced Elements
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
const imgStyle = new Style();
|
|
233
|
+
imgStyle.itemIsReplaced = true;
|
|
234
|
+
imgStyle.aspectRatio = 16 / 9; // 16:9 aspect ratio
|
|
235
|
+
imgStyle.size = { width: "100%", height: "auto" };
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## 🏗️ Building from Source
|
|
239
|
+
|
|
240
|
+
```bash
|
|
241
|
+
# Clone the repository
|
|
242
|
+
git clone https://github.com/ByteLandTechnology/taffy-layout.git
|
|
243
|
+
cd taffy-layout
|
|
244
|
+
|
|
245
|
+
# Install dependencies
|
|
246
|
+
npm install
|
|
247
|
+
|
|
248
|
+
# Build the WebAssembly module
|
|
249
|
+
npm run build
|
|
250
|
+
|
|
251
|
+
# Run tests
|
|
252
|
+
npm test
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## 📄 License
|
|
256
|
+
|
|
257
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
258
|
+
|
|
259
|
+
## 🙏 Acknowledgments
|
|
260
|
+
|
|
261
|
+
- [Taffy](https://github.com/DioxusLabs/taffy) - The Rust layout engine this project wraps
|
|
262
|
+
- [wasm-bindgen](https://github.com/rustwasm/wasm-bindgen) - Rust/WebAssembly interoperability
|
package/pkg/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "taffy-wasm",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"collaborators": [
|
|
5
|
+
"ByteLandTechnology <github@byteland.app>"
|
|
6
|
+
],
|
|
7
|
+
"description": "WebAssembly bindings for Taffy layout library",
|
|
8
|
+
"version": "0.9.10",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/ByteLandTechnology/taffy-layout"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"taffy_wasm_bg.wasm",
|
|
16
|
+
"taffy_wasm.js",
|
|
17
|
+
"taffy_wasm.d.ts"
|
|
18
|
+
],
|
|
19
|
+
"main": "taffy_wasm.js",
|
|
20
|
+
"types": "taffy_wasm.d.ts",
|
|
21
|
+
"sideEffects": [
|
|
22
|
+
"./snippets/*"
|
|
23
|
+
]
|
|
24
|
+
}
|