Skip to content

Add conditional scrolling to useScrollToBottom hook #956

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

xxxijustwei
Copy link

Description

This PR enhances the useScrollToBottom hook by introducing conditional scrolling behavior. It uses an IntersectionObserver to detect whether the bottom element is in the viewport. Automatic scrolling to the bottom only occurs when the user is near or at the bottom, preventing unwanted scrolling when viewing earlier content (e.g., in a chat or log view).

Changes

  • Added shouldScrollRef to track whether scrolling to the bottom is needed.
  • Implemented IntersectionObserver to monitor the visibility of the bottom element (endRef).
  • Updated scrolling logic to execute scrollIntoView only when the bottom element is visible.
  • Added cleanup for IntersectionObserver in the useEffect return function.

- Introduce IntersectionObserver to detect if the bottom element is in viewport

- Only scroll to bottom when the user is near or at the bottom
Copy link

vercel bot commented Apr 26, 2025

@xxxijustwei is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

- Extract scroll logic into reusable hook
- Add scrollToBottom function for manual control
- Use useEffect to trigger scroll on new user messages
- Rename refs for clarity and consistency
- Update types and return object from hook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant