taffy-js 0.2.10 → 0.2.12

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 CHANGED
@@ -22,7 +22,7 @@ npm install taffy-js
22
22
 
23
23
  ## 🚀 Quick Start
24
24
 
25
- ```javascript
25
+ ```typescript
26
26
  import {
27
27
  loadTaffy,
28
28
  TaffyTree,
@@ -32,52 +32,48 @@ import {
32
32
  AlignItems,
33
33
  } from "taffy-js";
34
34
 
35
- async function main() {
36
- // Initialize WebAssembly module
37
- await loadTaffy();
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
+ );
38
61
 
39
- // Create a layout tree
40
- const tree = new TaffyTree();
62
+ // Compute layout
63
+ tree.computeLayout(container, { width: 300, height: 200 });
41
64
 
42
- // Create container style
43
- const containerStyle = new Style();
44
- containerStyle.display = Display.Flex;
45
- containerStyle.flexDirection = FlexDirection.Column;
46
- containerStyle.alignItems = AlignItems.Center;
47
- containerStyle.size = { width: 300, height: 200 };
48
- containerStyle.padding = { left: 10, right: 10, top: 10, bottom: 10 };
49
-
50
- // Create child styles
51
- const childStyle = new Style();
52
- childStyle.flexGrow = 1;
53
- childStyle.size = { width: "100%", height: "auto" };
54
-
55
- // Create nodes
56
- const child1 = tree.newLeaf(childStyle);
57
- const child2 = tree.newLeaf(childStyle);
58
- const container = tree.newWithChildren(
59
- containerStyle,
60
- BigUint64Array.from([child1, child2]),
61
- );
62
-
63
- // Compute layout
64
- tree.computeLayout(container, { width: 300, height: 200 });
65
-
66
- // Read computed layouts
67
- const containerLayout = tree.getLayout(container);
68
- const child1Layout = tree.getLayout(child1);
69
- const child2Layout = tree.getLayout(child2);
70
-
71
- console.log(`Container: ${containerLayout.width}x${containerLayout.height}`);
72
- console.log(
73
- `Child 1: ${child1Layout.width}x${child1Layout.height} at (${child1Layout.x}, ${child1Layout.y})`,
74
- );
75
- console.log(
76
- `Child 2: ${child2Layout.width}x${child2Layout.height} at (${child2Layout.x}, ${child2Layout.y})`,
77
- );
78
- }
65
+ // Read computed layouts
66
+ const containerLayout = tree.getLayout(container);
67
+ const child1Layout = tree.getLayout(child1);
68
+ const child2Layout = tree.getLayout(child2);
79
69
 
80
- main();
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
+ );
81
77
  ```
82
78
 
83
79
  ## 📖 API Reference
@@ -86,272 +82,49 @@ main();
86
82
 
87
83
  The main class for managing layout trees.
88
84
 
89
- ```typescript
90
- class TaffyTree {
91
- // Construction
92
- constructor();
93
- static withCapacity(capacity: number): TaffyTree;
94
-
95
- // Node Creation (throws TaffyError on failure)
96
- newLeaf(style: Style): bigint;
97
- newLeafWithContext(style: Style, context: any): bigint;
98
- newWithChildren(style: Style, children: BigUint64Array): bigint;
99
-
100
- // Tree Operations
101
- clear(): void;
102
- remove(node: bigint): bigint; // throws TaffyError
103
- totalNodeCount(): number;
104
-
105
- // Child Management (throws TaffyError on failure)
106
- addChild(parent: bigint, child: bigint): void;
107
- removeChild(parent: bigint, child: bigint): bigint;
108
- setChildren(parent: bigint, children: BigUint64Array): void;
109
- children(parent: bigint): BigUint64Array;
110
- childCount(parent: bigint): number;
111
- parent(child: bigint): bigint | undefined;
112
-
113
- // Style Management (throws TaffyError on failure)
114
- setStyle(node: bigint, style: Style): void;
115
- getStyle(node: bigint): Style;
116
-
117
- // Layout Computation (throws TaffyError on failure)
118
- computeLayout(node: bigint, availableSpace: Size<AvailableSpace>): void;
119
- computeLayoutWithMeasure(
120
- node: bigint,
121
- availableSpace: Size<AvailableSpace>,
122
- measureFunc: MeasureFunction,
123
- ): void;
124
-
125
- // Layout Results (throws TaffyError on failure)
126
- getLayout(node: bigint): Layout;
127
- unroundedLayout(node: bigint): Layout;
128
-
129
- // Dirty Tracking (throws TaffyError on failure)
130
- markDirty(node: bigint): void;
131
- dirty(node: bigint): boolean;
132
-
133
- // Configuration
134
- enableRounding(): void;
135
- disableRounding(): void;
136
- }
137
- ```
85
+ [View Documentation](https://github.com/ByteLandTechnology/taffy-js/blob/main/docs/classes/TaffyTree.md)
138
86
 
139
87
  ### Style
140
88
 
141
89
  Configuration object for node layout properties.
142
90
 
143
- ```typescript
144
- class Style {
145
- constructor();
146
-
147
- // Layout Mode
148
- display: Display; // Block, Flex, Grid, None
149
- position: Position; // Relative, Absolute
150
-
151
- // Flexbox
152
- flexDirection: FlexDirection; // Row, Column, RowReverse, ColumnReverse
153
- flexWrap: FlexWrap; // NoWrap, Wrap, WrapReverse
154
- flexGrow: number; // Growth factor (default: 0)
155
- flexShrink: number; // Shrink factor (default: 1)
156
- flexBasis: Dimension; // Initial size
157
-
158
- // Alignment
159
- alignItems: AlignItems | undefined;
160
- alignSelf: AlignSelf | undefined;
161
- alignContent: AlignContent | undefined;
162
- justifyContent: JustifyContent | undefined;
163
-
164
- // Sizing
165
- size: Size<Dimension>; // Width and height
166
- minSize: Size<Dimension>; // Minimum constraints
167
- maxSize: Size<Dimension>; // Maximum constraints
168
- aspectRatio: number | undefined; // Width/height ratio
169
- boxSizing: BoxSizing; // BorderBox, ContentBox
170
-
171
- // Spacing
172
- margin: Rect<LengthPercentageAuto>;
173
- padding: Rect<LengthPercentage>;
174
- border: Rect<LengthPercentage>;
175
- gap: Size<LengthPercentage>; // Row and column gap
176
- inset: Rect<LengthPercentageAuto>; // For absolute positioning
177
-
178
- // Overflow
179
- overflow: Point<Overflow>;
180
- }
181
- ```
91
+ [View Documentation](https://github.com/ByteLandTechnology/taffy-js/blob/main/docs/classes/Style.md)
182
92
 
183
93
  ### Layout
184
94
 
185
95
  Read-only computed layout result.
186
96
 
187
- ```typescript
188
- class Layout {
189
- // Position (relative to parent)
190
- readonly x: number;
191
- readonly y: number;
192
-
193
- // Size
194
- readonly width: number;
195
- readonly height: number;
196
-
197
- // Content size (for scrollable content)
198
- readonly contentWidth: number;
199
- readonly contentHeight: number;
200
-
201
- // Spacing
202
- readonly paddingTop: number;
203
- readonly paddingRight: number;
204
- readonly paddingBottom: number;
205
- readonly paddingLeft: number;
206
-
207
- readonly borderTop: number;
208
- readonly borderRight: number;
209
- readonly borderBottom: number;
210
- readonly borderLeft: number;
211
-
212
- readonly marginTop: number;
213
- readonly marginRight: number;
214
- readonly marginBottom: number;
215
- readonly marginLeft: number;
216
-
217
- // Scrollbars
218
- readonly scrollbarWidth: number;
219
- readonly scrollbarHeight: number;
220
-
221
- // Rendering order
222
- readonly order: number;
223
- }
224
- ```
97
+ [View Documentation](https://github.com/ByteLandTechnology/taffy-js/blob/main/docs/classes/Layout.md)
225
98
 
226
99
  ### Enums
227
100
 
228
- ```typescript
229
- enum Display {
230
- Block,
231
- Flex,
232
- Grid,
233
- None,
234
- }
235
- enum Position {
236
- Relative,
237
- Absolute,
238
- }
239
- enum FlexDirection {
240
- Row,
241
- Column,
242
- RowReverse,
243
- ColumnReverse,
244
- }
245
- enum FlexWrap {
246
- NoWrap,
247
- Wrap,
248
- WrapReverse,
249
- }
250
- enum AlignItems {
251
- Start,
252
- End,
253
- FlexStart,
254
- FlexEnd,
255
- Center,
256
- Baseline,
257
- Stretch,
258
- }
259
- enum AlignSelf {
260
- Auto,
261
- Start,
262
- End,
263
- FlexStart,
264
- FlexEnd,
265
- Center,
266
- Baseline,
267
- Stretch,
268
- }
269
- enum AlignContent {
270
- Start,
271
- End,
272
- FlexStart,
273
- FlexEnd,
274
- Center,
275
- Stretch,
276
- SpaceBetween,
277
- SpaceAround,
278
- SpaceEvenly,
279
- }
280
- enum JustifyContent {
281
- Start,
282
- End,
283
- FlexStart,
284
- FlexEnd,
285
- Center,
286
- Stretch,
287
- SpaceBetween,
288
- SpaceAround,
289
- SpaceEvenly,
290
- }
291
- enum Overflow {
292
- Visible,
293
- Hidden,
294
- Scroll,
295
- Auto,
296
- }
297
- enum BoxSizing {
298
- BorderBox,
299
- ContentBox,
300
- }
301
- ```
101
+ [View Documentation](https://github.com/ByteLandTechnology/taffy-js/blob/main/docs/modules.md#enums)
302
102
 
303
103
  ### Types
304
104
 
305
- ```typescript
306
- // Dimension values (CSS-like syntax)
307
- type Dimension = number | `${number}%` | "auto"; // e.g., 100, "50%", "auto"
308
- type LengthPercentage = number | `${number}%`; // e.g., 10, "25%"
309
- type LengthPercentageAuto = number | `${number}%` | "auto";
310
-
311
- // Geometry
312
- interface Size<T> {
313
- width: T;
314
- height: T;
315
- }
316
- interface Rect<T> {
317
- left: T;
318
- right: T;
319
- top: T;
320
- bottom: T;
321
- }
322
- interface Point<T> {
323
- x: T;
324
- y: T;
325
- }
326
-
327
- // Available space for layout computation
328
- type AvailableSpace = number | "minContent" | "maxContent";
329
-
330
- // Measure function for custom content measurement
331
- type MeasureFunction = (
332
- knownDimensions: Size<number | undefined>,
333
- availableSpace: Size<AvailableSpace>,
334
- node: bigint,
335
- context: any,
336
- style: Style,
337
- ) => Size<number>;
338
- ```
105
+ [View Documentation](https://github.com/ByteLandTechnology/taffy-js/blob/main/docs/modules.md#type-aliases)
339
106
 
340
107
  ## 📐 Custom Text Measurement
341
108
 
342
109
  For text nodes or other content that needs dynamic measurement:
343
110
 
344
- ```javascript
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
+
345
118
  const textNode = tree.newLeafWithContext(textStyle, { text: "Hello, World!" });
346
119
 
347
120
  tree.computeLayoutWithMeasure(
348
121
  rootNode,
349
- { width: 800, height: "maxContent" },
122
+ { width: 800, height: "max-content" },
350
123
  (known, available, node, context, style) => {
351
124
  if (context?.text) {
352
125
  // Your text measurement logic here
353
126
  const width = measureTextWidth(context.text);
354
- const height = measureTextHeight(context.text, available.width);
127
+ const height = measureTextHeight(context.text, available.width as number);
355
128
  return { width, height };
356
129
  }
357
130
  return { width: 0, height: 0 };
@@ -363,13 +136,16 @@ tree.computeLayoutWithMeasure(
363
136
 
364
137
  Methods that can fail throw a `TaffyError` as a JavaScript exception. Use try-catch to handle errors:
365
138
 
366
- ```javascript
139
+ ```typescript
367
140
  try {
141
+ const tree = new TaffyTree();
142
+ const style = new Style();
368
143
  const nodeId = tree.newLeaf(style);
369
144
  console.log("Created node:", nodeId);
370
- } catch (error) {
371
- // error is a TaffyError instance
372
- console.error("Error:", error.message);
145
+ } catch (e) {
146
+ if (e instanceof TaffyError) {
147
+ console.error("Error:", e.message);
148
+ }
373
149
  }
374
150
  ```
375
151
 
@@ -386,7 +162,7 @@ Taffy-JS works in all modern browsers that support WebAssembly:
386
162
 
387
163
  ### Flexbox Row Layout
388
164
 
389
- ```javascript
165
+ ```typescript
390
166
  const rowStyle = new Style();
391
167
  rowStyle.display = Display.Flex;
392
168
  rowStyle.flexDirection = FlexDirection.Row;
@@ -394,9 +170,46 @@ rowStyle.justifyContent = JustifyContent.SpaceBetween;
394
170
  rowStyle.gap = { width: 10, height: 0 };
395
171
  ```
396
172
 
173
+ ### CSS Grid Layout
174
+
175
+ ```typescript
176
+ import { Style, Display, GridAutoFlow } from "taffy-js";
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
+
397
210
  ### Absolute Positioning
398
211
 
399
- ```javascript
212
+ ```typescript
400
213
  const absoluteStyle = new Style();
401
214
  absoluteStyle.position = Position.Absolute;
402
215
  absoluteStyle.inset = { left: 10, top: 10, right: "auto", bottom: "auto" };
@@ -405,7 +218,7 @@ absoluteStyle.size = { width: 100, height: 50 };
405
218
 
406
219
  ### Percentage Sizing
407
220
 
408
- ```javascript
221
+ ```typescript
409
222
  const percentStyle = new Style();
410
223
  percentStyle.size = {
411
224
  width: "50%", // 50% of parent
@@ -413,6 +226,15 @@ percentStyle.size = {
413
226
  };
414
227
  ```
415
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
+
416
238
  ## 🏗️ Building from Source
417
239
 
418
240
  ```bash
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "taffy-js",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "WebAssembly bindings for Taffy layout library",
5
5
  "keywords": [
6
6
  "layout",
@@ -41,10 +41,11 @@
41
41
  "scripts": {
42
42
  "build": "npm run build:wasm && npm run build:ts && npm run docs",
43
43
  "build:wasm": "wasm-pack build --release --target web && rm -f pkg/.gitignore && npm run patch-dts",
44
- "build:ts": "tsc",
44
+ "build:ts": "tsc && npm run generate:examples",
45
45
  "build:dev": "wasm-pack build --dev --target web && npm run patch-dts && npm run build:ts",
46
46
  "docs": "typedoc && prettier --write docs",
47
47
  "patch-dts": "npx tsx scripts/patch-dts.ts",
48
+ "generate:examples": "npx tsx scripts/generate-example-tests.ts",
48
49
  "test": "vitest run",
49
50
  "prepare": "husky"
50
51
  },
@@ -77,4 +78,4 @@
77
78
  "*.{js,ts,jsx,tsx,json,md,yaml,yml}": "prettier --write",
78
79
  "*.rs": "cargo fmt --"
79
80
  }
80
- }
81
+ }