Merge pull request #8 from chriswritescode-dev/fix-scroll

Fix scroll
This commit is contained in:
Chris Scott authored and GitHub committed 2025-11-29 14:51:01 -05:00
commit f49c23eea7
2 files changed
+68 -26

No files matched your search

+8 -5
View File
@@ -49,7 +49,7 @@ function CodeBlock({ children, className, ...props }: CodeBlockProps) {
return (
<div className="relative">
<pre className={`bg-accent p-4 rounded-lg overflow-x-auto border border-border my-4 ${className || ''}`} {...props}>
<pre className={`bg-accent p-1 rounded-lg overflow-x-auto border border-border my-4 ${className || ''}`} {...props}>
{children}
</pre>
<button
@@ -73,7 +73,7 @@ export function TextPart({ part }: TextPartProps) {
}
return (
<div className="prose prose-invert prose-enhanced max-w-none text-foreground overflow-hidden break-words">
<div className="prose prose-invert prose-enhanced max-w-none text-foreground overflow-hidden break-words leading-snug">
<ReactMarkdown
remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeHighlight, rehypeRaw]}
@@ -101,16 +101,19 @@ export function TextPart({ part }: TextPartProps) {
)
},
p({ children }) {
return <p className="text-foreground">{children}</p>
return <p className="text-foreground my-0.5 md:my-1">{children}</p>
},
strong({ children }) {
return <strong className="font-semibold text-foreground">{children}</strong>
},
ul({ children }) {
return <ul className="list-disc text-foreground">{children}</ul>
return <ul className="list-disc text-foreground my-0.5 md:my-1">{children}</ul>
},
ol({ children }) {
return <ol className="list-decimal text-foreground">{children}</ol>
return <ol className="list-decimal text-foreground my-0.5 md:my-1">{children}</ol>
},
li({ children }) {
return <li className="text-foreground my-0.5 md:my-1">{children}</li>
}
}}
>
+60 -21
View File
@@ -1,6 +1,6 @@
import { useRef, useEffect, useCallback } from 'react'
const SCROLL_THRESHOLD = 100
const SCROLL_LOCK_MS = 300
interface MessageInfo {
role: string
@@ -27,23 +27,25 @@ export function useAutoScroll<T extends Message>({
sessionId,
onScrollStateChange
}: UseAutoScrollOptions<T>): UseAutoScrollReturn {
const isFollowingRef = useRef(true)
const lastMessageCountRef = useRef(0)
const hasInitialScrolledRef = useRef(false)
const isProgrammaticScrollRef = useRef(false)
const userScrolledAtRef = useRef(0)
const userDisengagedRef = useRef(false)
const pointerStartYRef = useRef<number | null>(null)
const scrollToBottom = useCallback(() => {
if (!containerRef?.current) return
isProgrammaticScrollRef.current = true
userScrolledAtRef.current = 0
userDisengagedRef.current = false
containerRef.current.scrollTop = containerRef.current.scrollHeight
isFollowingRef.current = true
onScrollStateChange?.(false)
}, [containerRef, onScrollStateChange])
useEffect(() => {
isFollowingRef.current = true
lastMessageCountRef.current = 0
hasInitialScrolledRef.current = false
userScrolledAtRef.current = 0
userDisengagedRef.current = false
}, [sessionId])
useEffect(() => {
@@ -51,21 +53,54 @@ export function useAutoScroll<T extends Message>({
const container = containerRef.current
const handleScroll = () => {
if (isProgrammaticScrollRef.current) {
isProgrammaticScrollRef.current = false
return
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 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()
}
isFollowingRef.current = false
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight
const isScrolledUp = distanceFromBottom > SCROLL_THRESHOLD
onScrollStateChange?.(isScrolledUp)
}
container.addEventListener('scroll', handleScroll, { passive: true })
return () => container.removeEventListener('scroll', handleScroll)
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('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('keydown', handleKeyDown)
}
}, [containerRef, onScrollStateChange])
useEffect(() => {
@@ -84,15 +119,19 @@ export function useAutoScroll<T extends Message>({
if (currentCount > prevCount) {
const newMessage = messages[currentCount - 1]
if (newMessage?.info.role === 'user') {
isFollowingRef.current = true
scrollToBottom()
return
}
}
if (isFollowingRef.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 }