@solidtv/solid 1.7.4 → 1.8.0-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.
Files changed (68) hide show
  1. package/MIGRATION-1.8.md +270 -0
  2. package/dist/src/core/dom-renderer/dashedBorderStyle.d.ts +20 -0
  3. package/dist/src/core/dom-renderer/dashedBorderStyle.js +104 -0
  4. package/dist/src/core/dom-renderer/dashedBorderStyle.js.map +1 -0
  5. package/dist/src/core/dom-renderer/domRenderer.d.ts +14 -0
  6. package/dist/src/core/dom-renderer/domRenderer.js +194 -12
  7. package/dist/src/core/dom-renderer/domRenderer.js.map +1 -1
  8. package/dist/src/core/dom-renderer/domRendererTypes.d.ts +12 -2
  9. package/dist/src/core/elementNode.d.ts +176 -9
  10. package/dist/src/core/elementNode.js +1906 -411
  11. package/dist/src/core/elementNode.js.map +1 -1
  12. package/dist/src/core/flexLayout.d.ts +6 -0
  13. package/dist/src/core/flexLayout.js +535 -218
  14. package/dist/src/core/flexLayout.js.map +1 -1
  15. package/dist/src/core/focusManager.js +70 -23
  16. package/dist/src/core/focusManager.js.map +1 -1
  17. package/dist/src/core/fontLoaded.d.ts +10 -0
  18. package/dist/src/core/fontLoaded.js +26 -0
  19. package/dist/src/core/fontLoaded.js.map +1 -0
  20. package/dist/src/core/intrinsicTypes.d.ts +2 -1
  21. package/dist/src/core/lightningInit.js +8 -1
  22. package/dist/src/core/lightningInit.js.map +1 -1
  23. package/dist/src/core/shaders.d.ts +5 -2
  24. package/dist/src/core/shaders.js +6 -0
  25. package/dist/src/core/shaders.js.map +1 -1
  26. package/dist/src/core/states.d.ts +2 -0
  27. package/dist/src/core/states.js +26 -7
  28. package/dist/src/core/states.js.map +1 -1
  29. package/dist/src/core/stylePlan.d.ts +39 -0
  30. package/dist/src/core/stylePlan.js +159 -0
  31. package/dist/src/core/stylePlan.js.map +1 -0
  32. package/dist/src/core/utils.js +2 -2
  33. package/dist/src/primitives/Grid.jsx +22 -3
  34. package/dist/src/primitives/Grid.jsx.map +1 -1
  35. package/dist/src/primitives/Virtual.jsx +16 -6
  36. package/dist/src/primitives/Virtual.jsx.map +1 -1
  37. package/dist/src/primitives/utils/handleNavigation.js +29 -6
  38. package/dist/src/primitives/utils/handleNavigation.js.map +1 -1
  39. package/dist/src/primitives/utils/withScrolling.js +2 -1
  40. package/dist/src/primitives/utils/withScrolling.js.map +1 -1
  41. package/dist/src/solidOpts.js +22 -8
  42. package/dist/src/solidOpts.js.map +1 -1
  43. package/dist/tsconfig.tsbuildinfo +1 -1
  44. package/docs/AI_CODING_GUIDELINES.md +7 -6
  45. package/docs/AI_UPGRADE_1.8.md +290 -0
  46. package/package.json +5 -3
  47. package/src/core/dom-renderer/dashedBorderStyle.ts +128 -0
  48. package/src/core/dom-renderer/domRenderer.ts +201 -10
  49. package/src/core/dom-renderer/domRendererTypes.ts +12 -0
  50. package/src/core/elementNode.ts +2124 -459
  51. package/src/core/flexLayout.ts +583 -231
  52. package/src/core/focusManager.ts +86 -27
  53. package/src/core/fontLoaded.ts +29 -0
  54. package/src/core/intrinsicTypes.ts +2 -0
  55. package/src/core/lightningInit.ts +8 -1
  56. package/src/core/shaders.ts +12 -0
  57. package/src/core/states.ts +29 -9
  58. package/src/core/stylePlan.ts +195 -0
  59. package/src/core/utils.ts +2 -2
  60. package/src/primitives/Grid.tsx +35 -9
  61. package/src/primitives/Virtual.tsx +20 -6
  62. package/src/primitives/utils/handleNavigation.ts +40 -6
  63. package/src/primitives/utils/withScrolling.ts +3 -2
  64. package/src/solidOpts.ts +23 -8
  65. package/dist/src/core/flex.d.ts +0 -2
  66. package/dist/src/core/flex.js +0 -285
  67. package/dist/src/core/flex.js.map +0 -1
  68. package/src/core/flex.ts +0 -332
@@ -1,7 +1,42 @@
1
1
  import { type ElementNode } from './elementNode.js';
2
+ import { Config } from './config.js';
2
3
  import { isTextNode, isElementText } from './utils.js';
3
4
 
4
- function getArrayValue(
5
+ // One flex pass over one container. It allocates nothing in the steady
6
+ // state: the per-child values live in module-level typed arrays that grow on
7
+ // demand and are reused by every pass, and the children are reached through
8
+ // an index array (`order`), sorted by `flexOrder` only when a child has one.
9
+ // A prop is written only when it must be, so a relayout that moves nothing
10
+ // writes nothing (and starts no transition). A prop with a transition reads
11
+ // back its current, animated value, not its target, so for those the last
12
+ // value this engine wrote is kept on the node (`_flexX`, `_flexY`, `_flexW`,
13
+ // `_flexH`) and the write is skipped only when both match.
14
+
15
+ /** Slot flags. */
16
+ const FROM_RECORD = 1; // the main size came from the flex record (B8)
17
+ const GROWN = 2;
18
+ const SHRUNK = 4;
19
+
20
+ let capacity = 0;
21
+ let childIndex = new Int32Array(0); // slot -> index in node.children
22
+ let order = new Int32Array(0); // layout position -> slot
23
+ let flags = new Uint8Array(0);
24
+ let mainSize = new Float64Array(0);
25
+ let mainRead = new Float64Array(0); // the main size read from the child
26
+ let ownSize = new Float64Array(0); // the size before flexGrow/flexShrink (B8)
27
+ let marginStart = new Float64Array(0);
28
+ let marginEnd = new Float64Array(0);
29
+ let totalMain = new Float64Array(0);
30
+ let crossSize = new Float64Array(0);
31
+ let marginCrossStart = new Float64Array(0);
32
+ let marginCrossEnd = new Float64Array(0);
33
+ let growFactor = new Float64Array(0);
34
+ let shrinkFactor = new Float64Array(0);
35
+ let orderKey = new Float64Array(0);
36
+ let lineStart = new Float64Array(0);
37
+
38
+ /** A `padding`/`margin` side from a number or a 2-4 value array. */
39
+ export function getArrayValue(
5
40
  val: number | number[] | undefined,
6
41
  index: number,
7
42
  defaultValue: number = 0,
@@ -21,13 +56,178 @@ function getArrayValue(
21
56
  return result ?? defaultValue;
22
57
  }
23
58
 
59
+ /**
60
+ * Whether a write of `name` (or its alias) on `c` starts a transition, as
61
+ * ElementNode's animatable setter decides it.
62
+ */
63
+ function animates(c: ElementNode, name: string, alias: string): boolean {
64
+ const t = c.transition;
65
+ if (!t || c.rendered !== true || !Config.animationsEnabled) {
66
+ return false;
67
+ }
68
+ return t === true || !!t[name] || !!t[alias];
69
+ }
70
+
71
+ function setSize(c: ElementNode, isWidth: boolean, v: number): void {
72
+ if (isWidth) {
73
+ c.width = v;
74
+ } else {
75
+ c.height = v;
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Writes a size the engine computed, unless `current` (read from the node)
81
+ * is already `v` and, with a transition on it (`current` is then the
82
+ * animated value), `v` is also what this engine last wrote. Returns whether
83
+ * it wrote. Callers test `current !== v || c.transition` first, so the
84
+ * common no-write case passes no doubles to a call.
85
+ */
86
+ function writeSize(
87
+ c: ElementNode,
88
+ isWidth: boolean,
89
+ current: number,
90
+ v: number,
91
+ ): boolean {
92
+ const animated = c.transition
93
+ ? isWidth
94
+ ? animates(c, 'w', 'width')
95
+ : animates(c, 'h', 'height')
96
+ : false;
97
+ if (current === v && (!animated || (isWidth ? c._flexW : c._flexH) === v)) {
98
+ return false;
99
+ }
100
+ if (animated) {
101
+ if (isWidth) {
102
+ c._flexW = v;
103
+ } else {
104
+ c._flexH = v;
105
+ }
106
+ }
107
+ setSize(c, isWidth, v);
108
+ return true;
109
+ }
110
+
111
+ /** Writes x or y as writeSize writes a size. */
112
+ function setPos(c: ElementNode, isX: boolean, v: number): void {
113
+ if (isX) {
114
+ const animated = c.transition ? animates(c, 'x', 'x') : false;
115
+ if (c.x === v && (!animated || c._flexX === v)) return;
116
+ if (animated) c._flexX = v;
117
+ c.x = v;
118
+ } else {
119
+ const animated = c.transition ? animates(c, 'y', 'y') : false;
120
+ if (c.y === v && (!animated || c._flexY === v)) return;
121
+ if (animated) c._flexY = v;
122
+ c.y = v;
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Places child `c` (slot `k`) on the cross axis of a line that starts at
128
+ * `start` and is `size` long.
129
+ */
130
+ function alignCross(
131
+ c: ElementNode,
132
+ k: number,
133
+ isRow: boolean,
134
+ align: string | undefined,
135
+ start: number,
136
+ size: number,
137
+ ): void {
138
+ const alignSelf = c.alignSelf || align;
139
+ if (alignSelf === 'flexStart') {
140
+ setPos(c, !isRow, start + marginCrossStart[k]!);
141
+ } else if (alignSelf === 'center') {
142
+ setPos(
143
+ c,
144
+ !isRow,
145
+ start + (size - crossSize[k]!) / 2 + marginCrossStart[k]!,
146
+ );
147
+ } else if (alignSelf === 'flexEnd') {
148
+ setPos(c, !isRow, start + size - crossSize[k]! - marginCrossEnd[k]!);
149
+ }
150
+ }
151
+
152
+ let passDepth = 0;
153
+
154
+ /**
155
+ * Lays out the children of a `display: 'flex'` container. Returns true when
156
+ * the container's own size changed, so its parent must be laid out again.
157
+ */
24
158
  export default function (node: ElementNode): boolean {
159
+ if (passDepth === 0) {
160
+ passDepth = 1;
161
+ try {
162
+ return layout(node);
163
+ } finally {
164
+ passDepth = 0;
165
+ }
166
+ }
167
+ return nestedLayout(node);
168
+ }
169
+
170
+ /**
171
+ * A pass started while another runs (from an app callback a write fired,
172
+ * such as onAnimation calling updateLayout): it gets its own scratch, so
173
+ * the outer pass's is intact when it resumes. Allocates; not the hot path.
174
+ */
175
+ function nestedLayout(node: ElementNode): boolean {
176
+ const sCapacity = capacity;
177
+ const sChildIndex = childIndex;
178
+ const sOrder = order;
179
+ const sFlags = flags;
180
+ const sMainSize = mainSize;
181
+ const sMainRead = mainRead;
182
+ const sOwnSize = ownSize;
183
+ const sMarginStart = marginStart;
184
+ const sMarginEnd = marginEnd;
185
+ const sTotalMain = totalMain;
186
+ const sCrossSize = crossSize;
187
+ const sMarginCrossStart = marginCrossStart;
188
+ const sMarginCrossEnd = marginCrossEnd;
189
+ const sGrowFactor = growFactor;
190
+ const sShrinkFactor = shrinkFactor;
191
+ const sOrderKey = orderKey;
192
+ const sLineStart = lineStart;
193
+ capacity = 0;
194
+ passDepth++;
195
+ try {
196
+ return layout(node);
197
+ } finally {
198
+ passDepth--;
199
+ capacity = sCapacity;
200
+ childIndex = sChildIndex;
201
+ order = sOrder;
202
+ flags = sFlags;
203
+ mainSize = sMainSize;
204
+ mainRead = sMainRead;
205
+ ownSize = sOwnSize;
206
+ marginStart = sMarginStart;
207
+ marginEnd = sMarginEnd;
208
+ totalMain = sTotalMain;
209
+ crossSize = sCrossSize;
210
+ marginCrossStart = sMarginCrossStart;
211
+ marginCrossEnd = sMarginCrossEnd;
212
+ growFactor = sGrowFactor;
213
+ shrinkFactor = sShrinkFactor;
214
+ orderKey = sOrderKey;
215
+ lineStart = sLineStart;
216
+ }
217
+ }
218
+
219
+ function layout(node: ElementNode): boolean {
220
+ const children = node.children;
221
+ const numChildren = children.length;
222
+
223
+ if (numChildren === 0) {
224
+ return false;
225
+ }
226
+
25
227
  const direction = node.flexDirection || 'row';
26
228
  const isRow = direction === 'row' || direction === 'row-reverse';
27
229
  const isReverse =
28
230
  direction === 'row-reverse' || direction === 'column-reverse';
29
- const dimension = isRow ? 'width' : 'height';
30
- const crossDimension = isRow ? 'height' : 'width';
31
231
 
32
232
  // padding order: Top, Right, Bottom, Left
33
233
  const nodePadding = node.padding;
@@ -42,207 +242,311 @@ export default function (node: ElementNode): boolean {
42
242
  const paddingCrossEnd = isRow ? paddingBottom : paddingRight;
43
243
  const nodePaddingTotal = paddingStart + paddingEnd;
44
244
 
45
- const minDimension = isRow ? 'minWidth' : 'minHeight';
46
- const crossMinDimension = isRow ? 'minHeight' : 'minWidth';
47
-
48
- const children = node.children;
49
- const numChildren = children.length;
50
-
51
- if (numChildren === 0) {
52
- return false;
245
+ const flexWrap = node.flexWrap;
246
+ const isWrapReverse = flexWrap === 'wrap-reverse';
247
+ // B10: wrap-reverse wraps too.
248
+ const wrapping = flexWrap === 'wrap' || isWrapReverse;
249
+ const align = node.alignItems || (flexWrap ? 'flexStart' : undefined);
250
+
251
+ // Scratch for numChildren slots. Written out here, not a helper: terser's
252
+ // default `reduce_funcs` makes a function with one call site a closure
253
+ // created per call, and one created in this cold branch is no better: in
254
+ // Chrome 141 the function holding it (Maglev) then boxes this pass's
255
+ // doubles (virtual-grid: 2 to 5.5 KiB per press).
256
+ if (numChildren > capacity) {
257
+ let c = capacity * 2;
258
+ if (c < numChildren) {
259
+ c = numChildren < 16 ? 16 : numChildren;
260
+ }
261
+ capacity = c;
262
+ childIndex = new Int32Array(c);
263
+ order = new Int32Array(c);
264
+ flags = new Uint8Array(c);
265
+ mainSize = new Float64Array(c);
266
+ mainRead = new Float64Array(c);
267
+ ownSize = new Float64Array(c);
268
+ marginStart = new Float64Array(c);
269
+ marginEnd = new Float64Array(c);
270
+ totalMain = new Float64Array(c);
271
+ crossSize = new Float64Array(c);
272
+ marginCrossStart = new Float64Array(c);
273
+ marginCrossEnd = new Float64Array(c);
274
+ growFactor = new Float64Array(c);
275
+ shrinkFactor = new Float64Array(c);
276
+ orderKey = new Float64Array(c);
277
+ lineStart = new Float64Array(c);
53
278
  }
54
279
 
55
- // Optimize arrays caching
56
- const processableChildrenIndices: number[] = [];
280
+ let n = 0;
57
281
  let hasOrder = false;
58
282
  let totalFlexGrow = 0;
59
283
  let totalFlexShrink = 0;
284
+ let sizeWork = false;
60
285
 
61
286
  for (let i = 0; i < numChildren; i++) {
62
287
  const c = children[i]!;
63
288
 
64
- if (isElementText(c) && c.text && !(c.width || c.height)) {
65
- return false; // specific text layout constraint
66
- }
67
-
289
+ // B5: a child that is not a flex item is skipped before the text check,
290
+ // so an unsized text with flexItem={false} does not block the pass.
68
291
  if (isTextNode(c) || c.flexItem === false) {
69
292
  continue;
70
293
  }
71
294
 
72
- if (c.flexOrder !== undefined) {
295
+ const w = c.width;
296
+ const h = c.height;
297
+ if (isElementText(c) && c.text && !(w || h)) {
298
+ return false; // specific text layout constraint
299
+ }
300
+
301
+ const flexOrder = c.flexOrder;
302
+ if (flexOrder !== undefined) {
73
303
  hasOrder = true;
74
304
  }
75
305
 
76
306
  const flexGrow = c.flexGrow;
77
- if (flexGrow !== undefined && flexGrow > 0) {
78
- totalFlexGrow += flexGrow;
79
- }
307
+ const grow = flexGrow !== undefined && flexGrow > 0 ? flexGrow : 0;
308
+ totalFlexGrow += grow;
80
309
 
81
310
  const flexShrink = c.flexShrink;
82
311
  if (flexShrink !== undefined && flexShrink > 0) {
83
312
  totalFlexShrink += flexShrink;
84
313
  }
85
314
 
86
- if (c[minDimension] && (c[dimension] || 0) < c[minDimension]) {
87
- c[dimension] = c[minDimension]!;
315
+ let main = isRow ? w : h;
316
+ let cross = isRow ? h : w;
317
+ const minMain = isRow ? c.minWidth : c.minHeight;
318
+ if (minMain && (main || 0) < minMain) {
319
+ setSize(c as ElementNode, isRow, minMain);
320
+ main = isRow ? c.width : c.height;
321
+ }
322
+ const minCross = isRow ? c.minHeight : c.minWidth;
323
+ if (minCross && (cross || 0) < minCross) {
324
+ setSize(c as ElementNode, !isRow, minCross);
325
+ cross = isRow ? c.height : c.width;
88
326
  }
89
327
 
90
- if (
91
- c[crossMinDimension] &&
92
- (c[crossDimension] || 0) < c[crossMinDimension]
93
- ) {
94
- c[crossDimension] = c[crossMinDimension]!;
328
+ // B8: flexGrow and flexShrink start from the item's own size, not from
329
+ // the size the last pass grew or shrank it to. The record holds both; a
330
+ // size written since (anything but the flexed one) is the new own size.
331
+ // With a transition the read is the animated value, so the record is
332
+ // kept as it is.
333
+ const read = main || 0;
334
+ let own = read;
335
+ let flag = 0;
336
+ const grown = c._flexGrown;
337
+ if (grown !== undefined) {
338
+ if (
339
+ (grow > 0 || (flexShrink !== undefined && flexShrink > 0)) &&
340
+ (read === grown ||
341
+ (isRow
342
+ ? animates(c as ElementNode, 'w', 'width')
343
+ : animates(c as ElementNode, 'h', 'height')))
344
+ ) {
345
+ own = c._flexBase!;
346
+ flag = FROM_RECORD;
347
+ sizeWork = true;
348
+ } else {
349
+ c._flexGrown = undefined;
350
+ }
95
351
  }
96
352
 
97
- processableChildrenIndices.push(i);
353
+ const flexBasis = c.flexBasis;
354
+ const base =
355
+ flexBasis === undefined || flexBasis === 'auto'
356
+ ? own
357
+ : Math.max(flexBasis as number, minMain || 0);
358
+
359
+ // index mappings for margins: Top: 0, Right: 1, Bottom: 2, Left: 3
360
+ const margin = c.margin;
361
+ let mStart: number;
362
+ let mEnd: number;
363
+ let mCrossStart: number;
364
+ let mCrossEnd: number;
365
+ if (isRow) {
366
+ mStart = c.marginLeft || getArrayValue(margin, 3);
367
+ mEnd = c.marginRight || getArrayValue(margin, 1);
368
+ mCrossStart = c.marginTop || getArrayValue(margin, 0);
369
+ mCrossEnd = c.marginBottom || getArrayValue(margin, 2);
370
+ } else {
371
+ mStart = c.marginTop || getArrayValue(margin, 0);
372
+ mEnd = c.marginBottom || getArrayValue(margin, 2);
373
+ mCrossStart = c.marginLeft || getArrayValue(margin, 3);
374
+ mCrossEnd = c.marginRight || getArrayValue(margin, 1);
375
+ }
376
+
377
+ const k = n++;
378
+ childIndex[k] = i;
379
+ flags[k] = flag;
380
+ mainSize[k] = base;
381
+ mainRead[k] = read;
382
+ ownSize[k] = own;
383
+ marginStart[k] = mStart;
384
+ marginEnd[k] = mEnd;
385
+ totalMain[k] = base + mStart + mEnd;
386
+ crossSize[k] = cross || 0;
387
+ marginCrossStart[k] = mCrossStart;
388
+ marginCrossEnd[k] = mCrossEnd;
389
+ growFactor[k] = grow;
390
+ shrinkFactor[k] = flexShrink || 0;
391
+ orderKey[k] = flexOrder || 0;
98
392
  }
99
393
 
100
- if (hasOrder) {
101
- processableChildrenIndices.sort((aIdx, bIdx) => {
102
- const a = children[aIdx] as ElementNode;
103
- const b = children[bIdx] as ElementNode;
104
- return (a.flexOrder || 0) - (b.flexOrder || 0);
105
- });
394
+ if (n === 0) {
395
+ return false;
106
396
  }
107
397
 
398
+ for (let p = 0; p < n; p++) {
399
+ order[p] = p;
400
+ }
401
+ if (hasOrder) {
402
+ // Stable insertion sort by flexOrder (a missing one counts as 0).
403
+ for (let p = 1; p < n; p++) {
404
+ const k = order[p]!;
405
+ const key = orderKey[k]!;
406
+ let q = p - 1;
407
+ while (q >= 0 && orderKey[order[q]!]! > key) {
408
+ order[q + 1] = order[q]!;
409
+ q--;
410
+ }
411
+ order[q + 1] = k;
412
+ }
413
+ }
108
414
  // Apply reverse layout ordering
109
415
  if (isReverse || node.direction === 'rtl') {
110
- processableChildrenIndices.reverse();
111
- }
112
-
113
- const numProcessedChildren = processableChildrenIndices.length;
114
- if (numProcessedChildren === 0) {
115
- return false;
416
+ for (let lo = 0, hi = n - 1; lo < hi; lo++, hi--) {
417
+ const t = order[lo]!;
418
+ order[lo] = order[hi]!;
419
+ order[hi] = t;
420
+ }
116
421
  }
117
422
 
118
- const prop = isRow ? 'x' : 'y';
119
- const crossProp = isRow ? 'y' : 'x';
423
+ const nodeWidth = node.width;
424
+ const nodeHeight = node.height;
425
+ const nodeMain = isRow ? nodeWidth : nodeHeight;
120
426
  const containerSize = Math.max(
121
- node[dimension] || 0,
122
- node[minDimension] || 0,
427
+ nodeMain || 0,
428
+ (isRow ? node.minWidth : node.minHeight) || 0,
123
429
  0,
124
430
  );
125
431
  let containerCrossSize = Math.max(
126
- node[crossDimension] || 0,
127
- node[crossMinDimension] || 0,
432
+ (isRow ? nodeHeight : nodeWidth) || 0,
433
+ (isRow ? node.minHeight : node.minWidth) || 0,
128
434
  0,
129
435
  );
130
- const isWrapReverse = node.flexWrap === 'wrap-reverse';
131
436
  const gap = node.gap || 0;
132
437
  const justify = node.justifyContent || 'flexStart';
133
- const align = node.alignItems || (node.flexWrap ? 'flexStart' : undefined);
134
438
  let containerUpdated = false;
135
439
 
136
- // Resolve sizes matching old processed calculation
137
- const childMainSizes = new Float32Array(numProcessedChildren);
138
- const childMarginStarts = new Float32Array(numProcessedChildren);
139
- const childMarginEnds = new Float32Array(numProcessedChildren);
140
- const childTotalMainSizes = new Float32Array(numProcessedChildren);
141
- const childCrossSizes = new Float32Array(numProcessedChildren);
142
- const childMarginCrossStarts = new Float32Array(numProcessedChildren);
143
- const childMarginCrossEnds = new Float32Array(numProcessedChildren);
144
-
145
440
  let sumOfFlexBaseSizesWithMargins = 0;
146
-
147
- for (let idx = 0; idx < numProcessedChildren; idx++) {
148
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
149
- const marginArray = c.margin;
150
- // index mappings for margins: Top: 0, Right: 1, Bottom: 2, Left: 3
151
- // if row: main = left/right (3/1),
152
- const flexBasis = c.flexBasis;
153
- const isBasisAuto = flexBasis === undefined || flexBasis === 'auto';
154
- const computedBasis = isBasisAuto
155
- ? c[dimension] || 0
156
- : (flexBasis as number);
157
- const baseMainSize = isBasisAuto
158
- ? computedBasis
159
- : Math.max(computedBasis, c[minDimension] || 0);
160
-
161
- const marginStart = isRow
162
- ? c.marginLeft || getArrayValue(marginArray, 3)
163
- : c.marginTop || getArrayValue(marginArray, 0);
164
- const marginEnd = isRow
165
- ? c.marginRight || getArrayValue(marginArray, 1)
166
- : c.marginBottom || getArrayValue(marginArray, 2);
167
- const marginCrossStart = isRow
168
- ? c.marginTop || getArrayValue(marginArray, 0)
169
- : c.marginLeft || getArrayValue(marginArray, 3);
170
- const marginCrossEnd = isRow
171
- ? c.marginBottom || getArrayValue(marginArray, 2)
172
- : c.marginRight || getArrayValue(marginArray, 1);
173
-
174
- childMainSizes[idx] = baseMainSize;
175
- childMarginStarts[idx] = marginStart;
176
- childMarginEnds[idx] = marginEnd;
177
- childTotalMainSizes[idx] = baseMainSize + marginStart + marginEnd;
178
- childCrossSizes[idx] = c[crossDimension] || 0;
179
- childMarginCrossStarts[idx] = marginCrossStart;
180
- childMarginCrossEnds[idx] = marginCrossEnd;
181
-
182
- sumOfFlexBaseSizesWithMargins += childTotalMainSizes[idx]!;
441
+ for (let p = 0; p < n; p++) {
442
+ sumOfFlexBaseSizesWithMargins += totalMain[order[p]!]!;
183
443
  }
184
444
 
185
- if ((totalFlexGrow > 0 || totalFlexShrink > 0) && numProcessedChildren > 1) {
186
- node.flexBoundary = node.flexBoundary || 'fixed';
445
+ if ((totalFlexGrow > 0 || totalFlexShrink > 0) && n > 1) {
446
+ if (!node.flexBoundary) {
447
+ node.flexBoundary = 'fixed';
448
+ }
187
449
 
188
- const totalGapSpace =
189
- numProcessedChildren > 0 ? gap * (numProcessedChildren - 1) : 0;
450
+ // The space inside the padding.
190
451
  const availableSpace =
191
- containerSize - sumOfFlexBaseSizesWithMargins - totalGapSpace;
452
+ containerSize -
453
+ nodePaddingTotal -
454
+ sumOfFlexBaseSizesWithMargins -
455
+ gap * (n - 1);
192
456
 
193
457
  if (availableSpace > 0 && totalFlexGrow > 0) {
194
- for (let idx = 0; idx < numProcessedChildren; idx++) {
195
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
196
- const flexGrowValue = c.flexGrow || 0;
197
- if (flexGrowValue > 0) {
198
- const shareOfSpace = (flexGrowValue / totalFlexGrow) * availableSpace;
199
- const newMainSize = childMainSizes[idx]! + shareOfSpace;
200
- c[dimension] = newMainSize;
201
- childMainSizes[idx] = newMainSize;
202
- childTotalMainSizes[idx] =
203
- newMainSize + childMarginStarts[idx]! + childMarginEnds[idx]!;
458
+ for (let p = 0; p < n; p++) {
459
+ const k = order[p]!;
460
+ const grow = growFactor[k]!;
461
+ if (grow > 0) {
462
+ const newMainSize =
463
+ mainSize[k]! + (grow / totalFlexGrow) * availableSpace;
464
+ mainSize[k] = newMainSize;
465
+ totalMain[k] = newMainSize + marginStart[k]! + marginEnd[k]!;
466
+ flags[k] = flags[k]! | GROWN;
204
467
  }
205
468
  }
206
- node._containsFlexGrow = node._containsFlexGrow ? null : true;
469
+ sizeWork = true;
207
470
  } else if (availableSpace < 0 && totalFlexShrink > 0) {
208
471
  // Flex Shrink Phase
209
472
  let totalScaledShrinkFactor = 0;
210
- for (let idx = 0; idx < numProcessedChildren; idx++) {
211
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
212
- const flexShrinkValue = c.flexShrink || 0;
213
- totalScaledShrinkFactor += flexShrinkValue * childMainSizes[idx]!;
473
+ for (let p = 0; p < n; p++) {
474
+ const k = order[p]!;
475
+ totalScaledShrinkFactor += shrinkFactor[k]! * mainSize[k]!;
214
476
  }
215
477
 
216
478
  if (totalScaledShrinkFactor > 0) {
217
- for (let idx = 0; idx < numProcessedChildren; idx++) {
218
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
219
- const flexShrinkValue = c.flexShrink || 0;
220
- if (flexShrinkValue > 0) {
479
+ for (let p = 0; p < n; p++) {
480
+ const k = order[p]!;
481
+ const shrink = shrinkFactor[k]!;
482
+ if (shrink > 0) {
221
483
  const shrinkRatio =
222
- (flexShrinkValue * childMainSizes[idx]!) /
223
- totalScaledShrinkFactor;
224
- const sizeReduction = shrinkRatio * Math.abs(availableSpace);
225
- let newMainSize = childMainSizes[idx]! - sizeReduction;
484
+ (shrink * mainSize[k]!) / totalScaledShrinkFactor;
485
+ let newMainSize =
486
+ mainSize[k]! - shrinkRatio * Math.abs(availableSpace);
226
487
 
227
488
  // Constrain by min width/height
228
- const minBound = c[minDimension] || 0;
489
+ const c = children[childIndex[k]!] as ElementNode;
490
+ const minBound = (isRow ? c.minWidth : c.minHeight) || 0;
229
491
  if (newMainSize < minBound) {
230
492
  newMainSize = minBound;
231
493
  }
232
494
 
233
- c[dimension] = newMainSize;
234
- childMainSizes[idx] = newMainSize;
235
- childTotalMainSizes[idx] =
236
- newMainSize + childMarginStarts[idx]! + childMarginEnds[idx]!;
495
+ mainSize[k] = newMainSize;
496
+ totalMain[k] = newMainSize + marginStart[k]! + marginEnd[k]!;
497
+ flags[k] = flags[k]! | SHRUNK;
498
+ sizeWork = true;
237
499
  }
238
500
  }
239
501
  }
240
- node._containsFlexGrow = node._containsFlexGrow ? null : true;
241
- } else if (node._containsFlexGrow) {
242
- node._containsFlexGrow = null;
243
502
  }
244
503
  }
245
504
 
505
+ // Whether this pass changed a child's size: updateLayout then lays the
506
+ // flex ones out again (`_containsFlexGrow`). A relayout that changes none
507
+ // asks for nothing, so a grown child that sizes itself to its content is
508
+ // not shrunk and regrown on every relayout.
509
+ let resized = false;
510
+ if (sizeWork) {
511
+ for (let p = 0; p < n; p++) {
512
+ const k = order[p]!;
513
+ const flag = flags[k]!;
514
+ if (flag === 0) {
515
+ continue;
516
+ }
517
+ const c = children[childIndex[k]!] as ElementNode;
518
+ const read = mainRead[k]!;
519
+ if ((flag & (GROWN | SHRUNK)) !== 0) {
520
+ const size = mainSize[k]!;
521
+ if (
522
+ (size !== read || c.transition) &&
523
+ writeSize(c, isRow, read, size)
524
+ ) {
525
+ resized = true;
526
+ }
527
+ const own = ownSize[k]!;
528
+ if (c._flexBase !== own) c._flexBase = own;
529
+ if (c._flexGrown !== size) c._flexGrown = size;
530
+ } else {
531
+ // B8: grown or shrunk by an earlier pass, not by this one: back to
532
+ // its own size.
533
+ const size = ownSize[k]!;
534
+ if (
535
+ (size !== read || c.transition) &&
536
+ writeSize(c, isRow, read, size)
537
+ ) {
538
+ resized = true;
539
+ }
540
+ c._flexGrown = undefined;
541
+ }
542
+ }
543
+ }
544
+ if (resized) {
545
+ node._containsFlexGrow = true;
546
+ } else if (node._containsFlexGrow) {
547
+ node._containsFlexGrow = null;
548
+ }
549
+
246
550
  let totalItemSize = 0;
247
551
  if (
248
552
  justify === 'center' ||
@@ -250,159 +554,207 @@ export default function (node: ElementNode): boolean {
250
554
  justify === 'spaceEvenly' ||
251
555
  justify === 'spaceAround'
252
556
  ) {
253
- for (let idx = 0; idx < numProcessedChildren; idx++) {
254
- totalItemSize += childTotalMainSizes[idx]!;
557
+ for (let p = 0; p < n; p++) {
558
+ totalItemSize += totalMain[order[p]!]!;
255
559
  }
256
560
  }
257
561
 
258
- const doCrossAlign = containerCrossSize
259
- ? (c: ElementNode, idx: number, crossCurrentPos: number = 0) => {
260
- const alignSelf = c.alignSelf || align;
261
- if (!alignSelf) {
262
- return;
263
- }
264
- if (alignSelf === 'flexStart') {
265
- c[crossProp] = crossCurrentPos + childMarginCrossStarts[idx]!;
266
- } else if (alignSelf === 'center') {
267
- c[crossProp] =
268
- crossCurrentPos +
269
- (containerCrossSize - childCrossSizes[idx]!) / 2 +
270
- childMarginCrossStarts[idx]!;
271
- } else if (alignSelf === 'flexEnd') {
272
- c[crossProp] =
273
- crossCurrentPos +
274
- containerCrossSize -
275
- childCrossSizes[idx]! -
276
- childMarginCrossEnds[idx]!;
277
- }
278
- }
279
- : (_c: ElementNode, _idx: number, _crossCurrentPos: number = 0) => {
280
- /* no-op */
281
- };
562
+ // Decided before the row's height is fitted below (as before 1.7).
563
+ const crossAlign = containerCrossSize > 0;
282
564
 
283
565
  if (isRow && node._calcHeight && !node.flexCrossBoundary) {
566
+ // An item alignCross places sits inside the cross padding, so the row
567
+ // fits it with that padding; one it leaves where it is, without.
568
+ const crossPadding = paddingCrossStart + paddingCrossEnd;
284
569
  let maxHeight = 0;
285
- for (let idx = 0; idx < numProcessedChildren; idx++) {
286
- if (childCrossSizes[idx]! > maxHeight) maxHeight = childCrossSizes[idx]!;
570
+ for (let p = 0; p < n; p++) {
571
+ const k = order[p]!;
572
+ let size = crossSize[k]!;
573
+ if (
574
+ size > 0 &&
575
+ crossAlign &&
576
+ (align || (children[childIndex[k]!] as ElementNode).alignSelf)
577
+ ) {
578
+ size += crossPadding;
579
+ }
580
+ if (size > maxHeight) maxHeight = size;
287
581
  }
288
- const newHeight = maxHeight || node.height;
289
- if (newHeight !== node.height) {
582
+ const newHeight = maxHeight || nodeHeight;
583
+ if (
584
+ (newHeight !== nodeHeight || node.transition) &&
585
+ writeSize(node, false, nodeHeight, newHeight)
586
+ ) {
290
587
  containerUpdated = true;
291
- node.height = containerCrossSize = newHeight;
588
+ containerCrossSize = newHeight;
292
589
  }
293
590
  }
294
591
 
592
+ // B7: center and flexEnd align inside the padded box, not the full size.
593
+ const crossBox = containerCrossSize - paddingCrossStart - paddingCrossEnd;
594
+
295
595
  let currentPos = paddingStart;
296
596
  if (justify === 'flexStart') {
297
- if (node.flexWrap === 'wrap') {
298
- const childCrossSizeVar =
299
- numProcessedChildren > 0 ? childCrossSizes[0]! : containerCrossSize;
300
- let crossCurrentPos = isWrapReverse
301
- ? containerCrossSize - paddingCrossEnd - childCrossSizeVar
302
- : paddingCrossStart;
597
+ if (wrapping) {
598
+ const lineSize = crossSize[order[0]!]!;
303
599
  const crossGap = isRow ? (node.columnGap ?? gap) : (node.rowGap ?? gap);
600
+ let line = paddingCrossStart;
304
601
 
305
- for (let idx = 0; idx < numProcessedChildren; idx++) {
306
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
602
+ for (let p = 0; p < n; p++) {
603
+ const k = order[p]!;
307
604
  if (
308
- currentPos + childTotalMainSizes[idx]! > containerSize &&
605
+ currentPos + totalMain[k]! > containerSize &&
309
606
  currentPos > paddingStart
310
607
  ) {
311
608
  currentPos = paddingStart;
312
- crossCurrentPos += isWrapReverse
313
- ? -(childCrossSizeVar + crossGap)
314
- : childCrossSizeVar + crossGap;
609
+ line += lineSize + crossGap;
315
610
  }
316
- c[prop] = currentPos + childMarginStarts[idx]!;
317
- currentPos += childTotalMainSizes[idx]! + gap;
318
- doCrossAlign(c, idx, crossCurrentPos);
611
+ setPos(
612
+ children[childIndex[k]!] as ElementNode,
613
+ isRow,
614
+ currentPos + marginStart[k]!,
615
+ );
616
+ currentPos += totalMain[k]! + gap;
617
+ lineStart[k] = line;
319
618
  }
320
619
 
321
- const finalCrossSize = isWrapReverse
322
- ? containerCrossSize - crossCurrentPos + paddingCrossStart
323
- : crossCurrentPos + childCrossSizeVar + paddingCrossEnd;
620
+ // Each item is aligned from its line's start; center and flexEnd
621
+ // measure against the container's cross size (B7: less its padding),
622
+ // as before 1.7. B9: a container with no cross size places its items
623
+ // too, against the line. B10: wrap-reverse mirrors the lines, so the
624
+ // first one is at the end.
625
+ const alignSize = crossAlign ? crossBox : lineSize;
626
+ for (let p = 0; p < n; p++) {
627
+ const k = order[p]!;
628
+ alignCross(
629
+ children[childIndex[k]!] as ElementNode,
630
+ k,
631
+ isRow,
632
+ align,
633
+ isWrapReverse
634
+ ? line + paddingCrossStart - lineStart[k]!
635
+ : lineStart[k]!,
636
+ alignSize,
637
+ );
638
+ }
324
639
 
325
- if (node[crossDimension] !== finalCrossSize) {
326
- node[`preFlex${crossDimension}`] = node[crossDimension];
327
- node[crossDimension] = finalCrossSize;
328
- containerUpdated = true;
640
+ const finalCrossSize = line + lineSize + paddingCrossEnd;
641
+ if (isRow) {
642
+ const height = node.height;
643
+ if (
644
+ (height !== finalCrossSize || node.transition) &&
645
+ writeSize(node, false, height, finalCrossSize)
646
+ ) {
647
+ node.preFlexheight = height;
648
+ containerUpdated = true;
649
+ }
650
+ } else {
651
+ const width = node.width;
652
+ if (
653
+ (width !== finalCrossSize || node.transition) &&
654
+ writeSize(node, true, width, finalCrossSize)
655
+ ) {
656
+ node.preFlexwidth = width;
657
+ containerUpdated = true;
658
+ }
329
659
  }
330
660
  } else {
331
- for (let idx = 0; idx < numProcessedChildren; idx++) {
332
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
333
- c[prop] = currentPos + childMarginStarts[idx]!;
334
- currentPos += childTotalMainSizes[idx]! + gap;
335
- doCrossAlign(c, idx, paddingCrossStart);
661
+ for (let p = 0; p < n; p++) {
662
+ const k = order[p]!;
663
+ const c = children[childIndex[k]!] as ElementNode;
664
+ setPos(c, isRow, currentPos + marginStart[k]!);
665
+ currentPos += totalMain[k]! + gap;
666
+ if (crossAlign) {
667
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
668
+ }
336
669
  }
337
670
  }
338
671
 
339
672
  // Update container size
340
- if (node.flexBoundary !== 'fixed' && node.flexWrap !== 'wrap') {
673
+ if (node.flexBoundary !== 'fixed' && !wrapping) {
341
674
  let calculatedSize = currentPos - gap + paddingEnd;
342
- const minSize = node[minDimension] || 0;
675
+ const minSize = (isRow ? node.minWidth : node.minHeight) || 0;
343
676
  if (calculatedSize < minSize) {
344
677
  calculatedSize = minSize;
345
678
  }
346
- if (calculatedSize !== (node[dimension] || 0)) {
347
- node[`preFlex${dimension}`] = containerSize;
348
- node[dimension] = calculatedSize;
679
+ const current = nodeMain || 0;
680
+ if (
681
+ (calculatedSize !== current || node.transition) &&
682
+ writeSize(node, isRow, current, calculatedSize)
683
+ ) {
684
+ if (isRow) {
685
+ node.preFlexwidth = containerSize;
686
+ } else {
687
+ node.preFlexheight = containerSize;
688
+ }
349
689
  return true;
350
690
  }
351
691
  }
352
692
  } else if (justify === 'flexEnd') {
353
693
  currentPos = containerSize - paddingEnd;
354
- for (let idx = numProcessedChildren - 1; idx >= 0; idx--) {
355
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
356
- c[prop] = currentPos - childMainSizes[idx]! - childMarginEnds[idx]!;
357
- currentPos -= childTotalMainSizes[idx]! + gap;
358
- doCrossAlign(c, idx, paddingCrossStart);
694
+ for (let p = n - 1; p >= 0; p--) {
695
+ const k = order[p]!;
696
+ const c = children[childIndex[k]!] as ElementNode;
697
+ setPos(c, isRow, currentPos - mainSize[k]! - marginEnd[k]!);
698
+ currentPos -= totalMain[k]! + gap;
699
+ if (crossAlign) {
700
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
701
+ }
359
702
  }
360
703
  } else if (justify === 'center') {
704
+ // B6: centred inside the padded box.
361
705
  currentPos =
362
- (containerSize - (totalItemSize + gap * (numProcessedChildren - 1))) / 2 +
706
+ (containerSize -
707
+ paddingStart -
708
+ paddingEnd -
709
+ (totalItemSize + gap * (n - 1))) /
710
+ 2 +
363
711
  paddingStart;
364
- for (let idx = 0; idx < numProcessedChildren; idx++) {
365
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
366
- c[prop] = currentPos + childMarginStarts[idx]!;
367
- currentPos += childTotalMainSizes[idx]! + gap;
368
- doCrossAlign(c, idx, paddingCrossStart);
712
+ for (let p = 0; p < n; p++) {
713
+ const k = order[p]!;
714
+ const c = children[childIndex[k]!] as ElementNode;
715
+ setPos(c, isRow, currentPos + marginStart[k]!);
716
+ currentPos += totalMain[k]! + gap;
717
+ if (crossAlign) {
718
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
719
+ }
369
720
  }
370
721
  } else if (justify === 'spaceBetween') {
371
722
  const spaceBetween =
372
- numProcessedChildren > 1
373
- ? (containerSize - totalItemSize - nodePaddingTotal) /
374
- (numProcessedChildren - 1)
375
- : 0;
723
+ n > 1 ? (containerSize - totalItemSize - nodePaddingTotal) / (n - 1) : 0;
376
724
  currentPos = paddingStart;
377
- for (let idx = 0; idx < numProcessedChildren; idx++) {
378
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
379
- c[prop] = currentPos + childMarginStarts[idx]!;
380
- currentPos += childTotalMainSizes[idx]! + spaceBetween;
381
- doCrossAlign(c, idx, paddingCrossStart);
725
+ for (let p = 0; p < n; p++) {
726
+ const k = order[p]!;
727
+ const c = children[childIndex[k]!] as ElementNode;
728
+ setPos(c, isRow, currentPos + marginStart[k]!);
729
+ currentPos += totalMain[k]! + spaceBetween;
730
+ if (crossAlign) {
731
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
732
+ }
382
733
  }
383
734
  } else if (justify === 'spaceAround') {
384
- const spaceAround =
385
- numProcessedChildren > 0
386
- ? (containerSize - totalItemSize - nodePaddingTotal) /
387
- numProcessedChildren
388
- : 0;
735
+ const spaceAround = (containerSize - totalItemSize - nodePaddingTotal) / n;
389
736
  currentPos = paddingStart + spaceAround / 2;
390
- for (let idx = 0; idx < numProcessedChildren; idx++) {
391
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
392
- c[prop] = currentPos + childMarginStarts[idx]!;
393
- currentPos += childTotalMainSizes[idx]! + spaceAround;
394
- doCrossAlign(c, idx, paddingCrossStart);
737
+ for (let p = 0; p < n; p++) {
738
+ const k = order[p]!;
739
+ const c = children[childIndex[k]!] as ElementNode;
740
+ setPos(c, isRow, currentPos + marginStart[k]!);
741
+ currentPos += totalMain[k]! + spaceAround;
742
+ if (crossAlign) {
743
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
744
+ }
395
745
  }
396
746
  } else if (justify === 'spaceEvenly') {
397
747
  const spaceEvenly =
398
- (containerSize - totalItemSize - nodePaddingTotal) /
399
- (numProcessedChildren + 1);
748
+ (containerSize - totalItemSize - nodePaddingTotal) / (n + 1);
400
749
  currentPos = spaceEvenly + paddingStart;
401
- for (let idx = 0; idx < numProcessedChildren; idx++) {
402
- const c = children[processableChildrenIndices[idx]!] as ElementNode;
403
- c[prop] = currentPos + childMarginStarts[idx]!;
404
- currentPos += childTotalMainSizes[idx]! + spaceEvenly;
405
- doCrossAlign(c, idx, paddingCrossStart);
750
+ for (let p = 0; p < n; p++) {
751
+ const k = order[p]!;
752
+ const c = children[childIndex[k]!] as ElementNode;
753
+ setPos(c, isRow, currentPos + marginStart[k]!);
754
+ currentPos += totalMain[k]! + spaceEvenly;
755
+ if (crossAlign) {
756
+ alignCross(c, k, isRow, align, paddingCrossStart, crossBox);
757
+ }
406
758
  }
407
759
  }
408
760