Merge pull request #2108 into validated PR integration

This commit is contained in:
Bingxi Zhao (Frank)
2026-10-29 00:56:10 +08:00
2 changed files with 28 additions and 9 deletions

View File

@@ -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]);

View File

@@ -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(<Harness />); 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);
});