xonecas.io
70160
Ce

cel-tui / commit 43a6f24

fix: resolve TODO regressions

xonecas2026-04-11 43a6f24794e212de47d704b0c978e13a150b3764 parent ea2bbed

13 files changed

TODO.md+0โˆ’14
@@ -10,16 +10,6 @@ Remaining work, known bugs, and planned improvements.
1010
1111 ---
1212
13-## Bugs / Spec Violations
14-
15-- ๐Ÿ”ง **Scrollbar thumb position ignores padding** โ€” `paintScrollbar` in `paint.ts` computes `maxOffset = contentHeight - rect.height` without accounting for container padding, while the scroll clamping logic uses `contentHeight + padY - rect.height`. For padded scrollable containers the thumb position is slightly off.
16-
17-- ๐Ÿ”ง **Key events leak through layers** โ€” When no element is focused and the topmost layer's root has no `onKeyPress`, `handleKeyEvent` falls through to lower layers. Mouse input stops at the topmost layer with a node at the event position, but keyboard input doesn't follow the same rule. A modal overlay without its own root `onKeyPress` would leak keys to the base layer, breaking the "events target the topmost layer" principle.
18-
19-- ๐Ÿ”ง **`space-between` + `gap` double-spaces children** โ€” When both `justifyContent: "space-between"` and `gap` are set, children get `gap` spacing plus the distributed remaining space on top. The remaining-space calculation subtracts `totalGap` first, then `space-between` distributes what's left as extra gap โ€” so the effective gap is `gap + betweenGaps[i]`. CSS flex doesn't stack `space-between` on top of `gap` this way. The spec doesn't define this interaction.
20-
21----
22-
2313 ## API Improvements
2414
2515 - โŒ **Focus callbacks need reason metadata** โ€” `onFocus`/`onBlur` currently expose only that focus changed, not why. Controlled-focus apps can't distinguish blur caused by `Escape` from blur caused by Tab traversal, mouse clicks, overlay changes, or other focus transitions, which forces awkward workarounds for behaviors like "Escape blurs first, second Escape triggers a global action".
@@ -32,10 +22,6 @@ Remaining work, known bugs, and planned improvements.
3222
3323 - โŒ **Markdown heading inline styling** โ€” Headings (`#`, `##`, `###`) still strip inline formatting to plain text. Since headings are short and single-line, this is low priority. Paragraphs, list items, and blockquotes now render inline formatting via wrapping HStack.
3424
35-## Toolchain
36-
37-- ๐Ÿ”ง **Biome formatter clashes with Prettier on generic type arguments** โ€” When a chained `.method<TypeArgs>(longString)` call exceeds `lineWidth`, biome's formatter breaks at the function args `(` while prettier breaks at the type args `<`. They never converge. Current config has biome formatter enabled (`indentStyle: "space"`), which means both formatters fight on this pattern. Fix: either disable biome's formatter (set `formatter.enabled: false` โ€” `organizeImports` still works as an err โ€ฆ
38-
3925 ## Future Enhancements
4026
4127 - ๐Ÿ’ก **Cursor style/shape customization** โ€” TextInput currently renders the cursor as an inverted cell (block cursor). There's no way to choose bar (`โ”‚` between characters), underline, or custom cursor characters. A `cursorStyle` prop on TextInput (`"block" | "bar" | "underline"`) would cover the common cases. The native terminal cursor shape could also be set via DECSCUSR (`CSI q`) sequences to match.
biome.json+1โˆ’1
@@ -8,7 +8,7 @@
88 }
99 },
1010 "formatter": {
11- "indentStyle": "space"
11+ "enabled": false
1212 },
1313 "linter": {
1414 "enabled": true,
examples/hello.ts+9โˆ’11
@@ -10,6 +10,7 @@
1010
1111 import { cel, VStack, HStack, Text, ProcessTerminal } from "@cel-tui/core";
1212 import { Spacer } from "@cel-tui/components";
13+import { warningBox } from "./warning-box";
1314
1415 const logo = [
1516 "โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ",
@@ -66,17 +67,14 @@ cel.viewport(() => {
6667 },
6768 },
6869 [
69- Text("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”", { fgColor: "color03" }),
70- Text("โ”‚ Terminal too small :( โ”‚", { fgColor: "color03" }),
71- Text("โ”‚ โ”‚", { fgColor: "color03" }),
72- Text("โ”‚ Please resize to at โ”‚", { fgColor: "color03" }),
73- Text(
74- `โ”‚ least ${String(MIN_COLS).padStart(3)}ร—${String(MIN_ROWS).padStart(2)} chars. โ”‚`,
75- { fgColor: "color03" },
76- ),
77- Text("โ”‚ โ”‚", { fgColor: "color03" }),
78- Text("โ”‚ Ctrl+Q to quit โ”‚", { fgColor: "color03" }),
79- Text("โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜", { fgColor: "color03" }),
70+ ...warningBox([
71+ " Terminal too small :(",
72+ "",
73+ " Please resize to at",
74+ ` least ${String(MIN_COLS).padStart(3)}ร—${String(MIN_ROWS).padStart(2)} chars.`,
75+ "",
76+ " Ctrl+Q to quit",
77+ ]),
8078 ],
8179 );
8280 }
examples/pet.ts+9โˆ’11
@@ -22,6 +22,7 @@ import {
2222 ProcessTerminal,
2323 } from "@cel-tui/core";
2424 import { Button, Divider, Spacer, VDivider } from "@cel-tui/components";
25+import { warningBox } from "./warning-box";
2526
2627 // โ”€โ”€โ”€ Pet Definitions โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
2728
@@ -347,17 +348,14 @@ function tooSmallView() {
347348 },
348349 },
349350 [
350- Text("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”", { fgColor: "color03" }),
351- Text("โ”‚ Terminal too small :( โ”‚", { fgColor: "color03" }),
352- Text("โ”‚ โ”‚", { fgColor: "color03" }),
353- Text("โ”‚ Please resize to at โ”‚", { fgColor: "color03" }),
354- Text(
355- `โ”‚ least ${String(MIN_COLS).padStart(3)}ร—${String(MIN_ROWS).padStart(2)} chars. โ”‚`,
356- { fgColor: "color03" },
357- ),
358- Text("โ”‚ โ”‚", { fgColor: "color03" }),
359- Text("โ”‚ Ctrl+Q to quit โ”‚", { fgColor: "color03" }),
360- Text("โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜", { fgColor: "color03" }),
351+ ...warningBox([
352+ " Terminal too small :(",
353+ "",
354+ " Please resize to at",
355+ ` least ${String(MIN_COLS).padStart(3)}ร—${String(MIN_ROWS).padStart(2)} chars.`,
356+ "",
357+ " Ctrl+Q to quit",
358+ ]),
361359 ],
362360 );
363361 }
examples/simple-scroll.ts+9โˆ’11
@@ -9,6 +9,7 @@
99 */
1010 import { cel, VStack, HStack, Text, ProcessTerminal } from "@cel-tui/core";
1111 import { Divider, Spacer } from "@cel-tui/components";
12+import { warningBox } from "./warning-box";
1213
1314 const MIN_COLS = 30;
1415 const MIN_ROWS = 8;
@@ -64,17 +65,14 @@ cel.viewport(() => {
6465 },
6566 },
6667 [
67- Text("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”", { fgColor: "color03" }),
68- Text("โ”‚ Terminal too small :( โ”‚", { fgColor: "color03" }),
69- Text("โ”‚ โ”‚", { fgColor: "color03" }),
70- Text("โ”‚ Please resize to at โ”‚", { fgColor: "color03" }),
71- Text(
72- `โ”‚ least ${String(MIN_COLS).padStart(3)}ร—${String(MIN_ROWS).padStart(2)} chars. โ”‚`,
73- { fgColor: "color03" },
74- ),
75- Text("โ”‚ โ”‚", { fgColor: "color03" }),
76- Text("โ”‚ Ctrl+Q to quit โ”‚", { fgColor: "color03" }),
77- Text("โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜", { fgColor: "color03" }),
68+ ...warningBox([
69+ " Terminal too small :(",
70+ "",
71+ " Please resize to at",
72+ ` least ${String(MIN_COLS).padStart(3)}ร—${String(MIN_ROWS).padStart(2)} chars.`,
73+ "",
74+ " Ctrl+Q to quit",
75+ ]),
7876 ],
7977 );
8078 }
examples/syntax-highlight.ts+13โˆ’15
@@ -15,6 +15,7 @@ import {
1515 VDivider,
1616 type SyntaxHighlightTheme,
1717 } from "@cel-tui/components";
18+import { warningBox } from "./warning-box";
1819
1920 const LANGUAGE = "typescript";
2021 const BASH_LANGUAGE = "bash";
@@ -269,17 +270,14 @@ cel.viewport(() => {
269270 },
270271 },
271272 [
272- Text("โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”", { fgColor: "color03" }),
273- Text("โ”‚ Terminal too small :( โ”‚", { fgColor: "color03" }),
274- Text("โ”‚ โ”‚", { fgColor: "color03" }),
275- Text("โ”‚ Please resize to at least โ”‚", { fgColor: "color03" }),
276- Text(
277- `โ”‚ ${String(MIN_COLS).padStart(3)}ร—${String(minRows).padStart(2)} characters. โ”‚`,
278- { fgColor: "color03" },
279- ),
280- Text("โ”‚ โ”‚", { fgColor: "color03" }),
281- Text("โ”‚ Ctrl+Q to quit โ”‚", { fgColor: "color03" }),
282- Text("โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜", { fgColor: "color03" }),
273+ ...warningBox([
274+ " Terminal too small :(",
275+ "",
276+ " Please resize to at least",
277+ ` ${String(MIN_COLS).padStart(3)}ร—${String(minRows).padStart(2)} characters.`,
278+ "",
279+ " Ctrl+Q to quit",
280+ ]),
283281 ],
284282 );
285283 }
@@ -289,14 +287,14 @@ cel.viewport(() => {
289287 const theme = THEME_OPTIONS[themeIndex]!;
290288 const panes = useSideBySide
291289 ? HStack({ flex: 1 }, [
292- codePane("TypeScript", "streaming", content, LANGUAGE, theme.theme),
290+ codePane("TypeScript", status, content, LANGUAGE, theme.theme),
293291 VDivider({ fgColor: "color08" }),
294- codePane("Bash", "streaming", bashContent, BASH_LANGUAGE, theme.theme),
292+ codePane("Bash", status, bashContent, BASH_LANGUAGE, theme.theme),
295293 ])
296294 : VStack({ flex: 1 }, [
297- codePane("TypeScript", "streaming", content, LANGUAGE, theme.theme),
295+ codePane("TypeScript", status, content, LANGUAGE, theme.theme),
298296 Divider({ fgColor: "color08" }),
299- codePane("Bash", "streaming", bashContent, BASH_LANGUAGE, theme.theme),
297+ codePane("Bash", status, bashContent, BASH_LANGUAGE, theme.theme),
300298 ]);
301299
302300 return VStack(
examples/warning-box.ts+14โˆ’0
@@ -0,0 +1,14 @@
1+import { Text } from "@cel-tui/core";
2+
3+export function warningBox(lines: string[]) {
4+ const innerWidth = lines.reduce((max, line) => Math.max(max, line.length), 0);
5+ const border = "โ”€".repeat(innerWidth);
6+
7+ return [
8+ Text(`โ”Œ${border}โ”`, { fgColor: "color03" }),
9+ ...lines.map((line) =>
10+ Text(`โ”‚${line.padEnd(innerWidth)}โ”‚`, { fgColor: "color03" }),
11+ ),
12+ Text(`โ””${border}โ”˜`, { fgColor: "color03" }),
13+ ];
14+}
packages/core/src/cel.test.ts+89โˆ’0
@@ -1027,6 +1027,30 @@ describe("cel end-to-end", () => {
10271027 await waitForRender();
10281028 expect(rootKey).toBe("z");
10291029 });
1030+
1031+ test("unfocused key does not leak through a topmost layer without a root handler", async () => {
1032+ const term = setup(20, 5);
1033+ let baseLayerKey = "";
1034+
1035+ cel.viewport(() => [
1036+ VStack(
1037+ {
1038+ width: 20,
1039+ height: 5,
1040+ onKeyPress: (key) => {
1041+ baseLayerKey = key;
1042+ },
1043+ },
1044+ [Text("base")],
1045+ ),
1046+ VStack({ width: 20, height: 5 }, [Text("overlay")]),
1047+ ]);
1048+ await waitForRender();
1049+
1050+ term.sendInput("z");
1051+ await waitForRender();
1052+ expect(baseLayerKey).toBe("");
1053+ });
10301054 });
10311055
10321056 describe("TextInput Tab handling", () => {
@@ -1850,6 +1874,71 @@ describe("cel end-to-end", () => {
18501874 await waitForRender();
18511875 expect(clicks).toEqual(["btn"]);
18521876 });
1877+
1878+ test("controlled TextInput blur from re-render preserves Tab continuity", async () => {
1879+ const term = setup(40, 5);
1880+ let value = "";
1881+ let inputFocused = false;
1882+ let buttonFocused = false;
1883+
1884+ cel.viewport(() =>
1885+ VStack({ width: 40, height: 5 }, [
1886+ TextInput({
1887+ value,
1888+ onChange: (v) => {
1889+ value = v;
1890+ cel.render();
1891+ },
1892+ focused: inputFocused,
1893+ onFocus: () => {
1894+ inputFocused = true;
1895+ buttonFocused = false;
1896+ cel.render();
1897+ },
1898+ onBlur: () => {
1899+ inputFocused = false;
1900+ cel.render();
1901+ },
1902+ onKeyPress: (key) => {
1903+ if (key === "enter") {
1904+ inputFocused = false;
1905+ cel.render();
1906+ return false;
1907+ }
1908+ },
1909+ }),
1910+ HStack(
1911+ {
1912+ onClick: () => {},
1913+ focused: buttonFocused,
1914+ onFocus: () => {
1915+ buttonFocused = true;
1916+ inputFocused = false;
1917+ cel.render();
1918+ },
1919+ onBlur: () => {
1920+ buttonFocused = false;
1921+ cel.render();
1922+ },
1923+ },
1924+ [Text("Button")],
1925+ ),
1926+ ]),
1927+ );
1928+ await waitForRender();
1929+
1930+ term.sendInput(TAB);
1931+ await waitForRender();
1932+ expect(inputFocused).toBe(true);
1933+
1934+ term.sendInput(ENTER);
1935+ await waitForRender();
1936+ expect(inputFocused).toBe(false);
1937+
1938+ term.sendInput(TAB);
1939+ await waitForRender();
1940+ expect(buttonFocused).toBe(true);
1941+ });
18531942 });
18541943
18551944 describe("focusStyle rendering", () => {
packages/core/src/cel.ts+39โˆ’13
@@ -123,6 +123,8 @@ function doRender(): void {
123123 currentLayouts.push(layoutTree);
124124 }
125125
126+ syncLastFocusedIndex();
127+
126128 // Stamp uncontrolled focus and scroll before painting
127129 stampUncontrolledFocus();
128130 stampUncontrolledScroll();
@@ -210,6 +212,32 @@ function findFocusedTIInTree(ln: LayoutNode): LayoutNode | null {
210212 return null;
211213 }
212214
215+function syncLastFocusedIndex(): void {
216+ const topLayer = currentLayouts[currentLayouts.length - 1];
217+ if (!topLayer) return;
218+
219+ const focusables = collectFocusable(topLayer);
220+ if (focusables.length === 0) return;
221+
222+ const controlled = findFocusedInTree(topLayer);
223+ if (controlled) {
224+ let idx = focusables.indexOf(controlled);
225+ if (idx === -1) {
226+ idx = focusables.findIndex(
227+ (focusable) => focusable.node === controlled.node,
228+ );
229+ }
230+ if (idx !== -1) {
231+ lastFocusedIndex = idx;
232+ }
233+ return;
234+ }
235+
236+ if (frameworkFocusIndex >= 0 && frameworkFocusIndex < focusables.length) {
237+ lastFocusedIndex = frameworkFocusIndex;
238+ }
239+}
240+
213241 // --- Input handling ---
214242
215243 const BRACKETED_PASTE_START = "\x1b[200~";
@@ -996,20 +1024,18 @@ function handleKeyEvent(event: KeyInput): void {
9961024 }
9971025 }
9981026
999- // No focused element โ€” try root onKeyPress on topmost layer
1000- for (let i = currentLayouts.length - 1; i >= 0; i--) {
1001- const layoutRoot = requiredAt(currentLayouts, i, "layout root");
1002- const path = [layoutRoot];
1003- const handlers = collectKeyPressHandlers(path);
1004- if (handlers.length > 0) {
1005- for (const h of handlers) {
1006- const result = h.handler(key);
1007- if (result !== false) break;
1008- }
1009- cel.render();
1010- return;
1011- }
1027+ // No focused element โ€” only the topmost layer's root can receive the key.
1028+ const topLayer = currentLayouts[currentLayouts.length - 1];
1029+ if (!topLayer) return;
1030+
1031+ const handlers = collectKeyPressHandlers([topLayer]);
1032+ if (handlers.length === 0) return;
1033+
1034+ for (const h of handlers) {
1035+ const result = h.handler(key);
1036+ if (result !== false) break;
10121037 }
1038+ cel.render();
10131039 }
10141040
10151041 /**
packages/core/src/layout.test.ts+45โˆ’0
@@ -479,6 +479,28 @@ describe("layout", () => {
479479 expect(item(childRects(result), 0).y).toBe(0);
480480 });
481481
482+ test("space-between ignores main-axis gap in HStack", () => {
483+ const node = HStack(
484+ {
485+ width: 25,
486+ height: 1,
487+ justifyContent: "space-between",
488+ gap: 4,
489+ },
490+ [
491+ VStack({ width: 6, height: 1 }, []),
492+ VStack({ width: 6, height: 1 }, []),
493+ VStack({ width: 6, height: 1 }, []),
494+ ],
495+ );
496+ const result = layout(node, 25, 1);
497+ const rects = childRects(result);
498+
499+ expect(rects[0]).toEqual({ x: 0, y: 0, width: 6, height: 1 });
500+ expect(rects[1]).toEqual({ x: 10, y: 0, width: 6, height: 1 });
501+ expect(rects[2]).toEqual({ x: 19, y: 0, width: 6, height: 1 });
502+ });
503+
482504 test("center in HStack centers horizontally", () => {
483505 const node = HStack({ width: 80, height: 24, justifyContent: "center" }, [
484506 VStack({ width: 10, height: 24 }, []),
@@ -947,6 +969,29 @@ describe("layout", () => {
947969 expect(item(rects, 3).x).toBe(60);
948970 });
949971
972+ test("wrapping HStack space-between ignores main-axis gap", () => {
973+ const node = HStack(
974+ {
975+ width: 25,
976+ height: 4,
977+ flexWrap: "wrap",
978+ justifyContent: "space-between",
979+ gap: 4,
980+ },
981+ [
982+ VStack({ width: 6, height: 1 }, []),
983+ VStack({ width: 6, height: 1 }, []),
984+ VStack({ width: 6, height: 1 }, []),
985+ ],
986+ );
987+ const result = layout(node, 25, 4);
988+ const rects = childRects(result);
989+
990+ expect(rects[0]).toEqual({ x: 0, y: 0, width: 6, height: 1 });
991+ expect(rects[1]).toEqual({ x: 10, y: 0, width: 6, height: 1 });
992+ expect(rects[2]).toEqual({ x: 19, y: 0, width: 6, height: 1 });
993+ });
994+
950995 test("intrinsic height of wrapping HStack equals sum of row heights", () => {
951996 // HStack with no explicit height should size to fit all rows
952997 const node = VStack({ width: 80, height: 24 }, [
packages/core/src/layout.ts+18โˆ’8
@@ -64,6 +64,13 @@ function requiredAt<T>(
6464 return item;
6565 }
6666
67+function getMainAxisGap(
68+ gap: number,
69+ justify: ContainerProps["justifyContent"] | undefined,
70+): number {
71+ return justify === "space-between" ? 0 : gap;
72+}
73+
6774 // --- Intrinsic size computation ---
6875
6976 /**
@@ -111,6 +118,7 @@ function intrinsicMainSize(
111118 // If it's the cross axis, take the max of children on that axis.
112119 const props = node.props;
113120 const gap = props.gap ?? 0;
121+ const mainAxisGap = getMainAxisGap(gap, props.justifyContent);
114122 const containerIsVertical = node.type === "vstack";
115123 const axisMatchesMain = isVertical === containerIsVertical;
116124
@@ -149,7 +157,7 @@ function intrinsicMainSize(
149157 childMain = clamp(childMain, minMain, maxMain);
150158 }
151159 total += childMain;
152- if (i < node.children.length - 1) total += gap;
160+ if (i < node.children.length - 1) total += mainAxisGap;
153161 }
154162 return total + padMain;
155163 }
@@ -185,7 +193,7 @@ function intrinsicMainSize(
185193 }
186194 wrapHeights.push(h);
187195 }
188- const wrapRows = assignWrapRows(wrapWidths, innerCross, gap);
196+ const wrapRows = assignWrapRows(wrapWidths, innerCross, mainAxisGap);
189197 let total = 0;
190198 for (let ri = 0; ri < wrapRows.length; ri++) {
191199 let maxH = 0;
@@ -359,6 +367,7 @@ function layoutWrapHStack(
359367 const gap = props.gap ?? 0;
360368 const align = props.alignItems ?? "stretch";
361369 const justify = props.justifyContent ?? "start";
370+ const itemGap = getMainAxisGap(gap, justify);
362371
363372 // Padding
364373 const padX = props.padding?.x ?? 0;
@@ -404,7 +413,7 @@ function layoutWrapHStack(
404413 }
405414
406415 // Phase 2: Assign children to rows
407- const rows = assignWrapRows(baseWidths, innerW, gap);
416+ const rows = assignWrapRows(baseWidths, innerW, itemGap);
408417
409418 // Phase 3: Layout each row independently
410419 const layoutChildren: LayoutNode[] = [];
@@ -412,7 +421,7 @@ function layoutWrapHStack(
412421
413422 for (let ri = 0; ri < rows.length; ri++) {
414423 const rowIdx = requiredAt(rows, ri, "wrap row");
415- const rowGapTotal = gap * (rowIdx.length - 1);
424+ const rowGapTotal = itemGap * (rowIdx.length - 1);
416425 const rowAvail = innerW - rowGapTotal;
417426
418427 // Compute fixed and flex totals for this row
@@ -537,7 +546,7 @@ function layoutWrapHStack(
537546
538547 xOffset += childW;
539548 if (ci < rowIdx.length - 1) {
540- xOffset += gap;
549+ xOffset += itemGap;
541550 if (betweenGaps) {
542551 xOffset += requiredAt(betweenGaps, ci, "justified gap");
543552 }
@@ -609,7 +618,9 @@ function layoutNode(
609618
610619 // Gap
611620 const gap = props.gap ?? 0;
612- const totalGap = gap * (children.length - 1);
621+ const justify = props.justifyContent ?? "start";
622+ const mainAxisGap = getMainAxisGap(gap, justify);
623+ const totalGap = mainAxisGap * (children.length - 1);
613624 const mainAvail = (isVertical ? innerH : innerW) - totalGap;
614625
615626 // --- Measure phase: compute each child's main-axis and cross-axis size ---
@@ -738,7 +749,6 @@ function layoutNode(
738749 const remainingMain = Math.max(0, mainInner - totalContent);
739750
740751 // justifyContent: compute main-axis starting offset and per-gap extra space
741- const justify = props.justifyContent ?? "start";
742752 let mainStart = 0;
743753 let betweenGaps: number[] | null = null;
744754
@@ -780,7 +790,7 @@ function layoutNode(
780790
781791 mainOffset += info.mainSize;
782792 if (i < infos.length - 1) {
783- mainOffset += gap;
793+ mainOffset += mainAxisGap;
784794 if (betweenGaps) {
785795 mainOffset += requiredAt(betweenGaps, i, "justified gap");
786796 }
packages/core/src/paint.test.ts+20โˆ’0
@@ -402,6 +402,26 @@ describe("paint", () => {
402402 expect(lastCol.some((c) => c !== " ")).toBe(true);
403403 });
404404
405+ test("scrollbar thumb position accounts for vertical padding", () => {
406+ const node = VStack(
407+ {
408+ width: 6,
409+ height: 7,
410+ overflow: "scroll",
411+ scrollOffset: 1,
412+ scrollbar: true,
413+ padding: { y: 1 },
414+ },
415+ Array.from({ length: 7 }, (_, i) => Text(`line${i}`)),
416+ );
417+ const ln = layout(node, 6, 7);
418+ const buf = new CellBuffer(6, 7);
419+ paint(ln, buf);
420+
421+ const lastCol = Array.from({ length: 7 }, (_, y) => buf.get(5, y).char);
422+ expect(lastCol.join("")).toBe("โ”‚โ”ƒโ”ƒโ”ƒโ”ƒโ”ƒโ”‚");
423+ });
424+
405425 test("scroll clamps to max offset", () => {
406426 const node = VStack(
407427 { width: 10, height: 3, overflow: "scroll", scrollOffset: 99 },
packages/core/src/paint.ts+15โˆ’7
@@ -232,23 +232,30 @@ function paintScrollbar(
232232 ): void {
233233 const { rect, children } = ln;
234234 const isVertical = ln.node.type === "vstack";
235+ const props =
236+ ln.node.type === "vstack" || ln.node.type === "hstack"
237+ ? ln.node.props
238+ : null;
239+ if (!props) return;
235240
236241 if (isVertical) {
237- // Compute total content height from children
242+ // Compute total scrollable height from children plus bottom padding.
243+ // Child positions already include top padding, so add only the trailing pad.
238244 let contentHeight = 0;
239245 for (const child of children) {
240246 const childBottom = child.rect.y + child.rect.height - rect.y;
241247 if (childBottom > contentHeight) contentHeight = childBottom;
242248 }
249+ const scrollHeight = contentHeight + (props.padding?.y ?? 0);
243250 const viewportH = rect.height;
244- if (contentHeight <= viewportH) return; // no scrollbar needed
251+ if (scrollHeight <= viewportH) return; // no scrollbar needed
245252
246253 // Thumb size and position
247254 const thumbSize = Math.max(
248255 1,
249- Math.round((viewportH / contentHeight) * viewportH),
256+ Math.round((viewportH / scrollHeight) * viewportH),
250257 );
251- const maxOffset = contentHeight - viewportH;
258+ const maxOffset = scrollHeight - viewportH;
252259 const thumbPos =
253260 maxOffset > 0
254261 ? Math.round((scrollOffset / maxOffset) * (viewportH - thumbSize))
@@ -276,14 +283,15 @@ function paintScrollbar(
276283 const childRight = child.rect.x + child.rect.width - rect.x;
277284 if (childRight > contentWidth) contentWidth = childRight;
278285 }
286+ const scrollWidth = contentWidth + (props.padding?.x ?? 0);
279287 const viewportW = rect.width;
280- if (contentWidth <= viewportW) return;
288+ if (scrollWidth <= viewportW) return;
281289
282290 const thumbSize = Math.max(
283291 1,
284- Math.round((viewportW / contentWidth) * viewportW),
292+ Math.round((viewportW / scrollWidth) * viewportW),
285293 );
286- const maxOffset = contentWidth - viewportW;
294+ const maxOffset = scrollWidth - viewportW;
287295 const thumbPos =
288296 maxOffset > 0
289297 ? Math.round((scrollOffset / maxOffset) * (viewportW - thumbSize))