fix: make playground scroll not scroll down when not needed (#8676)

Fixed playground scroll

Co-authored-by: Carlos Coelho <80289056+carlosrcoelho@users.noreply.github.com>
This commit is contained in:
Lucas Oliveira 2025-06-23 15:26:41 -03:00 • committed by GitHub
commit eeb7f246fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 8 deletions

View file

@ -24,10 +24,7 @@ export function ChatScrollAnchor({
if (canScroll) { if (canScroll) {
if (!scrollRef.current) return; if (!scrollRef.current) return;
if ( if (trackVisibility.category === "error") {
playgroundScrollBehaves === "instant" ||
trackVisibility.category === "error"
) {
scrollRef.current.scrollIntoView({ scrollRef.current.scrollIntoView({
behavior: playgroundScrollBehaves, behavior: playgroundScrollBehaves,
}); });
@ -37,14 +34,22 @@ export function ChatScrollAnchor({
behavior: "smooth", behavior: "smooth",
}); });
}, 400); }, 400);
setPlaygroundScrollBehaves("smooth");
} else { } else {
scrollRef.current.scrollIntoView({ scrollRef.current.scrollIntoView({
behavior: playgroundScrollBehaves, behavior: playgroundScrollBehaves,
}); });
if (playgroundScrollBehaves === "smooth") {
setPlaygroundScrollBehaves("instant");
setTimeout(() => {
if (!scrollRef.current) return;
scrollRef.current.scrollIntoView({
behavior: "instant",
});
}, 200);
}
} }
} }
}, [canScroll, trackVisibility]); }, [trackVisibility]);
return <div ref={scrollRef} className="h-px w-full" />; return <div ref={scrollRef} className="h-px w-full" />;
} }

View file

@ -191,20 +191,24 @@ export default function ChatView({
if (!messagesRef.current) return; if (!messagesRef.current) return;
const { scrollTop, scrollHeight, clientHeight } = messagesRef.current; const { scrollTop, scrollHeight, clientHeight } = messagesRef.current;
const atBottom = scrollHeight - clientHeight <= scrollTop + 3; const atBottom = scrollHeight - clientHeight <= scrollTop + 30;
if (scrollDir === Direction.Up) { if (scrollDir === Direction.Up) {
setCanScroll(false); setCanScroll(false);
setScrolledUp(true); setScrolledUp(true);
} else { } else {
if (atBottom || !scrolledUp) { if (atBottom && !scrolledUp) {
setCanScroll(true); setCanScroll(true);
} }
setScrolledUp(false); setScrolledUp(false);
} }
}; };
const setPlaygroundScrollBehaves = useUtilityStore(
(state) => state.setPlaygroundScrollBehaves,
);
useEffect(() => { useEffect(() => {
setPlaygroundScrollBehaves("smooth");
setCanScroll(true); setCanScroll(true);
}, [chatHistory?.length]); }, [chatHistory?.length]);