xonecas.io
70160
Ce

cel-tui / commit 587ac55

fix(core): clamp scroll offset to content bounds

Scroll-down no longer overshoots past content, which previously
showed infinite blank space. The max offset is now clamped to
contentSize - viewportSize for containers (VStack/HStack children)
and TextInput (wrapped line count). Scroll-up was already clamped
at 0.

xonecas2026-04-04 587ac554de2ae49fdb4f4ee56403c4d566a2676c parent ad4be61

2 files changed

packages/core/src/cel.test.ts+31−1
@@ -192,7 +192,8 @@ describe("cel end-to-end", () => {
192192 scrollOffset = offset;
193193 },
194194 },
195- [Text("scrollable")],
195+ // Enough content to exceed viewport (5 rows)
196+ Array.from({ length: 10 }, (_, i) => Text(`line ${i + 1}`)),
196197 ),
197198 );
198199 await waitForRender();
@@ -238,6 +239,35 @@ describe("cel end-to-end", () => {
238239
239240 expect(scrollOffset).toBe(3);
240241 });
242+
243+ test("scroll clamps at max offset (no blank space past content)", async () => {
244+ const term = setup(20, 5);
245+ let scrollOffset = 0;
246+ cel.viewport(() =>
247+ VStack(
248+ {
249+ width: 20,
250+ height: 5,
251+ overflow: "scroll",
252+ scrollOffset: scrollOffset,
253+ onScroll: (offset) => {
254+ scrollOffset = offset;
255+ },
256+ },
257+ // 8 items in 5-row viewport → max offset = 3
258+ Array.from({ length: 8 }, (_, i) => Text(`item ${i + 1}`)),
259+ ),
260+ );
261+ await waitForRender();
262+
263+ // Scroll down 10 times — should clamp at 3
264+ for (let i = 0; i < 10; i++) {
265+ term.sendInput("\x1b[<65;4;3M");
266+ await waitForRender();
267+ }
268+
269+ expect(scrollOffset).toBe(3);
270+ });
241271 });
242272
243273 describe("focus system", () => {
packages/core/src/cel.ts+48−3
@@ -27,6 +27,7 @@ import {
2727 type EditState,
2828 } from "./text-edit.js";
2929 import type { Terminal } from "./terminal.js";
30+import { visibleWidth } from "./width.js";
3031
3132 type RenderFn = () => Node | Node[];
3233
@@ -183,6 +184,44 @@ function changeFocus(target: LayoutNode | null): void {
183184 }
184185 }
185186
187+/**
188+ * Compute the maximum scroll offset for a scrollable container or TextInput.
189+ * Returns 0 if content fits within the viewport.
190+ */
191+function getMaxScrollOffset(target: LayoutNode): number {
192+ const { rect, children } = target;
193+
194+ // TextInput: compute content height from wrapped text value
195+ if (target.node.type === "textinput") {
196+ const w = rect.width;
197+ const value = target.node.props.value;
198+ let lineCount = 0;
199+ for (const rawLine of value.split("\n")) {
200+ const lw = visibleWidth(rawLine);
201+ lineCount += w > 0 && lw > w ? Math.ceil(lw / w) : 1;
202+ }
203+ return Math.max(0, lineCount - rect.height);
204+ }
205+
206+ const isVertical = target.node.type === "vstack";
207+
208+ if (isVertical) {
209+ let contentHeight = 0;
210+ for (const child of children) {
211+ const childBottom = child.rect.y + child.rect.height - rect.y;
212+ if (childBottom > contentHeight) contentHeight = childBottom;
213+ }
214+ return Math.max(0, contentHeight - rect.height);
215+ } else {
216+ let contentWidth = 0;
217+ for (const child of children) {
218+ const childRight = child.rect.x + child.rect.width - rect.x;
219+ if (childRight > contentWidth) contentWidth = childRight;
220+ }
221+ return Math.max(0, contentWidth - rect.width);
222+ }
223+}
224+
186225 function handleMouseEvent(event: MouseEvent): void {
187226 // Hit test on topmost layer first
188227 for (let i = currentLayouts.length - 1; i >= 0; i--) {
@@ -209,12 +248,14 @@ function handleMouseEvent(event: MouseEvent): void {
209248 const target = findScrollTarget(path);
210249 if (target) {
211250 const delta = event.type === "scroll-up" ? -1 : 1;
251+ const maxOffset = getMaxScrollOffset(target);
212252
213253 if (target.node.type === "textinput") {
214254 // TextInput scroll is always framework-managed
215255 const tiProps = target.node.props;
216256 const current = getTextInputScroll(tiProps);
217- setTextInputScroll(tiProps, Math.max(0, current + delta));
257+ const clamped = Math.max(0, Math.min(maxOffset, current + delta));
258+ setTextInputScroll(tiProps, clamped);
218259 cel.render();
219260 } else {
220261 const props = target.node.type !== "text" ? target.node.props : null;
@@ -227,13 +268,17 @@ function handleMouseEvent(event: MouseEvent): void {
227268 batchScrollOffsets?.get(props) ??
228269 (props as any).scrollOffset ??
229270 0;
230- const newOffset = Math.max(0, baseOffset + delta);
271+ const newOffset = Math.max(
272+ 0,
273+ Math.min(maxOffset, baseOffset + delta),
274+ );
231275 batchScrollOffsets?.set(props, newOffset);
232276 props.onScroll(newOffset);
233277 } else {
234278 // Uncontrolled scroll: framework manages state
235279 const current = getContainerScroll(props);
236- setContainerScroll(props, Math.max(0, current + delta));
280+ const clamped = Math.max(0, Math.min(maxOffset, current + delta));
281+ setContainerScroll(props, clamped);
237282 }
238283 cel.render();
239284 }