@@ -60,9 +60,137 @@ function paintLayoutNode(
6060 break;
6161 }
6262
63+ // Determine scroll offset for scrollable containers
64+ const isContainer = node.type === "vstack" || node.type === "hstack";
65+ const containerProps = isContainer ? node.props : null;
66+ const isScrollable = containerProps?.overflow === "scroll";
67+ const scrollOffset = isScrollable
68+ ? (containerProps.scrollOffset ?? getContainerScroll(containerProps))
69+ : 0;
70+ const isVertical = node.type === "vstack";
71+
6372 // Recurse into children, using this node's clipped rect as the clip for children
6473 for (const child of ln.children) {
65- paintLayoutNode(child, buf, clipped);
74+ if (isScrollable && scrollOffset !== 0) {
75+ // Paint child with shifted position
76+ const shifted = shiftLayoutNode(child, isVertical, -scrollOffset);
77+ paintLayoutNode(shifted, buf, clipped);
78+ } else {
79+ paintLayoutNode(child, buf, clipped);
80+ }
81+ }
82+
83+ // Paint scrollbar if enabled
84+ if (isScrollable && containerProps.scrollbar) {
85+ paintScrollbar(ln, scrollOffset, buf, clipped);
86+ }
87+}
88+
89+/**
90+ * Create a copy of a layout node (and all descendants) with positions
91+ * shifted along the given axis.
92+ */
93+function shiftLayoutNode(
94+ ln: LayoutNode,
95+ isVertical: boolean,
96+ offset: number,
97+): LayoutNode {
98+ const newRect: Rect = {
99+ x: ln.rect.x + (isVertical ? 0 : offset),
100+ y: ln.rect.y + (isVertical ? offset : 0),
101+ width: ln.rect.width,
102+ height: ln.rect.height,
103+ };
104+ return {
105+ node: ln.node,
106+ rect: newRect,
107+ children: ln.children.map((c) => shiftLayoutNode(c, isVertical, offset)),
108+ };
109+}
110+
111+/**
112+ * Paint a scrollbar indicator for a scrollable container.
113+ */
114+function paintScrollbar(
115+ ln: LayoutNode,
116+ scrollOffset: number,
117+ buf: CellBuffer,
118+ clipRect: Rect,
119+): void {
120+ const { rect, children } = ln;
121+ const isVertical = ln.node.type === "vstack";
122+
123+ if (isVertical) {
124+ // Compute total content height from children
125+ let contentHeight = 0;
126+ for (const child of children) {
127+ const childBottom = child.rect.y + child.rect.height - rect.y;
128+ if (childBottom > contentHeight) contentHeight = childBottom;
129+ }
130+ const viewportH = rect.height;
131+ if (contentHeight <= viewportH) return; // no scrollbar needed
132+
133+ // Thumb size and position
134+ const thumbSize = Math.max(
135+ 1,
136+ Math.round((viewportH / contentHeight) * viewportH),
137+ );
138+ const maxOffset = contentHeight - viewportH;
139+ const thumbPos =
140+ maxOffset > 0
141+ ? Math.round((scrollOffset / maxOffset) * (viewportH - thumbSize))
142+ : 0;
143+
144+ const barX = rect.x + rect.width - 1;
145+ for (let row = 0; row < viewportH; row++) {
146+ const absY = rect.y + row;
147+ if (absY < clipRect.y || absY >= clipRect.y + clipRect.height) continue;
148+ if (barX < clipRect.x || barX >= clipRect.x + clipRect.width) continue;
149+ const isThumb = row >= thumbPos && row < thumbPos + thumbSize;
150+ buf.set(barX, absY, {
151+ char: isThumb ? "┃" : "│",
152+ fgColor: isThumb ? "white" : "brightBlack",
153+ bgColor: null,
154+ bold: false,
155+ italic: false,
156+ underline: false,
157+ });
158+ }
159+ } else {
160+ // Horizontal scrollbar
161+ let contentWidth = 0;
162+ for (const child of children) {
163+ const childRight = child.rect.x + child.rect.width - rect.x;
164+ if (childRight > contentWidth) contentWidth = childRight;
165+ }
166+ const viewportW = rect.width;
167+ if (contentWidth <= viewportW) return;
168+
169+ const thumbSize = Math.max(
170+ 1,
171+ Math.round((viewportW / contentWidth) * viewportW),
172+ );
173+ const maxOffset = contentWidth - viewportW;
174+ const thumbPos =
175+ maxOffset > 0
176+ ? Math.round((scrollOffset / maxOffset) * (viewportW - thumbSize))
177+ : 0;
178+
179+ const barY = rect.y + rect.height - 1;
180+ for (let col = 0; col < viewportW; col++) {
181+ const absX = rect.x + col;
182+ if (absX < clipRect.x || absX >= clipRect.x + clipRect.width) continue;
183+ if (barY < clipRect.y || barY >= clipRect.y + clipRect.height) continue;
184+ const isThumb = col >= thumbPos && col < thumbPos + thumbSize;
185+ buf.set(absX, barY, {
186+ char: isThumb ? "━" : "─",
187+ fgColor: isThumb ? "white" : "brightBlack",
188+ bgColor: null,
189+ bold: false,
190+ italic: false,
191+ underline: false,
192+ });
193+ }
66194 }
67195 }
68196
@@ -285,7 +413,24 @@ function offsetToWrappedPos(
285413 return { line: wrappedLine, col: 0 };
286414 }
287415
288-// --- TextInput framework-managed state ---
416+// --- Framework-managed state ---
417+
418+import type { ContainerProps } from "@cel-tui/types";
419+
420+const containerScrolls = new WeakMap<ContainerProps, number>();
421+
422+/** Get the scroll offset for an uncontrolled scrollable container. */
423+export function getContainerScroll(props: ContainerProps): number {
424+ return containerScrolls.get(props) ?? 0;
425+}
426+
427+/** Set the scroll offset for an uncontrolled scrollable container. */
428+export function setContainerScroll(
429+ props: ContainerProps,
430+ scroll: number,
431+): void {
432+ containerScrolls.set(props, scroll);
433+}
289434
290435 const textInputCursors = new WeakMap<TextInputProps, number>();
291436 const textInputScrolls = new WeakMap<TextInputProps, number>();