Skip to content
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

fix: hovering over edit button jumps to bottom of chat #46

Open
wants to merge 1 commit 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
8 changes: 4 additions & 4 deletions components/message-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export function PureMessageActions({
return null;

return (
<TooltipProvider delayDuration={0}>
<div className="flex flex-row gap-2">
<div className="message-actions flex flex-row gap-2 items-center">
<TooltipProvider delayDuration={0}>
<Tooltip>
<TooltipTrigger asChild>
<Button
Expand All @@ -54,8 +54,8 @@ export function PureMessageActions({
</TooltipTrigger>
<TooltipContent>Copy</TooltipContent>
</Tooltip>
</div>
</TooltipProvider>
</TooltipProvider>
</div>
);
}

Expand Down
1 change: 1 addition & 0 deletions components/message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ const PurePreviewMessage = ({
initial={{ y: 5, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
data-role={message.role}
data-message={message.id}
>
<div
className={cn(
Expand Down
42 changes: 34 additions & 8 deletions components/use-scroll-to-bottom.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useEffect, useRef, type RefObject } from 'react';

export function useScrollToBottom<T extends HTMLElement>(): [
RefObject<T>,
RefObject<T>,
] {
export function useScrollToBottom<T extends HTMLElement>(
shouldScroll?: (mutation: MutationRecord, container: T) => boolean
): [RefObject<T>, RefObject<T>] {
const containerRef = useRef<T>(null);
const endRef = useRef<T>(null);

Expand All @@ -12,20 +11,47 @@ export function useScrollToBottom<T extends HTMLElement>(): [
const end = endRef.current;

if (container && end) {
const observer = new MutationObserver(() => {
end.scrollIntoView({ behavior: 'instant', block: 'end' });
const observer = new MutationObserver((mutations) => {
const defaultShouldScroll = (mutation: MutationRecord) => {
// New message added
if (mutation.type === 'childList' &&
mutation.target === container &&
Array.from(mutation.addedNodes).some(node =>
node instanceof Element &&
node.hasAttribute('data-role')
)) {
return true;
}

// Content streaming in
if (mutation.type === 'characterData') {
const target = mutation.target as Node;
const messageElement = target.parentElement?.closest('[data-role="assistant"]');
return !!messageElement;
}

return false;
};

const shouldScrollNow = mutations.some(mutation =>
shouldScroll ? shouldScroll(mutation, container) : defaultShouldScroll(mutation)
);

if (shouldScrollNow) {
end.scrollIntoView({ behavior: 'instant', block: 'end' });
}
});

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

return () => observer.disconnect();
}
}, []);
}, [shouldScroll]);

return [containerRef, endRef];
}