xonecas.io
70160
Ce

cel-tui / commit eb4da2c

feat(core): auto-scroll TextInput to keep cursor visible

When a focused TextInput's cursor moves below the viewport, the
scroll offset is increased so the cursor line is at the bottom.
When the cursor moves above the viewport, scroll snaps to show
the cursor line at the top. Scroll state is persisted via the
onChange-keyed WeakMap.

xonecas2026-04-04 eb4da2cf1ccea4f7221f6f3c9089ba7c29e222d9 parent 747a5d7

3 files changed

TODO.md+1−1
@@ -77,7 +77,7 @@ What's implemented, what's missing, and what needs hardening. Reference `spec.md
7777 - ✅ Placeholder rendering
7878 - ✅ Cursor display (inverted colors)
7979 - ✅ Cursor state keyed on `onChange` function reference for stable identity across re-renders
80-- ❌ Framework-managed scroll (auto-scroll to keep cursor visible)
80+- ✅ Framework-managed scroll (auto-scroll to keep cursor visible)
8181 - ❌ Mouse wheel scroll inside TextInput
8282
8383 ## Terminal
packages/core/src/paint.ts+16−2
@@ -345,8 +345,22 @@ function paintTextInput(
345345 }
346346 }
347347
348- // Framework-managed scroll: use stored scroll offset
349- const scrollOffset = getTextInputScroll(props);
348+ // Framework-managed scroll: auto-scroll to keep cursor visible
349+ let scrollOffset = getTextInputScroll(props);
350+
351+ if (props.focused) {
352+ const cursorOffset = getTextInputCursor(props);
353+ const cursorPos = offsetToWrappedPos(value, cursorOffset, w);
354+ // Scroll down if cursor is below viewport
355+ if (cursorPos.line >= scrollOffset + h) {
356+ scrollOffset = cursorPos.line - h + 1;
357+ }
358+ // Scroll up if cursor is above viewport
359+ if (cursorPos.line < scrollOffset) {
360+ scrollOffset = cursorPos.line;
361+ }
362+ setTextInputScroll(props, scrollOffset);
363+ }
350364
351365 // Paint visible lines (grapheme-aware)
352366 for (let row = 0; row < h; row++) {
packages/core/src/text-input.test.ts+46−0
@@ -237,6 +237,52 @@ describe("TextInput integration", () => {
237237 expect(value).toBe("abcXdef");
238238 });
239239
240+ test("auto-scrolls to keep cursor visible when typing past viewport", async () => {
241+ const term = setup(20, 3); // 3 rows tall
242+ let value = "line1\nline2\nline3";
243+ const onChange = (v: string) => {
244+ value = v;
245+ cel.render();
246+ };
247+
248+ cel.viewport(() =>
249+ VStack({}, [
250+ TextInput({
251+ value,
252+ focused: true,
253+ height: 3,
254+ onChange,
255+ }),
256+ ]),
257+ );
258+ await waitForRender();
259+
260+ // Press enter to add line4 — cursor is now on line 4 (index 3)
261+ // which is below the 3-row viewport
262+ term.sendInput("\r");
263+ await waitForRender();
264+
265+ // The buffer should show the cursor line (line4 area)
266+ // not the top lines. Check that the scroll adjusted.
267+ const buf = cel._getBuffer()!;
268+ // The cursor should be visible somewhere in the viewport.
269+ // With 4 lines and 3-row viewport, scroll should be at least 1.
270+ // Row 2 (bottom of viewport) should show content from line3 or line4 area.
271+ // At minimum, the empty line4 cursor should be visible.
272+ // Let's verify line1 is NOT visible (scrolled past)
273+ let hasLine1 = false;
274+ for (let x = 0; x < 20; x++) {
275+ if (buf.get(x, 0).char === "l" && buf.get(x + 1, 0).char === "i") {
276+ const row = Array.from(
277+ { length: 5 },
278+ (_, i) => buf.get(x + i, 0).char,
279+ ).join("");
280+ if (row === "line1") hasLine1 = true;
281+ }
282+ }
283+ expect(hasLine1).toBe(false);
284+ });
285+
240286 test("unfocused TextInput does not consume keys", async () => {
241287 const term = setup(20, 3);
242288 let receivedKey = "";