xonecas.io
70160
Ce

cel-tui / commit 9e3dfab

fix(core): correct cross-axis intrinsic sizing for nested containers

intrinsicMainSize() was summing children along the container's own main
axis regardless of the requested axis. When a VStack asked an HStack
child for its height, it got the sum of children's widths instead of
the max of children's heights — causing massive layout overflow.

Split the container branch into two cases:
- Main axis (requested matches container direction): sum children + gaps
- Cross axis (requested differs): max of children on that axis

Also updates demo.gif from new screencast.

xonecas2026-04-03 9e3dfab899e95cdcee0d6a0420fd69b4df3ad4d6 parent 6982c8f

3 files changed

assets/demo.gif+0−0
Binary file, not shown.
packages/core/src/layout.test.ts+75−0
@@ -278,6 +278,81 @@ describe("layout", () => {
278278 });
279279 });
280280
281+ describe("cross-axis intrinsic sizing", () => {
282+ test("HStack inside VStack uses max child height, not sum of widths", () => {
283+ // HStack with 3 Text children: each is 1 line tall.
284+ // HStack's intrinsic height should be 1 (max), not sum of widths.
285+ const node = VStack({ width: 80, height: 24 }, [
286+ HStack({}, [Text("hello"), Text("world")]),
287+ Text("below"),
288+ ]);
289+ const result = layout(node, 80, 24);
290+ const rects = childRects(result);
291+ // HStack height = 1 (tallest child is a single-line Text)
292+ expect(rects[0]!.height).toBe(1);
293+ // "below" Text starts right after
294+ expect(rects[1]!.y).toBe(1);
295+ expect(rects[1]!.height).toBe(1);
296+ });
297+
298+ test("HStack intrinsic height with multi-line text child", () => {
299+ const node = VStack({ width: 80, height: 24 }, [
300+ HStack({}, [
301+ Text("a"),
302+ Text("b\nc\nd"), // 3 lines tall
303+ ]),
304+ ]);
305+ const result = layout(node, 80, 24);
306+ // HStack height = max child height = 3
307+ expect(childRects(result)[0]!.height).toBe(3);
308+ });
309+
310+ test("VStack inside HStack uses max child width, not sum of heights", () => {
311+ const node = HStack({ width: 80, height: 24 }, [
312+ VStack({}, [VStack({ height: 3 }, []), VStack({ height: 5 }, [])]),
313+ VStack({ width: 10 }, []),
314+ ]);
315+ const result = layout(node, 80, 24);
316+ const rects = childRects(result);
317+ // First VStack intrinsic width should fill parent (80), not 3+5=8
318+ // Actually with no explicit width children, innerCross is used.
319+ // The second child starts after the first.
320+ // VStack intrinsic width = max of children's widths on cross axis
321+ // Its children have no explicit width, so they'd fill available.
322+ // Let's just check that it doesn't sum heights as width.
323+ expect(rects[0]!.width).not.toBe(8);
324+ });
325+
326+ test("hello example pattern: VStack with HStack children stacks tightly", () => {
327+ // Mimics the hello.ts layout pattern that was broken.
328+ // Key assertion: HStack children get height=1 (max child height),
329+ // not the sum of children widths.
330+ const node = VStack({ width: 80 }, [
331+ Text("logo line"),
332+ Text(""),
333+ HStack({ gap: 2 }, [Text("item1"), Text("item2"), Text("item3")]),
334+ Text(""),
335+ HStack({ gap: 1 }, [Text("a"), Text("b")]),
336+ ]);
337+ const result = layout(node, 80, 24);
338+ const rects = childRects(result);
339+ // All children should be height 1
340+ expect(rects[0]!.height).toBe(1); // Text
341+ expect(rects[1]!.height).toBe(1); // Text("")
342+ expect(rects[2]!.height).toBe(1); // HStack (was broken: summed widths)
343+ expect(rects[3]!.height).toBe(1); // Text("")
344+ expect(rects[4]!.height).toBe(1); // HStack
345+ // Children stack sequentially with no gaps
346+ expect(rects[0]!.y).toBe(0);
347+ expect(rects[1]!.y).toBe(1);
348+ expect(rects[2]!.y).toBe(2);
349+ expect(rects[3]!.y).toBe(3);
350+ expect(rects[4]!.y).toBe(4);
351+ // Total intrinsic height = 5
352+ expect(result.rect.height).toBe(5);
353+ });
354+ });
355+
281356 describe("nested layout", () => {
282357 test("editor-like layout: sidebar + main area", () => {
283358 const node = HStack({ width: 80, height: 24 }, [
packages/core/src/layout.ts+38−14
@@ -94,9 +94,14 @@ function intrinsicMainSize(
9494 return 0;
9595 }
9696
97- // Container: sum children along main axis + gaps
97+ // Container: compute intrinsic size along the requested axis.
98+ // If the requested axis matches the container's main axis, sum children + gaps.
99+ // If it's the cross axis, take the max of children on that axis.
98100 const props = node.props;
99101 const gap = props.gap ?? 0;
102+ const containerIsVertical = node.type === "vstack";
103+ const axisMatchesMain = isVertical === containerIsVertical;
104+
100105 const padMain = isVertical
101106 ? (props.padding?.y ?? 0) * 2
102107 : (props.padding?.x ?? 0) * 2;
@@ -104,29 +109,48 @@ function intrinsicMainSize(
104109 ? (props.padding?.x ?? 0) * 2
105110 : (props.padding?.y ?? 0) * 2;
106111 const innerCross = Math.max(0, crossSize - padCross);
107- const childIsVertical = node.type === "vstack";
108112
109- let total = 0;
110- for (let i = 0; i < node.children.length; i++) {
111- const child = node.children[i]!;
113+ if (axisMatchesMain) {
114+ // Sum children along the main axis + gaps
115+ let total = 0;
116+ for (let i = 0; i < node.children.length; i++) {
117+ const child = node.children[i]!;
118+ const cProps = getProps(child);
119+
120+ let childMain: number;
121+ if (isVertical) {
122+ childMain =
123+ resolveSizeValue(cProps?.height, 0) ??
124+ intrinsicMainSize(child, true, innerCross);
125+ } else {
126+ childMain =
127+ resolveSizeValue(cProps?.width, 0) ??
128+ intrinsicMainSize(child, false, innerCross);
129+ }
130+ total += childMain;
131+ if (i < node.children.length - 1) total += gap;
132+ }
133+ return total + padMain;
134+ }
135+
136+ // Cross axis: max of children on the requested axis
137+ let maxSize = 0;
138+ for (const child of node.children) {
112139 const cProps = getProps(child);
113140
114- // Use child's explicit main size, or recurse for intrinsic
115- let childMain: number;
116- if (childIsVertical) {
117- childMain =
141+ let childSize: number;
142+ if (isVertical) {
143+ childSize =
118144 resolveSizeValue(cProps?.height, 0) ??
119145 intrinsicMainSize(child, true, innerCross);
120146 } else {
121- childMain =
147+ childSize =
122148 resolveSizeValue(cProps?.width, 0) ??
123149 intrinsicMainSize(child, false, innerCross);
124150 }
125- total += childMain;
126- if (i < node.children.length - 1) total += gap;
151+ if (childSize > maxSize) maxSize = childSize;
127152 }
128-
129- return total + padMain;
153+ return maxSize + padMain;
130154 }
131155
132156 // --- Largest remainder rounding ---