From 7c257cddcdc86a0eca726856183f51dec1ee2b84 Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Sat, 29 Nov 2025 14:50:30 -0500 Subject: [PATCH] Use pointer events and timestamp lock for reliable scroll disengagement --- frontend/src/hooks/useAutoScroll.ts | 68 +++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/frontend/src/hooks/useAutoScroll.ts b/frontend/src/hooks/useAutoScroll.ts index ffe7fb30..7bf12c08 100644 --- a/frontend/src/hooks/useAutoScroll.ts +++ b/frontend/src/hooks/useAutoScroll.ts @@ -1,5 +1,7 @@ import { useRef, useEffect, useCallback } from 'react' +const SCROLL_LOCK_MS = 300 + interface MessageInfo { role: string } @@ -27,22 +29,23 @@ export function useAutoScroll({ }: UseAutoScrollOptions): UseAutoScrollReturn { const lastMessageCountRef = useRef(0) const hasInitialScrolledRef = useRef(false) + const userScrolledAtRef = useRef(0) const userDisengagedRef = useRef(false) - const lastScrollTopRef = useRef(0) + const pointerStartYRef = useRef(null) const scrollToBottom = useCallback(() => { if (!containerRef?.current) return + userScrolledAtRef.current = 0 userDisengagedRef.current = false containerRef.current.scrollTop = containerRef.current.scrollHeight - lastScrollTopRef.current = containerRef.current.scrollTop onScrollStateChange?.(false) }, [containerRef, onScrollStateChange]) useEffect(() => { lastMessageCountRef.current = 0 hasInitialScrolledRef.current = false + userScrolledAtRef.current = 0 userDisengagedRef.current = false - lastScrollTopRef.current = 0 }, [sessionId]) useEffect(() => { @@ -50,27 +53,53 @@ export function useAutoScroll({ const container = containerRef.current - const handleWheel = (e: WheelEvent) => { - if (e.deltaY < 0) { - userDisengagedRef.current = true - onScrollStateChange?.(true) + const markDisengaged = () => { + userScrolledAtRef.current = Date.now() + userDisengagedRef.current = true + onScrollStateChange?.(true) + } + + const handlePointerDown = (e: PointerEvent) => { + pointerStartYRef.current = e.clientY + } + + const handlePointerMove = (e: PointerEvent) => { + if (pointerStartYRef.current === null) return + if (e.clientY > pointerStartYRef.current) { + markDisengaged() } } - const handleTouchMove = () => { - const currentScrollTop = container.scrollTop - if (currentScrollTop < lastScrollTopRef.current) { - userDisengagedRef.current = true - onScrollStateChange?.(true) + const handlePointerUp = () => { + pointerStartYRef.current = null + } + + const handleWheel = (e: WheelEvent) => { + if (e.deltaY < 0) { + markDisengaged() + } + } + + const handleKeyDown = (e: KeyboardEvent) => { + if (['PageUp', 'ArrowUp', 'Home'].includes(e.key)) { + markDisengaged() } - lastScrollTopRef.current = currentScrollTop } + container.addEventListener('pointerdown', handlePointerDown, { passive: true }) + container.addEventListener('pointermove', handlePointerMove, { passive: true }) + container.addEventListener('pointerup', handlePointerUp, { passive: true }) + container.addEventListener('pointercancel', handlePointerUp, { passive: true }) container.addEventListener('wheel', handleWheel, { passive: true }) - container.addEventListener('touchmove', handleTouchMove, { passive: true }) + container.addEventListener('keydown', handleKeyDown) + return () => { + container.removeEventListener('pointerdown', handlePointerDown) + container.removeEventListener('pointermove', handlePointerMove) + container.removeEventListener('pointerup', handlePointerUp) + container.removeEventListener('pointercancel', handlePointerUp) container.removeEventListener('wheel', handleWheel) - container.removeEventListener('touchmove', handleTouchMove) + container.removeEventListener('keydown', handleKeyDown) } }, [containerRef, onScrollStateChange]) @@ -95,9 +124,14 @@ export function useAutoScroll({ } } - if (!userDisengagedRef.current) { - scrollToBottom() + const timeSinceUserScroll = Date.now() - userScrolledAtRef.current + const recentlyScrolled = timeSinceUserScroll < SCROLL_LOCK_MS + + if (recentlyScrolled || userDisengagedRef.current) { + return } + + scrollToBottom() }, [messages, containerRef, scrollToBottom]) return { scrollToBottom }