xonecas.io
70160
Ce

cel-tui / commit 29ac7ad

fix(core): getMaxScrollOffset accounts for container padding

The max scroll offset calculation now adds padding to the content extent
so scrollable containers with padding don't allow scrolling past content.

Added test: scroll clamps correctly with padding.

xonecas2026-04-04 29ac7ad153e222b895d830ea4bb303bfb04dc99e parent dab1c55

2 files changed

packages/core/src/cel.test.ts+30−0
@@ -338,6 +338,36 @@ describe("cel end-to-end", () => {
338338 for (let x = 0; x < 6; x++) row2 += buf.get(x, 2).char;
339339 expect(row2).toBe("item 5");
340340 });
341+
342+ test("scroll clamps correctly with padding", async () => {
343+ const term = setup(20, 7);
344+ let scrollOffset = 0;
345+ cel.viewport(() =>
346+ VStack(
347+ {
348+ width: 20,
349+ height: 7,
350+ overflow: "scroll",
351+ padding: { y: 1 },
352+ scrollOffset: scrollOffset,
353+ onScroll: (offset) => {
354+ scrollOffset = offset;
355+ },
356+ },
357+ // 7 items, viewport inner height = 5 (7 - 2*1), max offset = 2
358+ Array.from({ length: 7 }, (_, i) => Text(`line ${i}`)),
359+ ),
360+ );
361+ await waitForRender();
362+
363+ // Scroll down 10 times — should clamp at 2 (7 items - 5 visible = 2)
364+ for (let i = 0; i < 10; i++) {
365+ term.sendInput("\x1b[<65;4;4M");
366+ await waitForRender();
367+ }
368+
369+ expect(scrollOffset).toBe(2);
370+ });
341371 });
342372
343373 describe("focus system", () => {
packages/core/src/cel.ts+7−2
@@ -408,6 +408,9 @@ function getMaxScrollOffset(target: LayoutNode): number {
408408 }
409409
410410 const isVertical = target.node.type === "vstack";
411+ const props = target.node.type !== "text" ? target.node.props : null;
412+ const padX = (props as any)?.padding?.x ?? 0;
413+ const padY = (props as any)?.padding?.y ?? 0;
411414
412415 if (isVertical) {
413416 let contentHeight = 0;
@@ -415,14 +418,16 @@ function getMaxScrollOffset(target: LayoutNode): number {
415418 const childBottom = child.rect.y + child.rect.height - rect.y;
416419 if (childBottom > contentHeight) contentHeight = childBottom;
417420 }
418- return Math.max(0, contentHeight - rect.height);
421+ // Viewport is the inner height (minus bottom padding)
422+ // Content starts at padY, so contentHeight includes top padding offset
423+ return Math.max(0, contentHeight + padY - rect.height);
419424 } else {
420425 let contentWidth = 0;
421426 for (const child of children) {
422427 const childRight = child.rect.x + child.rect.width - rect.x;
423428 if (childRight > contentWidth) contentWidth = childRight;
424429 }
425- return Math.max(0, contentWidth - rect.width);
430+ return Math.max(0, contentWidth + padX - rect.width);
426431 }
427432 }