@holtjs/stage 0.0.0-stage → 0.3.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/dist/flex.js ADDED
@@ -0,0 +1,426 @@
1
+ // src/flex.ts
2
+ /** Normalize uniform, [v, h], or [top, right, bottom, left] padding into `[top, right, bottom, left]`. */
3
+ function resolvePadding(padding) {
4
+ if (padding == null) return [
5
+ 0,
6
+ 0,
7
+ 0,
8
+ 0
9
+ ];
10
+ if (typeof padding === "number") {
11
+ const p = Number.isFinite(padding) ? padding : 0;
12
+ return [
13
+ p,
14
+ p,
15
+ p,
16
+ p
17
+ ];
18
+ }
19
+ if (padding.length === 2) {
20
+ const v = Number.isFinite(padding[0]) ? padding[0] : 0;
21
+ const h = Number.isFinite(padding[1]) ? padding[1] : 0;
22
+ return [
23
+ v,
24
+ h,
25
+ v,
26
+ h
27
+ ];
28
+ }
29
+ return [
30
+ Number.isFinite(padding[0]) ? padding[0] : 0,
31
+ Number.isFinite(padding[1]) ? padding[1] : 0,
32
+ Number.isFinite(padding[2]) ? padding[2] : 0,
33
+ Number.isFinite(padding[3]) ? padding[3] : 0
34
+ ];
35
+ }
36
+ function clampSize(val, minVal, maxVal) {
37
+ let out = Number.isFinite(val) ? Math.max(0, val) : 0;
38
+ if (minVal !== void 0 && Number.isFinite(minVal)) out = Math.max(Math.max(0, minVal), out);
39
+ if (maxVal !== void 0 && Number.isFinite(maxVal)) out = Math.min(Math.max(0, maxVal), out);
40
+ return out;
41
+ }
42
+ /**
43
+ * Solve a single-axis flex container (`row` or `col`) with optional grow/shrink, min/max clamping,
44
+ * cross-axis alignment/stretch, and main-axis justification.
45
+ */
46
+ function computeFlexLayout(container, items) {
47
+ const isRow = (container.dir ?? "row") === "row";
48
+ const gap = Math.max(0, Number.isFinite(container.gap) ? container.gap ?? 0 : 0);
49
+ const [padT, padR, padB, padL] = resolvePadding(container.padding);
50
+ const align = container.align ?? "start";
51
+ const justify = container.justify ?? "start";
52
+ const snap = container.round !== false ? Math.round : (v) => v;
53
+ const fixedW = container.w != null && Number.isFinite(container.w) ? Math.max(0, container.w) : null;
54
+ const fixedH = container.h != null && Number.isFinite(container.h) ? Math.max(0, container.h) : null;
55
+ const fixedMain = isRow ? fixedW : fixedH;
56
+ const fixedCross = isRow ? fixedH : fixedW;
57
+ const padMainStart = isRow ? padL : padT;
58
+ const padMainEnd = isRow ? padR : padB;
59
+ const padCrossStart = isRow ? padT : padL;
60
+ const padCrossEnd = isRow ? padB : padR;
61
+ const rects = new Array(items.length);
62
+ const activeIdx = [];
63
+ for (let i = 0; i < items.length; i++) if (items[i].visible === false) rects[i] = {
64
+ x: 0,
65
+ y: 0,
66
+ w: 0,
67
+ h: 0,
68
+ visible: false
69
+ };
70
+ else activeIdx.push(i);
71
+ const count = activeIdx.length;
72
+ if (count === 0) return {
73
+ w: snap(fixedW ?? padL + padR),
74
+ h: snap(fixedH ?? padT + padB),
75
+ contentW: 0,
76
+ contentH: 0,
77
+ rects
78
+ };
79
+ const innerMain = fixedMain != null ? Math.max(0, fixedMain - padMainStart - padMainEnd) : null;
80
+ const innerCross = fixedCross != null ? Math.max(0, fixedCross - padCrossStart - padCrossEnd) : null;
81
+ const gapTotal = gap * (count - 1);
82
+ const mainSizes = new Float64Array(count);
83
+ const minMains = new Float64Array(count);
84
+ const maxMains = new Float64Array(count);
85
+ const weights = new Float64Array(count);
86
+ const frozen = new Uint8Array(count);
87
+ let baseMainSum = 0;
88
+ let naturalCrossMax = 0;
89
+ for (let k = 0; k < count; k++) {
90
+ const it = items[activeIdx[k]];
91
+ const minM = isRow ? it.minW ?? 0 : it.minH ?? 0;
92
+ const maxM = isRow ? it.maxW ?? 1e400 : it.maxH ?? 1e400;
93
+ const base = clampSize(it.basis != null && Number.isFinite(it.basis) ? it.basis : isRow ? it.w : it.h, minM, maxM);
94
+ mainSizes[k] = base;
95
+ minMains[k] = Math.max(0, minM);
96
+ maxMains[k] = maxM;
97
+ baseMainSum += base;
98
+ const clampedCross = clampSize(isRow ? it.h : it.w, isRow ? it.minH : it.minW, isRow ? it.maxH : it.maxW);
99
+ if (clampedCross > naturalCrossMax) naturalCrossMax = clampedCross;
100
+ }
101
+ if (innerMain != null) {
102
+ const initialFree = innerMain - (baseMainSum + gapTotal);
103
+ if (initialFree > 1e-6) {
104
+ let unfrozen = 0;
105
+ for (let k = 0; k < count; k++) {
106
+ const g = items[activeIdx[k]].grow ?? 0;
107
+ if (g > 0 && mainSizes[k] < maxMains[k]) {
108
+ weights[k] = g;
109
+ unfrozen++;
110
+ } else frozen[k] = 1;
111
+ }
112
+ let remaining = initialFree;
113
+ for (let pass = 0; pass < count && unfrozen > 0 && remaining > 1e-6; pass++) {
114
+ let weightSum = 0;
115
+ for (let k = 0; k < count; k++) if (!frozen[k]) weightSum += weights[k];
116
+ if (weightSum <= 0) break;
117
+ let clampedAny = false;
118
+ let consumed = 0;
119
+ for (let k = 0; k < count; k++) {
120
+ if (frozen[k]) continue;
121
+ const share = remaining * weights[k] / weightSum;
122
+ if (mainSizes[k] + share >= maxMains[k]) {
123
+ consumed += maxMains[k] - mainSizes[k];
124
+ mainSizes[k] = maxMains[k];
125
+ frozen[k] = 1;
126
+ unfrozen--;
127
+ clampedAny = true;
128
+ }
129
+ }
130
+ if (clampedAny) remaining = Math.max(0, remaining - consumed);
131
+ else {
132
+ for (let k = 0; k < count; k++) if (!frozen[k]) mainSizes[k] += remaining * weights[k] / weightSum;
133
+ remaining = 0;
134
+ break;
135
+ }
136
+ }
137
+ } else if (initialFree < -1e-6) {
138
+ let unfrozen = 0;
139
+ for (let k = 0; k < count; k++) {
140
+ const s = items[activeIdx[k]].shrink ?? 0;
141
+ if (s > 0 && mainSizes[k] > minMains[k]) {
142
+ weights[k] = s * Math.max(1, mainSizes[k]);
143
+ unfrozen++;
144
+ } else frozen[k] = 1;
145
+ }
146
+ let deficit = -initialFree;
147
+ for (let pass = 0; pass < count && unfrozen > 0 && deficit > 1e-6; pass++) {
148
+ let weightSum = 0;
149
+ for (let k = 0; k < count; k++) if (!frozen[k]) weightSum += weights[k];
150
+ if (weightSum <= 0) break;
151
+ let clampedAny = false;
152
+ let reclaimed = 0;
153
+ for (let k = 0; k < count; k++) {
154
+ if (frozen[k]) continue;
155
+ const cut = deficit * weights[k] / weightSum;
156
+ if (mainSizes[k] - cut <= minMains[k]) {
157
+ reclaimed += mainSizes[k] - minMains[k];
158
+ mainSizes[k] = minMains[k];
159
+ frozen[k] = 1;
160
+ unfrozen--;
161
+ clampedAny = true;
162
+ }
163
+ }
164
+ if (clampedAny) deficit = Math.max(0, deficit - reclaimed);
165
+ else {
166
+ for (let k = 0; k < count; k++) if (!frozen[k]) mainSizes[k] = Math.max(minMains[k], mainSizes[k] - deficit * weights[k] / weightSum);
167
+ deficit = 0;
168
+ break;
169
+ }
170
+ }
171
+ }
172
+ }
173
+ let usedMain = gapTotal;
174
+ for (let k = 0; k < count; k++) usedMain += mainSizes[k];
175
+ const resolvedInnerMain = innerMain ?? usedMain;
176
+ const resolvedInnerCross = innerCross ?? naturalCrossMax;
177
+ const surplusMain = Math.max(0, resolvedInnerMain - usedMain);
178
+ let mainCursor = padMainStart;
179
+ let stepGap = gap;
180
+ if (surplusMain > 0) {
181
+ if (justify === "center") mainCursor += surplusMain * .5;
182
+ else if (justify === "end") mainCursor += surplusMain;
183
+ else if (justify === "between" && count > 1) stepGap = gap + surplusMain / (count - 1);
184
+ else if (justify === "around") {
185
+ const slot = surplusMain / count;
186
+ mainCursor += slot * .5;
187
+ stepGap = gap + slot;
188
+ } else if (justify === "evenly") {
189
+ const slot = surplusMain / (count + 1);
190
+ mainCursor += slot;
191
+ stepGap = gap + slot;
192
+ }
193
+ }
194
+ for (let k = 0; k < count; k++) {
195
+ const idx = activeIdx[k];
196
+ const it = items[idx];
197
+ const itemAlign = it.alignSelf ?? align;
198
+ const minC = isRow ? it.minH : it.minW;
199
+ const maxC = isRow ? it.maxH : it.maxW;
200
+ const rawC = isRow ? it.h : it.w;
201
+ const crossSize = itemAlign === "stretch" ? clampSize(resolvedInnerCross, minC, maxC) : clampSize(rawC, minC, maxC);
202
+ let crossPos = padCrossStart;
203
+ if (itemAlign === "center") crossPos += (resolvedInnerCross - crossSize) * .5;
204
+ else if (itemAlign === "end") crossPos += resolvedInnerCross - crossSize;
205
+ const mPos = mainCursor;
206
+ const mSize = mainSizes[k];
207
+ mainCursor += mSize + stepGap;
208
+ const mStart = snap(mPos);
209
+ const mLen = Math.max(0, snap(mPos + mSize) - mStart);
210
+ const cStart = snap(crossPos);
211
+ const cLen = Math.max(0, snap(crossPos + crossSize) - cStart);
212
+ if (isRow) rects[idx] = {
213
+ x: mStart,
214
+ y: cStart,
215
+ w: mLen,
216
+ h: cLen,
217
+ visible: true
218
+ };
219
+ else rects[idx] = {
220
+ x: cStart,
221
+ y: mStart,
222
+ w: cLen,
223
+ h: mLen,
224
+ visible: true
225
+ };
226
+ }
227
+ const contentW = isRow ? usedMain : naturalCrossMax;
228
+ const contentH = isRow ? naturalCrossMax : usedMain;
229
+ const outW = fixedW ?? contentW + padL + padR;
230
+ const outH = fixedH ?? contentH + padT + padB;
231
+ return {
232
+ w: snap(outW),
233
+ h: snap(outH),
234
+ contentW: snap(contentW),
235
+ contentH: snap(contentH),
236
+ rects
237
+ };
238
+ }
239
+ /**
240
+ * Solve a multi-line wrapping flow (`columns` omitted) or fixed-column grid (`columns >= 1`)
241
+ * with crisp integer pixel snapping and `(row, col)` coordinates for 2D keyboard navigation.
242
+ */
243
+ function computeWrapLayout(container, items) {
244
+ const baseGap = Math.max(0, Number.isFinite(container.gap) ? container.gap ?? 0 : 0);
245
+ const colGap = Math.max(0, Number.isFinite(container.colGap) ? container.colGap ?? baseGap : baseGap);
246
+ const rowGap = Math.max(0, Number.isFinite(container.rowGap) ? container.rowGap ?? baseGap : baseGap);
247
+ const [padT, padR, padB, padL] = resolvePadding(container.padding);
248
+ const align = container.align ?? "start";
249
+ const justify = container.justify ?? "start";
250
+ const cellAlign = container.cellAlign ?? "stretch";
251
+ const snap = container.round !== false ? Math.round : (v) => v;
252
+ const fixedW = container.w != null && Number.isFinite(container.w) ? Math.max(0, container.w) : null;
253
+ const fixedH = container.h != null && Number.isFinite(container.h) ? Math.max(0, container.h) : null;
254
+ const cols = container.columns != null && Number.isFinite(container.columns) && container.columns >= 1 ? Math.floor(container.columns) : null;
255
+ const cellW = container.cellW != null && Number.isFinite(container.cellW) ? Math.max(0, container.cellW) : null;
256
+ const cellH = container.cellH != null && Number.isFinite(container.cellH) ? Math.max(0, container.cellH) : null;
257
+ const innerW = fixedW != null ? Math.max(0, fixedW - padL - padR) : null;
258
+ const rects = new Array(items.length);
259
+ const activeIdx = [];
260
+ for (let i = 0; i < items.length; i++) if (items[i].visible === false) rects[i] = {
261
+ x: 0,
262
+ y: 0,
263
+ w: 0,
264
+ h: 0,
265
+ row: -1,
266
+ col: -1,
267
+ visible: false
268
+ };
269
+ else activeIdx.push(i);
270
+ if (activeIdx.length === 0) return {
271
+ w: snap(fixedW ?? padL + padR),
272
+ h: snap(fixedH ?? padT + padB),
273
+ contentW: 0,
274
+ contentH: 0,
275
+ rows: 0,
276
+ colsPerRow: [],
277
+ rects
278
+ };
279
+ const rows = [];
280
+ let rowWidths = [];
281
+ let rowHeights = [];
282
+ if (cols !== null) {
283
+ let unitW = cellW;
284
+ if (unitW == null) {
285
+ if (innerW != null) unitW = Math.max(0, (innerW - colGap * (cols - 1)) / cols);
286
+ else {
287
+ let maxUnit = 0;
288
+ for (const idx of activeIdx) {
289
+ const it = items[idx];
290
+ const span = it.fullRow ? cols : Math.max(1, Math.min(cols, Math.floor(it.colSpan ?? 1)));
291
+ const candidate = (Math.max(0, Number.isFinite(it.w) ? it.w : 0) - colGap * (span - 1)) / span;
292
+ if (candidate > maxUnit) maxUnit = candidate;
293
+ }
294
+ unitW = maxUnit;
295
+ }
296
+ }
297
+ let curRow = [];
298
+ let colCursor = 0;
299
+ const flushGridRow = () => {
300
+ if (curRow.length === 0) return;
301
+ let maxH = cellH ?? 0;
302
+ let usedW = 0;
303
+ for (let k = 0; k < curRow.length; k++) {
304
+ usedW += curRow[k].slotW + (k > 0 ? colGap : 0);
305
+ if (curRow[k].h > maxH) maxH = curRow[k].h;
306
+ }
307
+ rows.push(curRow);
308
+ rowWidths.push(usedW);
309
+ rowHeights.push(maxH);
310
+ curRow = [];
311
+ colCursor = 0;
312
+ };
313
+ for (const idx of activeIdx) {
314
+ const it = items[idx];
315
+ const span = it.fullRow ? cols : Math.max(1, Math.min(cols, Math.floor(it.colSpan ?? 1)));
316
+ if (curRow.length > 0 && (it.fullRow || colCursor + span > cols)) flushGridRow();
317
+ const slotW = Math.max(0, unitW * span + colGap * (span - 1));
318
+ const natW = Math.max(0, Number.isFinite(it.w) ? it.w : 0);
319
+ const w = cellAlign === "stretch" || cellW != null ? slotW : Math.min(slotW, natW);
320
+ const h = cellH ?? Math.max(0, Number.isFinite(it.h) ? it.h : 0);
321
+ curRow.push({
322
+ idx,
323
+ col: colCursor,
324
+ w,
325
+ h,
326
+ slotW
327
+ });
328
+ colCursor += span;
329
+ if (it.fullRow || colCursor >= cols) flushGridRow();
330
+ }
331
+ flushGridRow();
332
+ } else {
333
+ let curRow = [];
334
+ let curW = 0;
335
+ let curH = 0;
336
+ const flushFlowRow = () => {
337
+ if (curRow.length === 0) return;
338
+ rows.push(curRow);
339
+ rowWidths.push(curW);
340
+ rowHeights.push(curH);
341
+ curRow = [];
342
+ curW = 0;
343
+ curH = 0;
344
+ };
345
+ for (const idx of activeIdx) {
346
+ const it = items[idx];
347
+ const itemW = it.fullRow && innerW != null ? innerW : cellW ?? Math.max(0, Number.isFinite(it.w) ? it.w : 0);
348
+ const itemH = cellH ?? Math.max(0, Number.isFinite(it.h) ? it.h : 0);
349
+ const nextW = curRow.length === 0 ? itemW : curW + colGap + itemW;
350
+ if (curRow.length > 0 && (it.fullRow || innerW != null && nextW > innerW + 1e-6)) flushFlowRow();
351
+ curRow.push({
352
+ idx,
353
+ col: curRow.length,
354
+ w: itemW,
355
+ h: itemH,
356
+ slotW: itemW
357
+ });
358
+ curW = curRow.length === 1 ? itemW : curW + colGap + itemW;
359
+ if (itemH > curH) curH = itemH;
360
+ if (it.fullRow) flushFlowRow();
361
+ }
362
+ flushFlowRow();
363
+ }
364
+ let contentW = 0;
365
+ let contentH = 0;
366
+ for (let r = 0; r < rows.length; r++) {
367
+ if (rowWidths[r] > contentW) contentW = rowWidths[r];
368
+ contentH += rowHeights[r] + (r > 0 ? rowGap : 0);
369
+ }
370
+ const resolvedInnerW = innerW ?? contentW;
371
+ const colsPerRow = new Array(rows.length);
372
+ let yCursor = padT;
373
+ for (let r = 0; r < rows.length; r++) {
374
+ const rowEntries = rows[r];
375
+ const rowW = rowWidths[r];
376
+ const rowH = rowHeights[r];
377
+ colsPerRow[r] = rowEntries.length;
378
+ const surplus = Math.max(0, resolvedInnerW - rowW);
379
+ let xCursor = padL;
380
+ let stepGap = colGap;
381
+ if (surplus > 0) {
382
+ if (justify === "center") xCursor += surplus * .5;
383
+ else if (justify === "end") xCursor += surplus;
384
+ else if (justify === "between" && rowEntries.length > 1) stepGap = colGap + surplus / (rowEntries.length - 1);
385
+ }
386
+ for (let k = 0; k < rowEntries.length; k++) {
387
+ const entry = rowEntries[k];
388
+ const itemH = align === "stretch" ? rowH : entry.h;
389
+ let itemY = yCursor;
390
+ if (align === "center") itemY += (rowH - itemH) * .5;
391
+ else if (align === "end") itemY += rowH - itemH;
392
+ let itemX = xCursor;
393
+ if (entry.slotW > entry.w) {
394
+ if (cellAlign === "center") itemX += (entry.slotW - entry.w) * .5;
395
+ else if (cellAlign === "end") itemX += entry.slotW - entry.w;
396
+ }
397
+ const x0 = snap(itemX);
398
+ const y0 = snap(itemY);
399
+ const w0 = Math.max(0, snap(itemX + entry.w) - x0);
400
+ const h0 = Math.max(0, snap(itemY + itemH) - y0);
401
+ rects[entry.idx] = {
402
+ x: x0,
403
+ y: y0,
404
+ w: w0,
405
+ h: h0,
406
+ row: r,
407
+ col: k,
408
+ visible: true
409
+ };
410
+ xCursor += entry.slotW + stepGap;
411
+ }
412
+ yCursor += rowH + rowGap;
413
+ }
414
+ const outW = fixedW ?? contentW + padL + padR;
415
+ const outH = fixedH ?? contentH + padT + padB;
416
+ return {
417
+ w: snap(outW),
418
+ h: snap(outH),
419
+ contentW: snap(contentW),
420
+ contentH: snap(contentH),
421
+ rows: rows.length,
422
+ colsPerRow,
423
+ rects
424
+ };
425
+ }
426
+ export { computeFlexLayout, computeWrapLayout, resolvePadding };