From 6682a7448e3acbe5f7e4869f4e5921b86de82d04 Mon Sep 17 00:00:00 2001 From: Willy Douhard Date: Sat, 8 Feb 2025 12:34:34 -0800 Subject: [PATCH] fix: scroll down button --- frontend/src/components/MarkdownAlert.tsx | 2 - frontend/src/components/chat/Footer.tsx | 6 -- .../src/components/chat/ScrollContainer.tsx | 59 +++++++++++++++---- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/frontend/src/components/MarkdownAlert.tsx b/frontend/src/components/MarkdownAlert.tsx index 54125ee9d5..acb83d1795 100644 --- a/frontend/src/components/MarkdownAlert.tsx +++ b/frontend/src/components/MarkdownAlert.tsx @@ -174,8 +174,6 @@ const AlertComponent = ({ const style = variantStyles[variant]; const Icon = style.Icon; - // console.log('AlertComponent rendering:', variant, children); - return (
diff --git a/frontend/src/components/chat/Footer.tsx b/frontend/src/components/chat/Footer.tsx index 505ccbbabc..5a42a6e733 100644 --- a/frontend/src/components/chat/Footer.tsx +++ b/frontend/src/components/chat/Footer.tsx @@ -6,7 +6,6 @@ import { FileSpec, useChatMessages } from '@chainlit/react-client'; import WaterMark from '@/components/WaterMark'; import MessageComposer from './MessageComposer'; -import ScrollDownButton from './ScrollDownButton'; interface Props { fileSpec: FileSpec; @@ -22,11 +21,6 @@ export default function ChatFooter({ showIfEmptyThread, ...props }: Props) { return (
- {!props.autoScrollRef.current ? ( - (props.autoScrollRef.current = true)} - /> - ) : null}
diff --git a/frontend/src/components/chat/ScrollContainer.tsx b/frontend/src/components/chat/ScrollContainer.tsx index 5333795db7..5f5418dbca 100644 --- a/frontend/src/components/chat/ScrollContainer.tsx +++ b/frontend/src/components/chat/ScrollContainer.tsx @@ -1,8 +1,11 @@ import { cn } from '@/lib/utils'; -import { MutableRefObject, useEffect, useRef } from 'react'; +import { ArrowDown } from 'lucide-react'; +import { MutableRefObject, useEffect, useRef, useState } from 'react'; import { useChatMessages } from '@chainlit/react-client'; +import { Button } from '@/components/ui/button'; + interface Props { autoScrollRef?: MutableRefObject; children: React.ReactNode; @@ -16,6 +19,22 @@ export default function ScrollContainer({ }: Props) { const ref = useRef(null); const { messages } = useChatMessages(); + const [showScrollButton, setShowScrollButton] = useState(false); + + const scrollToBottom = () => { + if (!ref.current) return; + + ref.current.scrollTo({ + top: ref.current.scrollHeight, + behavior: 'instant' + }); + + if (autoScrollRef) { + autoScrollRef.current = true; + } + + setShowScrollButton(false); + }; useEffect(() => { if (!ref.current || !autoScrollRef?.current) { @@ -25,23 +44,39 @@ export default function ScrollContainer({ }, [messages]); const handleScroll = () => { - if (!ref.current || !autoScrollRef) return; + if (!ref.current) return; const { scrollTop, scrollHeight, clientHeight } = ref.current; const atBottom = scrollTop + clientHeight >= scrollHeight - 10; - autoScrollRef.current = atBottom; + + if (autoScrollRef) { + autoScrollRef.current = atBottom; + } + + setShowScrollButton(!atBottom); }; return ( -
- {children} +
+
+ {children} +
+ {showScrollButton ? ( +
+ +
+ ) : null}
); }