Skip to content

Commit eeed6b4

Browse files
author
Dylan Huang
committed
Enhance auto-scroll functionality in ChatInterface to prevent initial scroll on mount
- Added a reference to track the initial mount state, preventing auto-scrolling on the first render. - Updated the scrolling logic to only trigger after the initial mount when new messages are received, improving user experience during chat interactions.
1 parent dcf2134 commit eeed6b4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

vite-app/src/components/ChatInterface.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ export const ChatInterface = ({ messages }: ChatInterfaceProps) => {
1919
const scrollContainerRef = useRef<HTMLDivElement>(null);
2020
const resizeHandleRef = useRef<HTMLDivElement>(null);
2121
const heightResizeHandleRef = useRef<HTMLDivElement>(null);
22+
const isInitialMountRef = useRef(true);
2223

2324
// Auto-scroll to bottom when new messages come in
2425
useEffect(() => {
25-
if (scrollContainerRef.current) {
26+
// Skip scrolling on initial mount
27+
if (isInitialMountRef.current) {
28+
isInitialMountRef.current = false;
29+
return;
30+
}
31+
32+
// Scroll to bottom when messages change (after initial mount)
33+
if (messages.length > 0 && scrollContainerRef.current) {
2634
scrollContainerRef.current.scrollTo({
2735
top: scrollContainerRef.current.scrollHeight,
2836
behavior: "smooth",

0 commit comments

Comments
 (0)