From 63a816d913d78d6e36a71a7c36d7ef46a0cf986a Mon Sep 17 00:00:00 2001 From: heojunfo Date: Wed, 28 Oct 2026 16:51:02 +0900 Subject: [PATCH] fix(web): let a scrollbar drag release chat auto-follow while streaming (#2072) handleScroll only read a scroll as the reader's when the scroll height and viewport were unchanged since the last remembered position, to tell a user gesture from the browser clamping scrollTop on shrink. While a reply streams the height changes every frame, so a scrollbar drag (no wheel or key event) was never seen and the next frame pinned the view back to the bottom. Compare against where the position would be without the reader instead: growth never moves scrollTop (overflow-anchor: none) and clamping only lowers it to the new maximum, so min(previous top, new max) is the expected position whatever the layout did. Co-Authored-By: Claude Opus 5.5 --- web/hooks/useChatAutoScroll.ts | 19 ++++++++++--------- web/tests/chat-autoscroll-layout.spec.tsx | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/web/hooks/useChatAutoScroll.ts b/web/hooks/useChatAutoScroll.ts index 08db25e18..9dfccffdc 100644 --- a/web/hooks/useChatAutoScroll.ts +++ b/web/hooks/useChatAutoScroll.ts @@ -49,17 +49,18 @@ export function useChatAutoScroll({ const container = containerRef.current; if (!container) return; const previous = positionRef.current; - const sameLayout = previous.height === container.scrollHeight && - previous.viewport === container.clientHeight; // The browser clamps scrollTop when content shrinks. That is not a user - // gesture. Neither clamping nor delayed growth may release/re-arm follow. - if (sameLayout) { - const remaining = container.scrollHeight - container.scrollTop - container.clientHeight; - if (remaining < 80 && (shouldAutoScrollRef.current || container.scrollTop > previous.top + 4)) { - shouldAutoScrollRef.current = true; - } - else if (container.scrollTop < previous.top - 4) shouldAutoScrollRef.current = false; + // gesture. Growth never moves it (overflow-anchor: none), so whatever the + // layout did, the position without the reader is at most the new maximum. + // Comparing against that keeps a scrollbar drag during streaming, when the + // reply grows between the drag and its scroll event, a gesture (#2072). + const max = Math.max(0, container.scrollHeight - container.clientHeight); + const expected = Math.min(previous.top, max); + const remaining = container.scrollHeight - container.scrollTop - container.clientHeight; + if (remaining < 80 && (shouldAutoScrollRef.current || container.scrollTop > expected + 4)) { + shouldAutoScrollRef.current = true; } + else if (container.scrollTop < expected - 4) shouldAutoScrollRef.current = false; rememberPosition(container); }, [rememberPosition]); diff --git a/web/tests/chat-autoscroll-layout.spec.tsx b/web/tests/chat-autoscroll-layout.spec.tsx index ca6689e78..ad6a5dd05 100644 --- a/web/tests/chat-autoscroll-layout.spec.tsx +++ b/web/tests/chat-autoscroll-layout.spec.tsx @@ -113,3 +113,21 @@ it("does not yank the viewport when the reader opens working notes", () => { expect(top).toBe(800); expect(following.current).toBe(false); }); + +it("lets the reader drag the scrollbar up while the reply is still growing (#2072)", () => { + render(); flushLayout(); + expect(top).toBe(800); + // A scrollbar drag fires no wheel/key event, and while streaming the reply + // grows between the drag and its scroll event. + top = 300; height = 1100; + fireEvent.scroll(screen.getByTestId("scroll")); flushLayout(); + expect(following.current).toBe(false); + height = 1400; flushLayout(); + expect(top).toBe(300); + // Dragging back to the live end follows again, even as it grows. + top = 1180; height = 1420; + fireEvent.scroll(screen.getByTestId("scroll")); flushLayout(); + expect(following.current).toBe(true); + height = 1600; flushLayout(); + expect(top).toBe(1400); +});