From e4588d9606d33a727a9671e77a9f5092bce4f624 Mon Sep 17 00:00:00 2001 From: Asim Shrestha Date: Fri, 7 Apr 2023 19:14:09 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Scroll=20to=20bottom?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ChatWindow.tsx | 22 +++++++++++++++++++--- src/pages/index.tsx | 10 ++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/components/ChatWindow.tsx b/src/components/ChatWindow.tsx index 0a43b96..6bf6411 100644 --- a/src/components/ChatWindow.tsx +++ b/src/components/ChatWindow.tsx @@ -1,13 +1,23 @@ import cx from "classnames"; import type { ReactNode } from "react"; -import React from "react"; +import React, { useEffect, useRef } from "react"; interface ChatWindowProps { children?: ReactNode; className: string; + messages: Message[]; } -const ChatWindow = ({ children, className }: ChatWindowProps) => { +const ChatWindow = ({ messages, children, className }: ChatWindowProps) => { + const scrollRef = useRef(null); + + useEffect(() => { + // Scroll to bottom on re-renders + if (scrollRef && scrollRef.current) { + scrollRef.current.scrollTop = scrollRef.current.scrollHeight; + } + }); + return (
{ } > -
+
+ {messages.map((message, index) => ( + + ))} {children}
diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 1d02e7b..5330fb5 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -62,14 +62,12 @@ const Home: NextPage = () => {
- - {messages.map((message, index) => ( + + {loading ? ( - ))} - {loading ?
LOADING...
: null} + ) : null}