Files
DeepTutor/web/hooks/useChatAutoScroll.ts
heojunfo 63a816d913 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 <noreply@anthropic.com>
2026-10-28 16:51:02 +09:00

143 lines
6.0 KiB
TypeScript

"use client";
import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
interface AutoScrollOptions {
hasMessages: boolean;
isStreaming: boolean;
composerHeight: number;
messageCount: number;
lastMessageContent?: string;
lastEventCount?: number;
}
/** Follow layout changes until the reader intentionally leaves the live end.
* Observe content size, not an arbitrary post-stream timeout: markdown, images
* and disclosure animations can settle independently of the transport.
* The scroll root must retain overflow-anchor: none to avoid competing writers.
*/
export function useChatAutoScroll({
hasMessages, isStreaming, composerHeight, messageCount,
lastMessageContent, lastEventCount,
}: AutoScrollOptions) {
const containerRef = useRef<HTMLDivElement>(null);
const endRef = useRef<HTMLDivElement>(null);
const shouldAutoScrollRef = useRef(true);
const positionRef = useRef({ top: 0, height: 0, viewport: 0 });
const rememberPosition = useCallback((container: HTMLDivElement) => {
positionRef.current = {
top: container.scrollTop,
height: container.scrollHeight,
viewport: container.clientHeight,
};
}, []);
const pinToBottom = useCallback(() => {
const container = containerRef.current;
if (!container) return;
container.scrollTop = container.scrollHeight;
rememberPosition(container);
}, [rememberPosition]);
useLayoutEffect(() => {
if (hasMessages && shouldAutoScrollRef.current) pinToBottom();
}, [pinToBottom, hasMessages, isStreaming, messageCount,
lastMessageContent, lastEventCount, composerHeight]);
const handleScroll = useCallback(() => {
const container = containerRef.current;
if (!container) return;
const previous = positionRef.current;
// The browser clamps scrollTop when content shrinks. That is not a user
// 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]);
useEffect(() => {
if (!hasMessages) return;
const container = containerRef.current;
if (!container) return;
let rafId = 0;
const schedule = () => {
if (rafId) return;
rafId = requestAnimationFrame(() => {
rafId = 0;
if (shouldAutoScrollRef.current) pinToBottom();
else rememberPosition(container);
});
};
// A scrollport's own box does not grow with scrollHeight. Observe its
// content children too: main chat has a wrapper, PartnerChat has rows.
const observed = new Set<Element>();
const resize = new ResizeObserver(schedule);
const observeContent = () => {
const targets = new Set<Element>([container, ...container.children]);
for (const target of observed) {
if (!targets.has(target)) { resize.unobserve(target); observed.delete(target); }
}
for (const target of targets) {
if (!observed.has(target)) { resize.observe(target); observed.add(target); }
}
schedule();
};
const mutations = new MutationObserver(observeContent);
mutations.observe(container, { childList: true, subtree: true });
container.addEventListener("load", schedule, true);
observeContent();
const release = () => { shouldAutoScrollRef.current = false; };
const onWheel = (event: WheelEvent) => { if (event.deltaY < 0) release(); };
let touchY = 0;
const onTouchStart = (event: TouchEvent) => { touchY = event.touches[0]?.clientY ?? 0; };
const onTouchMove = (event: TouchEvent) => {
const y = event.touches[0]?.clientY ?? 0;
if (y - touchY > 4) release();
touchY = y;
};
const onKeyDown = (event: KeyboardEvent) => {
const target = event.target;
if (target instanceof Element && target.closest("input,textarea,[contenteditable='true']")) return;
if (["ArrowUp", "PageUp", "Home"].includes(event.key) || (event.key === " " && event.shiftKey)) release();
};
const onPointerDown = (event: PointerEvent) => {
// Opening a trace/citation is deliberate browsing, not late model output.
// Sending/answering re-arms follow at the existing caller-owned boundary.
const target = event.target;
if (target instanceof Element && target.closest("button,a,summary,[role='button']")) release();
};
container.addEventListener("wheel", onWheel, { passive: true });
container.addEventListener("touchstart", onTouchStart, { passive: true });
container.addEventListener("touchmove", onTouchMove, { passive: true });
container.addEventListener("keydown", onKeyDown);
container.addEventListener("pointerdown", onPointerDown, true);
return () => {
resize.disconnect(); mutations.disconnect();
container.removeEventListener("load", schedule, true);
container.removeEventListener("wheel", onWheel);
container.removeEventListener("touchstart", onTouchStart);
container.removeEventListener("touchmove", onTouchMove);
container.removeEventListener("keydown", onKeyDown);
container.removeEventListener("pointerdown", onPointerDown, true);
if (rafId) cancelAnimationFrame(rafId);
};
}, [hasMessages, pinToBottom, rememberPosition]);
const scrollToBottom = useCallback((_behavior: ScrollBehavior) => {
void _behavior;
pinToBottom();
}, [pinToBottom]);
return { containerRef, endRef, shouldAutoScrollRef, scrollToBottom, handleScroll };
}