From 37071cde85c29266193acdbaba8a57ce8ee9c612 Mon Sep 17 00:00:00 2001 From: MK Fain Date: Wed, 3 Dec 2025 20:36:36 -0600 Subject: [PATCH] Fix GlobalChatPane (popup chat) auto-scroll behavior - Scroll to bottom when chat opens using requestAnimationFrame - Track userScrolledUp ref to detect when user manually scrolls up - Auto-scroll follows streaming content by watching lastMessageContent - Reset scroll tracking when clicking scroll-to-bottom button - Reset scroll tracking when chat opens - Only auto-scroll if user hasn't manually scrolled up to read history Also reverted unintended changes to main ChatPane.tsx --- src/components/GlobalChatPane.tsx | 45 +++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/components/GlobalChatPane.tsx b/src/components/GlobalChatPane.tsx index eec990fb..aa1af194 100644 --- a/src/components/GlobalChatPane.tsx +++ b/src/components/GlobalChatPane.tsx @@ -101,6 +101,11 @@ export function GlobalChatPane() { const scrollAreaRef = useRef(null); const textareaRef = useRef(null); + // Track if user has manually scrolled up + const userScrolledUp = useRef(false); + // Track the last message content to detect streaming updates + const lastMessageContent = messages[messages.length - 1]?.content || ''; + // Initialize provider model from settings if not set useEffect(() => { if (!providerModel && settings.recentlyUsedModels?.length) { @@ -108,23 +113,39 @@ export function GlobalChatPane() { } }, [providerModel, settings.recentlyUsedModels, setProviderModel]); - // Auto-scroll to bottom when new messages arrive + // Scroll to bottom when chat opens useEffect(() => { - if (scrollAreaRef.current) { - const container = scrollAreaRef.current; - const isNearBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - 100; - - if (isNearBottom || messages.length <= 1) { - container.scrollTop = container.scrollHeight; - } + if (isOpen && scrollAreaRef.current) { + // Small delay to ensure content is rendered + requestAnimationFrame(() => { + if (scrollAreaRef.current) { + scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight; + } + }); + // Reset scroll tracking when opening + userScrolledUp.current = false; } - }, [messages]); + }, [isOpen]); - // Handle scroll to show/hide scroll-to-bottom button + // Auto-scroll to bottom when messages change or streaming content updates + useEffect(() => { + if (!scrollAreaRef.current || !isOpen) return; + + // Only auto-scroll if user hasn't manually scrolled up + if (!userScrolledUp.current) { + scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight; + } + }, [messages, lastMessageContent, isLoading, isOpen]); + + // Handle scroll to detect user scrolling and show/hide scroll-to-bottom button const handleScroll = useCallback(() => { if (scrollAreaRef.current) { const container = scrollAreaRef.current; - const isNearBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - 100; + const threshold = 100; + const isNearBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - threshold; + + // Track if user scrolled away from bottom + userScrolledUp.current = !isNearBottom; setShowScrollToBottom(!isNearBottom && messages.length > 0); } }, [messages.length]); @@ -132,6 +153,8 @@ export function GlobalChatPane() { const scrollToBottom = useCallback(() => { if (scrollAreaRef.current) { scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight; + // Reset the flag so auto-scroll resumes + userScrolledUp.current = false; } }, []);