Improve scroll detection and add scroll-based header show/hide

This commit is contained in:
Chris Scott committed 2026-05-01 18:41:28 -04:00
1 parent 07b0e1548b
commit fba7c62394
3 files changed
+53 -8

No files matched your search

@@ -1244,7 +1244,7 @@ return (
{isMobile && showScrollButton ? (
<button
onClick={onScrollToBottom}
className="px-4 py-2 rounded-lg bg-muted hover:bg-muted-foreground/20 text-muted-foreground hover:text-foreground transition-all duration-200 active:scale-95 shadow-md flex items-center justify-center min-w-[52px] border border-border"
className="px-4 py-2 rounded-lg bg-black hover:bg-zinc-900 text-white transition-all duration-200 active:scale-95 shadow-md flex items-center justify-center min-w-[52px] border border-zinc-700"
title="Scroll to bottom"
>
<ArrowDown className="w-5 h-5" />
@@ -1252,7 +1252,7 @@ return (
) : !isMobile ? (
<button
onClick={onScrollToBottom}
className={`p-2 rounded-lg bg-muted hover:bg-muted-foreground/20 text-muted-foreground hover:text-foreground transition-all duration-200 active:scale-95 hover:scale-105 shadow-md ${showScrollButton ? 'visible' : 'invisible'}`}
className={`p-2 rounded-lg bg-black hover:bg-zinc-900 text-white transition-all duration-200 active:scale-95 hover:scale-105 shadow-md border border-zinc-700 ${showScrollButton ? 'visible' : 'invisible'}`}
title="Scroll to bottom"
>
<ArrowDown className="w-6 h-6" />
+18 -3
View File
@@ -2,6 +2,8 @@ import { useRef, useEffect, useCallback } from 'react'
import type { Message } from '@/api/types'
const SCROLL_LOCK_MS = 300
const BOTTOM_THRESHOLD_PX = 8
const USER_SCROLL_DELTA_PX = 0.5
interface UseAutoScrollOptions {
containerRef?: React.RefObject<HTMLDivElement | null>
@@ -27,6 +29,7 @@ export function useAutoScroll({
const userScrolledAtRef = useRef(0)
const userDisengagedRef = useRef(false)
const pointerStartYRef = useRef<number | null>(null)
const lastScrollTopRef = useRef(0)
const onScrollStateChangeRef = useRef(onScrollStateChange)
onScrollStateChangeRef.current = onScrollStateChange
@@ -36,6 +39,7 @@ export function useAutoScroll({
userScrolledAtRef.current = 0
userDisengagedRef.current = false
containerRef.current.scrollTop = containerRef.current.scrollHeight
lastScrollTopRef.current = containerRef.current.scrollTop
onScrollStateChangeRef.current?.(false)
}, [containerRef])
@@ -44,6 +48,7 @@ export function useAutoScroll({
hasInitialScrolledRef.current = false
userScrolledAtRef.current = 0
userDisengagedRef.current = false
lastScrollTopRef.current = 0
}, [sessionId])
useEffect(() => {
@@ -52,6 +57,9 @@ export function useAutoScroll({
const markDisengaged = () => {
userScrolledAtRef.current = Date.now()
if (!userDisengagedRef.current) {
onScrollStateChangeRef.current?.(true)
}
userDisengagedRef.current = true
}
@@ -84,9 +92,14 @@ export function useAutoScroll({
const handleScroll = () => {
const { scrollTop, scrollHeight, clientHeight } = container
const isAtBottom = scrollHeight - scrollTop - clientHeight < 150
if (isAtBottom) {
const isUserScrollingUp = scrollTop < lastScrollTopRef.current - USER_SCROLL_DELTA_PX
const isAtBottom = scrollHeight - scrollTop - clientHeight < BOTTOM_THRESHOLD_PX
if (isUserScrollingUp) {
markDisengaged()
}
if (isAtBottom && !isUserScrollingUp) {
if (userDisengagedRef.current) {
userScrolledAtRef.current = 0
userDisengagedRef.current = false
@@ -99,6 +112,8 @@ export function useAutoScroll({
onScrollStateChangeRef.current?.(true)
}
}
lastScrollTopRef.current = scrollTop
}
container.addEventListener('pointerdown', handlePointerDown, { passive: true })
+33 -3
View File
@@ -83,6 +83,8 @@ export function SessionDetail() {
const [showScrollButton, setShowScrollButton] = useState(false);
const [hasPromptContent, setHasPromptContent] = useState(false);
const [minimizedQuestion, setMinimizedQuestion] = useState<QuestionRequest | null>(null);
const [isHeaderVisible, setIsHeaderVisible] = useState(true);
const lastHeaderScrollTopRef = useRef(0);
const isMobile = useMobile();
const { keyboardHeight } = useVisualViewport();
@@ -208,6 +210,34 @@ export function SessionDetail() {
}
}, [sessionId, minimizedQuestion])
useEffect(() => {
const container = messageContainerRef.current
if (!container) return
lastHeaderScrollTopRef.current = container.scrollTop
setIsHeaderVisible(true)
const handleScroll = () => {
const currentScrollTop = container.scrollTop
const previousScrollTop = lastHeaderScrollTopRef.current
const maxScrollTop = container.scrollHeight - container.clientHeight
const isAtBottom = maxScrollTop - currentScrollTop < 24
const isScrollingDown = currentScrollTop > previousScrollTop + 4
const isScrollingUp = currentScrollTop < previousScrollTop - 4
if (isAtBottom || isScrollingDown) {
setIsHeaderVisible(true)
} else if (isScrollingUp) {
setIsHeaderVisible(false)
}
lastHeaderScrollTopRef.current = currentScrollTop
}
container.addEventListener('scroll', handleScroll, { passive: true })
return () => container.removeEventListener('scroll', handleScroll)
}, [repoDirectory, sessionId])
const syncPendingActionsForSession = useCallback(async () => {
if (!repoDirectory || !sessionId) return
await Promise.all([
@@ -420,9 +450,9 @@ export function SessionDetail() {
return (
<div
className="h-dvh max-h-dvh overflow-hidden bg-gradient-to-br from-background via-background to-background flex flex-col"
className="relative h-dvh max-h-dvh overflow-hidden bg-gradient-to-br from-background via-background to-background flex flex-col"
>
<Header>
<Header className={`!absolute left-0 right-0 top-0 !z-20 bg-gradient-to-b from-background from-65% via-background/90 via-80% to-transparent pb-5 transition-all duration-200 ease-out [&_button]:bg-black [&_button]:text-white [&_button]:border-zinc-700 [&_button:hover]:bg-zinc-900 ${isHeaderVisible ? 'translate-y-0 opacity-100 pointer-events-auto' : '-translate-y-full opacity-0 pointer-events-none'}`}>
<div className="flex items-center gap-1.5 sm:gap-3 min-w-0 flex-1">
{session?.parentID ? (
<>
@@ -467,7 +497,7 @@ export function SessionDetail() {
<SessionTodoDisplay sessionID={sessionId} />
<div className="flex-1 overflow-hidden flex flex-col relative">
<div key={sessionId} ref={messageContainerRef} className="flex-1 overflow-y-auto overflow-x-hidden overscroll-contain [mask-image:linear-gradient(to_bottom,transparent,black_16px,black)]" style={{ paddingBottom: promptOverlayHeight + inputBottomOffset + 16 }}>
<div key={sessionId} ref={messageContainerRef} className="flex-1 overflow-y-auto overflow-x-hidden overscroll-contain pt-12 sm:pt-14 [mask-image:linear-gradient(to_bottom,transparent,black_16px,black)]" style={{ paddingBottom: promptOverlayHeight + inputBottomOffset + 16 }}>
{repoLoading || assistantModeLoading || sessionLoading || messagesLoading ? (
<MessageSkeleton />
) : opcodeUrl && repoDirectory ? (