Skip to content

Commit a2adfbb

Browse files
authored
Merge pull request ChatGPTNextWeb#5821 from Sherlocksuper/scroll
feat: support more user-friendly scrolling
2 parents f22cec1 + 19facc7 commit a2adfbb

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

app/components/chat.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,9 +960,24 @@ function _Chat() {
960960
(scrollRef.current.scrollTop + scrollRef.current.clientHeight),
961961
) <= 1
962962
: false;
963+
const isAttachWithTop = useMemo(() => {
964+
const lastMessage = scrollRef.current?.lastElementChild as HTMLElement;
965+
// if scrolllRef is not ready or no message, return false
966+
if (!scrollRef?.current || !lastMessage) return false;
967+
const topDistance =
968+
lastMessage!.getBoundingClientRect().top -
969+
scrollRef.current.getBoundingClientRect().top;
970+
// leave some space for user question
971+
return topDistance < 100;
972+
}, [scrollRef?.current?.scrollHeight]);
973+
974+
const isTyping = userInput !== "";
975+
976+
// if user is typing, should auto scroll to bottom
977+
// if user is not typing, should auto scroll to bottom only if already at bottom
963978
const { setAutoScroll, scrollDomToBottom } = useScrollToBottom(
964979
scrollRef,
965-
isScrolledToBottom,
980+
(isScrolledToBottom || isAttachWithTop) && !isTyping,
966981
);
967982
const [hitBottom, setHitBottom] = useState(true);
968983
const isMobileScreen = useMobileScreen();

0 commit comments

Comments
 (0)