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 (!scrollRef.current) return;
if (
playgroundScrollBehaves === "instant" ||
trackVisibility.category === "error"
) {
if (trackVisibility.category === "error") {
scrollRef.current.scrollIntoView({
behavior: playgroundScrollBehaves,
});
@ -37,14 +34,22 @@ export function ChatScrollAnchor({
behavior: "smooth",
});
}, 400);
setPlaygroundScrollBehaves("smooth");
} else {
scrollRef.current.scrollIntoView({
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" />;
}

View file

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