From 0cb4e75728ef9e778cb7485a44ee8903c6bda9c7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Feb 2026 16:04:42 -0800 Subject: [PATCH] Preserve chat auto-scroll until user scrolls up - Track last scroll position and content height to avoid disabling tail mode on new messages - Stop auto-scroll immediately on upward mouse wheel input - Keep chat pinned to bottom reliably while streaming --- apps/web/src/components/ChatView.tsx | 30 +++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index de86991812..90aee38189 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -17,6 +17,7 @@ import { type DragEvent, type FormEvent, type KeyboardEvent, + type WheelEvent, memo, type RefObject, useCallback, @@ -402,6 +403,8 @@ export default function ChatView({ threadId }: ChatViewProps) { >(() => readLastInvokedScriptByProjectFromStorage()); const messagesScrollRef = useRef(null); const shouldAutoScrollRef = useRef(true); + const lastKnownScrollTopRef = useRef(0); + const lastKnownScrollHeightRef = useRef(0); const pendingAutoScrollFrameRef = useRef(null); const textareaRef = useRef(null); const composerCommandInputRef = useRef(null); @@ -1101,6 +1104,8 @@ export default function ChatView({ threadId }: ChatViewProps) { const scrollContainer = messagesScrollRef.current; if (!scrollContainer) return; scrollContainer.scrollTo({ top: scrollContainer.scrollHeight, behavior }); + lastKnownScrollTopRef.current = scrollContainer.scrollTop; + lastKnownScrollHeightRef.current = scrollContainer.scrollHeight; shouldAutoScrollRef.current = true; }, []); const requestAutoTailToBottom = useCallback(() => { @@ -1114,7 +1119,29 @@ export default function ChatView({ threadId }: ChatViewProps) { const onMessagesScroll = useCallback(() => { const scrollContainer = messagesScrollRef.current; if (!scrollContainer) return; - shouldAutoScrollRef.current = isScrollContainerAtBottom(scrollContainer); + const currentScrollTop = scrollContainer.scrollTop; + const currentScrollHeight = scrollContainer.scrollHeight; + const atBottom = isScrollContainerAtBottom(scrollContainer); + + if (atBottom) { + shouldAutoScrollRef.current = true; + } else if (shouldAutoScrollRef.current) { + // While tailing, only relinquish control when the user scrolls upward. + const scrolledUp = currentScrollTop < lastKnownScrollTopRef.current - 1; + const contentHeightChanged = + Math.abs(currentScrollHeight - lastKnownScrollHeightRef.current) > 1; + if (scrolledUp && !contentHeightChanged) { + shouldAutoScrollRef.current = false; + } + } + + lastKnownScrollTopRef.current = currentScrollTop; + lastKnownScrollHeightRef.current = currentScrollHeight; + }, []); + const onMessagesWheel = useCallback((event: WheelEvent) => { + if (event.deltaY < 0) { + shouldAutoScrollRef.current = false; + } }, []); useLayoutEffect(() => { if (!activeThread?.id) return; @@ -2061,6 +2088,7 @@ export default function ChatView({ threadId }: ChatViewProps) { ref={messagesScrollRef} className="min-h-0 flex-1 overflow-y-auto px-5 py-4" onScroll={onMessagesScroll} + onWheel={onMessagesWheel} > 0}