Skip to content

Commit

Permalink
add scrolling behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
ericcccsliu committed Mar 29, 2024
1 parent 551de1a commit 62897db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
49 changes: 27 additions & 22 deletions app/components/ConversationMessages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,35 +35,40 @@ const ConversationMessages: React.FC<ConversationMessagesProps> = ({
userName = "User",
}) => {
const messagesEndRef = useRef<HTMLDivElement>(null);
// const messagesContainerRef = useRef<HTMLDivElement>(null);

useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, [messages.length]);

// useEffect(() => {
// const messagesContainer = messagesContainerRef.current;
// if (messagesContainer) {
// const scrollHeight = messagesContainer.scrollHeight;
// const scrollTop = messagesContainer.scrollTop;
// const clientHeight = messagesContainer.clientHeight;
// const scrollPosition = scrollTop + clientHeight;
// const scrollThreshold = scrollHeight * 0.9; // Bottom 10% of the page
useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}
},
{
root: null,
rootMargin: "0px",
threshold: 0.01, // Adjust the threshold as needed
}
);

const currentMessagesEndRef = messagesEndRef.current;

if (currentMessagesEndRef) {
observer.observe(currentMessagesEndRef);
}

// if (scrollPosition >= scrollThreshold) {
// messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
// }
// }
// }, [messages]);
return () => {
if (currentMessagesEndRef) {
observer.unobserve(currentMessagesEndRef);
}
};
}, [messages]);

return (
<VStack
spacing={4}
align="stretch"
width="100%"
mb={10}
// ref={messagesContainerRef}
>
<VStack spacing={4} align="stretch" width="100%" mb={10}>
{messages.map((message, index) => (
<Box
key={index}
Expand Down Expand Up @@ -149,7 +154,7 @@ const ConversationMessages: React.FC<ConversationMessagesProps> = ({
</Button>
</Box>
))}
<div ref={messagesEndRef} />
<Box ref={messagesEndRef} height="5px" />
</VStack>
);
};
Expand Down
4 changes: 2 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { useEffect, useState } from "react";
import { useEffect, useState, } from "react";
import { useRouter } from "next/navigation";
import { useSearchParams } from "next/navigation";
import { useCookies } from "react-cookie";
Expand Down Expand Up @@ -156,7 +156,7 @@ export default function Home() {
/>
</Link>
</HStack>
<Center minHeight="calc(100vh - 80px)" mt={16} mb={24}>
<Center minHeight="calc(100vh - 80px)" mt={16} mb={24} >
<VStack
width="60%"
minWidth="500px"
Expand Down

0 comments on commit 62897db

Please sign in to comment.