Skip to content

Commit

Permalink
feat: scroll automatically to the latest message
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Dsouza committed Jun 25, 2024
1 parent cf180e7 commit 9be9a6d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions components/AutomaticScroller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function AutomaticScroller({ children, className }: Props) {
const ref = useRef<null | HTMLDivElement>(null);

useEffect(() => {
const mutationObserver = new MutationObserver(async () => {
const mutationObserver = new MutationObserver(() => {
if (ref.current) {
ref.current.scroll({ behavior: 'smooth', top: ref.current.scrollHeight });
}
Expand All @@ -21,13 +21,19 @@ export default function AutomaticScroller({ children, className }: Props) {
mutationObserver.observe(ref.current, {
childList: true,
});

return () => {
mutationObserver.disconnect();
};
}

return () => {
mutationObserver.disconnect();
};
}, [ref]);

useEffect(() => {
if (ref.current) {
ref.current.scroll({ behavior: 'smooth', top: ref.current.scrollHeight });
}
}, []);

return (
<div ref={ref} className={className}>
{children}
Expand Down

0 comments on commit 9be9a6d

Please sign in to comment.