Skip to content

Commit

Permalink
Merge pull request ChatGPTNextWeb#5821 from Sherlocksuper/scroll
Browse files Browse the repository at this point in the history
feat: support more user-friendly scrolling
  • Loading branch information
Dogtiti authored Nov 16, 2024
2 parents f22cec1 + 19facc7 commit a2adfbb
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -960,9 +960,24 @@ function _Chat() {
(scrollRef.current.scrollTop + scrollRef.current.clientHeight),
) <= 1
: false;
const isAttachWithTop = useMemo(() => {
const lastMessage = scrollRef.current?.lastElementChild as HTMLElement;
// if scrolllRef is not ready or no message, return false
if (!scrollRef?.current || !lastMessage) return false;
const topDistance =
lastMessage!.getBoundingClientRect().top -
scrollRef.current.getBoundingClientRect().top;
// leave some space for user question
return topDistance < 100;
}, [scrollRef?.current?.scrollHeight]);

const isTyping = userInput !== "";

// if user is typing, should auto scroll to bottom
// if user is not typing, should auto scroll to bottom only if already at bottom
const { setAutoScroll, scrollDomToBottom } = useScrollToBottom(
scrollRef,
isScrolledToBottom,
(isScrolledToBottom || isAttachWithTop) && !isTyping,
);
const [hitBottom, setHitBottom] = useState(true);
const isMobileScreen = useMobileScreen();
Expand Down

0 comments on commit a2adfbb

Please sign in to comment.