Fix auto-scroll using wheel/touch events and reduce mobile text spacing

This commit is contained in:
Chris Scott committed 2025-11-29 14:36:38 -05:00
1 parent d534ef1c6c
commit 1b9bb87858
2 files changed
+34 -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>
}
}}
>
+26 -21
View File
@@ -1,7 +1,5 @@
import { useRef, useEffect, useCallback } from 'react'
const SCROLL_THRESHOLD = 100
interface MessageInfo {
role: string
}
@@ -27,23 +25,24 @@ 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 userDisengagedRef = useRef(false)
const lastScrollTopRef = useRef(0)
const scrollToBottom = useCallback(() => {
if (!containerRef?.current) return
isProgrammaticScrollRef.current = true
userDisengagedRef.current = false
containerRef.current.scrollTop = containerRef.current.scrollHeight
isFollowingRef.current = true
lastScrollTopRef.current = containerRef.current.scrollTop
onScrollStateChange?.(false)
}, [containerRef, onScrollStateChange])
useEffect(() => {
isFollowingRef.current = true
lastMessageCountRef.current = 0
hasInitialScrolledRef.current = false
userDisengagedRef.current = false
lastScrollTopRef.current = 0
}, [sessionId])
useEffect(() => {
@@ -51,21 +50,28 @@ export function useAutoScroll<T extends Message>({
const container = containerRef.current
const handleScroll = () => {
if (isProgrammaticScrollRef.current) {
isProgrammaticScrollRef.current = false
return
const handleWheel = (e: WheelEvent) => {
if (e.deltaY < 0) {
userDisengagedRef.current = true
onScrollStateChange?.(true)
}
isFollowingRef.current = false
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight
const isScrolledUp = distanceFromBottom > SCROLL_THRESHOLD
onScrollStateChange?.(isScrolledUp)
}
const handleTouchMove = () => {
const currentScrollTop = container.scrollTop
if (currentScrollTop < lastScrollTopRef.current) {
userDisengagedRef.current = true
onScrollStateChange?.(true)
}
lastScrollTopRef.current = currentScrollTop
}
container.addEventListener('scroll', handleScroll, { passive: true })
return () => container.removeEventListener('scroll', handleScroll)
container.addEventListener('wheel', handleWheel, { passive: true })
container.addEventListener('touchmove', handleTouchMove, { passive: true })
return () => {
container.removeEventListener('wheel', handleWheel)
container.removeEventListener('touchmove', handleTouchMove)
}
}, [containerRef, onScrollStateChange])
useEffect(() => {
@@ -84,13 +90,12 @@ 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) {
if (!userDisengagedRef.current) {
scrollToBottom()
}
}, [messages, containerRef, scrollToBottom])