@@ -16,8 +16,6 @@ import {
1616 setTextInputCursor,
1717 getTextInputScroll,
1818 setTextInputScroll,
19- getContainerScroll,
20- setContainerScroll,
2119 } from "./paint.js";
2220 import {
2321 insertChar,
@@ -49,6 +47,17 @@ let frameworkFocusIndex = -1;
4947 /** The node whose props were stamped with `focused: true` during the last paint. */
5048 let stampedNode: LayoutNode | null = null;
5149
50+/**
51+ * Uncontrolled scroll offsets, keyed by structural tree path.
52+ * The path is a string like "0/2/1" representing the DFS child indices
53+ * from the layer root to the scrollable container. This survives re-renders
54+ * because the structural position is the same even with new props objects.
55+ */
56+const uncontrolledScrollOffsets = new Map<string, number>();
57+
58+/** Nodes whose props were stamped with scrollOffset during the last paint. */
59+let stampedScrollNodes: { node: Node; key: string }[] = [];
60+
5261 function doRender(): void {
5362 renderScheduled = false;
5463 if (!renderFn || !terminal) return;
@@ -81,8 +90,9 @@ function doRender(): void {
8190 currentLayouts.push(layoutTree);
8291 }
8392
84- // Stamp uncontrolled focus before painting so focusStyle and cursor work
93+ // Stamp uncontrolled focus and scroll before painting
8594 stampUncontrolledFocus();
95+ stampUncontrolledScroll();
8696
8797 // Paint each layer into the buffer
8898 for (const layoutTree of currentLayouts) {
@@ -91,6 +101,7 @@ function doRender(): void {
91101
92102 // Unstamp after paint so input handlers see clean props
93103 unstampUncontrolledFocus();
104+ unstampUncontrolledScroll();
94105
95106 // Emit to terminal — differential when possible
96107 if (prevBuffer) {
@@ -252,6 +263,75 @@ function unstampUncontrolledFocus(): void {
252263 stampedNode = null;
253264 }
254265
266+/**
267+ * Compute the structural tree path from a layer root to a target LayoutNode.
268+ * Returns a string like "0/2/1" or null if target is not in the tree.
269+ */
270+function computeTreePath(root: LayoutNode, target: LayoutNode): string | null {
271+ if (root === target) return "";
272+ for (let i = 0; i < root.children.length; i++) {
273+ const sub = computeTreePath(root.children[i]!, target);
274+ if (sub !== null) return sub === "" ? String(i) : `${i}/${sub}`;
275+ }
276+ return null;
277+}
278+
279+/**
280+ * Get the full path key for a scrollable node, prefixed by layer index.
281+ */
282+function getScrollPathKey(target: LayoutNode): string | null {
283+ for (let i = 0; i < currentLayouts.length; i++) {
284+ const path = computeTreePath(currentLayouts[i]!, target);
285+ if (path !== null) return `L${i}:${path}`;
286+ }
287+ return null;
288+}
289+
290+/**
291+ * Stamp `scrollOffset` on uncontrolled scrollable containers' props
292+ * before painting, so paint reads the correct offset.
293+ */
294+function stampUncontrolledScroll(): void {
295+ stampedScrollNodes = [];
296+ if (uncontrolledScrollOffsets.size === 0) return;
297+ for (let i = 0; i < currentLayouts.length; i++) {
298+ walkAndStampScroll(currentLayouts[i]!, `L${i}:`);
299+ }
300+}
301+
302+function walkAndStampScroll(ln: LayoutNode, pathKey: string): void {
303+ const node = ln.node;
304+ if (node.type === "vstack" || node.type === "hstack") {
305+ if (
306+ node.props.overflow === "scroll" &&
307+ node.props.scrollOffset === undefined &&
308+ !node.props.onScroll
309+ ) {
310+ const offset = uncontrolledScrollOffsets.get(pathKey);
311+ if (offset !== undefined && offset !== 0) {
312+ (node.props as any).scrollOffset = offset;
313+ stampedScrollNodes.push({ node, key: pathKey });
314+ }
315+ }
316+ }
317+ for (let i = 0; i < ln.children.length; i++) {
318+ // Keys match getScrollPathKey format: "L0:" for root, "L0:2" for child 2, "L0:2/1" for grandchild
319+ const childKey = pathKey.endsWith(":")
320+ ? `${pathKey}${i}`
321+ : `${pathKey}/${i}`;
322+ walkAndStampScroll(ln.children[i]!, childKey);
323+ }
324+}
325+
326+function unstampUncontrolledScroll(): void {
327+ for (const { node } of stampedScrollNodes) {
328+ if (node.type === "vstack" || node.type === "hstack") {
329+ delete (node.props as any).scrollOffset;
330+ }
331+ }
332+ stampedScrollNodes = [];
333+}
334+
255335 /**
256336 * Blur the currently focused element and focus a new one.
257337 * Manages both controlled (via onFocus/onBlur callbacks) and
@@ -383,7 +463,7 @@ function handleMouseEvent(event: MouseEvent): void {
383463 cel.render();
384464 } else {
385465 const props = target.node.type !== "text" ? target.node.props : null;
386- if (props && "onScroll" in props) {
466+ if (props && (props as any).overflow === "scroll") {
387467 if (props.onScroll) {
388468 // Controlled scroll: notify app.
389469 // Use batch accumulator if available (multiple events in one chunk),
@@ -399,10 +479,16 @@ function handleMouseEvent(event: MouseEvent): void {
399479 batchScrollOffsets?.set(props, newOffset);
400480 props.onScroll(newOffset);
401481 } else {
402- // Uncontrolled scroll: framework manages state
403- const current = getContainerScroll(props);
404- const clamped = Math.max(0, Math.min(maxOffset, current + delta));
405- setContainerScroll(props, clamped);
482+ // Uncontrolled scroll: framework manages state via path key
483+ const pathKey = getScrollPathKey(target);
484+ if (pathKey !== null) {
485+ const current = uncontrolledScrollOffsets.get(pathKey) ?? 0;
486+ const clamped = Math.max(
487+ 0,
488+ Math.min(maxOffset, current + delta),
489+ );
490+ uncontrolledScrollOffsets.set(pathKey, clamped);
491+ }
406492 }
407493 cel.render();
408494 }
@@ -731,6 +817,8 @@ export const cel = {
731817 lastFocusedIndex = -1;
732818 frameworkFocusIndex = -1;
733819 stampedNode = null;
820+ uncontrolledScrollOffsets.clear();
821+ stampedScrollNodes = [];
734822 },
735823
736824 /** @internal */