@@ -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 }