Skip to content

Add conditional scrolling to useScrollToBottom hook #956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions components/messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { UIMessage } from 'ai';
import { PreviewMessage, ThinkingMessage } from './message';
import { useScrollToBottom } from './use-scroll-to-bottom';
import { Greeting } from './greeting';
import { memo } from 'react';
import { memo, useEffect } from 'react';
import type { Vote } from '@/lib/db/schema';
import equal from 'fast-deep-equal';
import type { UseChatHelpers } from '@ai-sdk/react';
Expand All @@ -27,12 +27,19 @@ function PureMessages({
reload,
isReadonly,
}: MessagesProps) {
const [messagesContainerRef, messagesEndRef] =
const { containerRef, endRef, scrollToBottom } =
useScrollToBottom<HTMLDivElement>();

useEffect(() => {
if (messages.length <= 0) return;
if (messages[messages.length - 1].role === 'user') {
scrollToBottom();
}
}, [messages, scrollToBottom]);

return (
<div
ref={messagesContainerRef}
ref={containerRef}
className="flex flex-col min-w-0 gap-6 flex-1 overflow-y-scroll pt-4"
>
{messages.length === 0 && <Greeting />}
Expand All @@ -58,10 +65,7 @@ function PureMessages({
messages.length > 0 &&
messages[messages.length - 1].role === 'user' && <ThinkingMessage />}

<div
ref={messagesEndRef}
className="shrink-0 min-w-[24px] min-h-[24px]"
/>
<div ref={endRef} className="shrink-0 min-w-[24px] min-h-[24px]" />
</div>
);
}
Expand Down
47 changes: 37 additions & 10 deletions components/use-scroll-to-bottom.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,58 @@
import { useEffect, useRef, type RefObject } from 'react';
import { useEffect, useRef } from 'react';

export function useScrollToBottom<T extends HTMLElement>(): [
RefObject<T>,
RefObject<T>,
] {
export function useScrollToBottom<T extends HTMLElement>() {
const containerRef = useRef<T>(null);
const endRef = useRef<T>(null);
const shouldScrollRef = useRef(true);

const scrollToBottom = () => {
if (endRef.current) {
endRef.current.scrollIntoView({
behavior: 'instant',
block: 'end',
});
}
};

useEffect(() => {
const container = containerRef.current;
const end = endRef.current;

if (container && end) {
const observer = new MutationObserver(() => {
end.scrollIntoView({ behavior: 'instant', block: 'end' });
const intersectionObserver = new IntersectionObserver(
([entry]) => {
shouldScrollRef.current = entry.isIntersecting;
},
{ threshold: 0 },
);

intersectionObserver.observe(end);

const mutationObserver = new MutationObserver(() => {
if (shouldScrollRef.current) {
scrollToBottom();
}
});

observer.observe(container, {
mutationObserver.observe(container, {
childList: true,
subtree: true,
attributes: true,
characterData: true,
});

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

return [containerRef, endRef];
return {
containerRef,
endRef,
scrollToBottom,
};
}

export type UseScrollToBottomReturn = ReturnType<typeof useScrollToBottom>;