taffy-js 0.2.11 → 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
@@ -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,322 +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
- boxSizing: BoxSizing; // BorderBox, ContentBox
151
- overflow: Point<Overflow>; // Overflow handling
152
-
153
- // Flexbox Properties
154
- flexDirection: FlexDirection; // Row, Column, RowReverse, ColumnReverse
155
- flexWrap: FlexWrap; // NoWrap, Wrap, WrapReverse
156
- flexGrow: number; // Growth factor (default: 0)
157
- flexShrink: number; // Shrink factor (default: 1)
158
- flexBasis: Dimension; // Initial size
159
-
160
- // Alignment Properties
161
- alignItems: AlignItems | undefined;
162
- alignSelf: AlignSelf | undefined;
163
- alignContent: AlignContent | undefined;
164
- justifyContent: JustifyContent | undefined;
165
- justifyItems: AlignItems | undefined; // Grid container default justify
166
- justifySelf: AlignSelf | undefined; // Grid item self-justify
167
-
168
- // Sizing
169
- size: Size<Dimension>; // Width and height
170
- minSize: Size<Dimension>; // Minimum constraints
171
- maxSize: Size<Dimension>; // Maximum constraints
172
- aspectRatio: number | undefined; // Width/height ratio
173
-
174
- // Spacing
175
- margin: Rect<LengthPercentageAuto>;
176
- padding: Rect<LengthPercentage>;
177
- border: Rect<LengthPercentage>;
178
- gap: Size<LengthPercentage>; // Row and column gap
179
- inset: Rect<LengthPercentageAuto>; // For absolute positioning
180
-
181
- // Block Layout Properties
182
- itemIsTable: boolean; // Is this a table element?
183
- itemIsReplaced: boolean; // Is this a replaced element (img, video)?
184
- textAlign: TextAlign; // Legacy text alignment
185
- scrollbarWidth: number; // Scrollbar gutter width in pixels
186
-
187
- // CSS Grid Container Properties
188
- gridAutoFlow: GridAutoFlow; // Row, Column, RowDense, ColumnDense
189
- gridTemplateRows: GridTrack[]; // Track sizing for rows
190
- gridTemplateColumns: GridTrack[]; // Track sizing for columns
191
- gridAutoRows: TrackSizing[]; // Size for implicit rows
192
- gridAutoColumns: TrackSizing[]; // Size for implicit columns
193
- gridTemplateAreas: GridArea[]; // Named grid areas
194
- gridTemplateRowNames: string[][]; // Named lines between rows
195
- gridTemplateColumnNames: string[][]; // Named lines between columns
196
-
197
- // CSS Grid Item Properties
198
- gridRow: Line<GridPlacement>; // grid-row (start/end)
199
- gridColumn: Line<GridPlacement>; // grid-column (start/end)
200
- }
201
- ```
91
+ [View Documentation](https://github.com/ByteLandTechnology/taffy-js/blob/main/docs/classes/Style.md)
202
92
 
203
93
  ### Layout
204
94
 
205
95
  Read-only computed layout result.
206
96
 
207
- ```typescript
208
- class Layout {
209
- // Position (relative to parent)
210
- readonly x: number;
211
- readonly y: number;
212
-
213
- // Size
214
- readonly width: number;
215
- readonly height: number;
216
-
217
- // Content size (for scrollable content)
218
- readonly contentWidth: number;
219
- readonly contentHeight: number;
220
-
221
- // Spacing
222
- readonly paddingTop: number;
223
- readonly paddingRight: number;
224
- readonly paddingBottom: number;
225
- readonly paddingLeft: number;
226
-
227
- readonly borderTop: number;
228
- readonly borderRight: number;
229
- readonly borderBottom: number;
230
- readonly borderLeft: number;
231
-
232
- readonly marginTop: number;
233
- readonly marginRight: number;
234
- readonly marginBottom: number;
235
- readonly marginLeft: number;
236
-
237
- // Scrollbars
238
- readonly scrollbarWidth: number;
239
- readonly scrollbarHeight: number;
240
-
241
- // Rendering order
242
- readonly order: number;
243
- }
244
- ```
97
+ [View Documentation](https://github.com/ByteLandTechnology/taffy-js/blob/main/docs/classes/Layout.md)
245
98
 
246
99
  ### Enums
247
100
 
248
- ```typescript
249
- enum Display {
250
- Block,
251
- Flex,
252
- Grid,
253
- None,
254
- }
255
- enum Position {
256
- Relative,
257
- Absolute,
258
- }
259
- enum FlexDirection {
260
- Row,
261
- Column,
262
- RowReverse,
263
- ColumnReverse,
264
- }
265
- enum FlexWrap {
266
- NoWrap,
267
- Wrap,
268
- WrapReverse,
269
- }
270
- enum AlignItems {
271
- Start,
272
- End,
273
- FlexStart,
274
- FlexEnd,
275
- Center,
276
- Baseline,
277
- Stretch,
278
- }
279
- enum AlignSelf {
280
- Auto,
281
- Start,
282
- End,
283
- FlexStart,
284
- FlexEnd,
285
- Center,
286
- Baseline,
287
- Stretch,
288
- }
289
- enum AlignContent {
290
- Start,
291
- End,
292
- FlexStart,
293
- FlexEnd,
294
- Center,
295
- Stretch,
296
- SpaceBetween,
297
- SpaceAround,
298
- SpaceEvenly,
299
- }
300
- enum JustifyContent {
301
- Start,
302
- End,
303
- FlexStart,
304
- FlexEnd,
305
- Center,
306
- Stretch,
307
- SpaceBetween,
308
- SpaceAround,
309
- SpaceEvenly,
310
- }
311
- enum Overflow {
312
- Visible,
313
- Clip,
314
- Hidden,
315
- Scroll,
316
- }
317
- enum BoxSizing {
318
- BorderBox,
319
- ContentBox,
320
- }
321
- enum TextAlign {
322
- Auto,
323
- LegacyLeft,
324
- LegacyRight,
325
- LegacyCenter,
326
- }
327
- enum GridAutoFlow {
328
- Row,
329
- Column,
330
- RowDense,
331
- ColumnDense,
332
- }
333
- ```
101
+ [View Documentation](https://github.com/ByteLandTechnology/taffy-js/blob/main/docs/modules.md#enums)
334
102
 
335
103
  ### Types
336
104
 
337
- ```typescript
338
- // Dimension values (CSS-like syntax)
339
- type Dimension = number | `${number}%` | "auto"; // e.g., 100, "50%", "auto"
340
- type LengthPercentage = number | `${number}%`; // e.g., 10, "25%"
341
- type LengthPercentageAuto = number | `${number}%` | "auto";
342
-
343
- // Geometry
344
- interface Size<T> {
345
- width: T;
346
- height: T;
347
- }
348
- interface Rect<T> {
349
- left: T;
350
- right: T;
351
- top: T;
352
- bottom: T;
353
- }
354
- interface Point<T> {
355
- x: T;
356
- y: T;
357
- }
358
-
359
- // Available space for layout computation
360
- type AvailableSpace = number | "minContent" | "maxContent";
361
-
362
- // Grid Placement (CSS grid-row-start / grid-column-start)
363
- type GridPlacement = "auto" | number | { span: number };
364
-
365
- // Grid Line (CSS grid-row / grid-column shorthand)
366
- interface Line<T> {
367
- start: T;
368
- end: T;
369
- }
370
-
371
- // Grid Template Area
372
- interface GridArea {
373
- name: string;
374
- row_start: number;
375
- row_end: number;
376
- column_start: number;
377
- column_end: number;
378
- }
379
-
380
- // Measure function for custom content measurement
381
- type MeasureFunction = (
382
- knownDimensions: Size<number | undefined>,
383
- availableSpace: Size<AvailableSpace>,
384
- node: bigint,
385
- context: any,
386
- style: Style,
387
- ) => Size<number>;
388
- ```
105
+ [View Documentation](https://github.com/ByteLandTechnology/taffy-js/blob/main/docs/modules.md#type-aliases)
389
106
 
390
107
  ## 📐 Custom Text Measurement
391
108
 
392
109
  For text nodes or other content that needs dynamic measurement:
393
110
 
394
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
+
395
118
  const textNode = tree.newLeafWithContext(textStyle, { text: "Hello, World!" });
396
119
 
397
120
  tree.computeLayoutWithMeasure(
398
121
  rootNode,
399
- { width: 800, height: "maxContent" },
122
+ { width: 800, height: "max-content" },
400
123
  (known, available, node, context, style) => {
401
124
  if (context?.text) {
402
125
  // Your text measurement logic here
403
126
  const width = measureTextWidth(context.text);
404
- const height = measureTextHeight(context.text, available.width);
127
+ const height = measureTextHeight(context.text, available.width as number);
405
128
  return { width, height };
406
129
  }
407
130
  return { width: 0, height: 0 };
@@ -415,11 +138,14 @@ Methods that can fail throw a `TaffyError` as a JavaScript exception. Use try-ca
415
138
 
416
139
  ```typescript
417
140
  try {
141
+ const tree = new TaffyTree();
142
+ const style = new Style();
418
143
  const nodeId = tree.newLeaf(style);
419
144
  console.log("Created node:", nodeId);
420
- } catch (error) {
421
- // error is a TaffyError instance
422
- console.error("Error:", error.message);
145
+ } catch (e) {
146
+ if (e instanceof TaffyError) {
147
+ console.error("Error:", e.message);
148
+ }
423
149
  }
424
150
  ```
425
151
 
@@ -466,10 +192,10 @@ itemStyle.gridColumn = { start: 1, end: { span: 2 } }; // Spans 2 columns
466
192
  const gridStyle = new Style();
467
193
  gridStyle.display = Display.Grid;
468
194
  gridStyle.gridTemplateAreas = [
469
- { name: "header", row_start: 1, row_end: 2, column_start: 1, column_end: 4 },
470
- { name: "sidebar", row_start: 2, row_end: 4, column_start: 1, column_end: 2 },
471
- { name: "main", row_start: 2, row_end: 4, column_start: 2, column_end: 4 },
472
- { name: "footer", row_start: 4, row_end: 5, column_start: 1, column_end: 4 },
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 },
473
199
  ];
474
200
 
475
201
  // Named grid lines
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "taffy-js",
3
- "version": "0.2.11",
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
  },