Preserve chat scroll position while streaming new messages #51
main fromcodething/25696837·merged by juliusmarminge View on GitHubSummary#
- add
isScrollContainerNearBottomutility to detect whether the messages container is close enough to the bottom to auto-scroll - track user scroll intent in
ChatViewwithshouldAutoScrollRefand anonScrollhandler - only auto-scroll on message/worklog updates when the user is near the bottom; keep forced scroll on thread switch
- replace direct
scrollTop/scrollIntoViewcalls with a sharedscrollMessagesToBottomhelper - add focused unit tests for threshold behavior, invalid inputs, and default fallback logic in
chat-scroll.test.ts
Testing#
apps/web/src/chat-scroll.test.ts: verifies near-bottom detection at bottom, within threshold, above threshold, negative threshold clamping, andNaNfallback- Manual check (expected): while a run is streaming, scrolling up in chat should no longer jump back to bottom until user returns near bottom
- Not run: project lint and full test suite
Summary by CodeRabbit#
New Features
- Improved automatic scrolling behavior for chat messages. Chat now intelligently scrolls to the latest message while respecting user scroll position when reading earlier messages.
- Added smooth scrolling transitions for a better visual experience.
Tests
- Added test coverage for scroll behavior validation.
- 1
Greptile Summary
This PR implements smart auto-scroll behavior for the chat interface that respects user scroll position. Previously, the chat would aggressively scroll to bottom on every message update, disrupting users trying to review earlier messages during streaming responses.
Key changes:
- Added
isScrollContainerNearBottomutility to detect if user is within 64px of bottom - Tracks user scroll intent in
shouldAutoScrollRefupdated viaonScrollhandler - Only auto-scrolls on message/worklog updates when user is near bottom
- Always scrolls to bottom on thread switch (correct UX)
- Consolidated scroll logic into
scrollMessagesToBottomhelper - Comprehensive unit tests cover threshold behavior and edge cases
Confidence Score: 5/5
- This PR is safe to merge with no issues identified
- Clean implementation with proper edge case handling, comprehensive tests, and clear UX improvement. No bugs or security concerns identified. Code follows React best practices using refs for non-render state and proper hook dependencies.
- No files require special attention
Important Files Changed
Filename Overview apps/web/src/chat-scroll.test.ts Added comprehensive unit tests for scroll position detection utility with edge cases apps/web/src/chat-scroll.ts New utility to detect when scroll container is near bottom with threshold and fallback handling apps/web/src/components/ChatView.tsx Implemented smart auto-scroll with user intent tracking via shouldAutoScrollRef and scroll handler Flowchart
Last reviewed commit: 0476e50flowchart TD A[User Action] --> B{Thread Switch?} B -->|Yes| C[scrollMessagesToBottom - instant] B -->|No| D{New Message or WorkLog?} D -->|Yes| E{shouldAutoScrollRef.current?} E -->|true| F[scrollMessagesToBottom - smooth] E -->|false| G[No scroll - preserve position] D -->|No| G H[onMessagesScroll Event] --> I[isScrollContainerNearBottom] I --> J{Within 64px of bottom?} J -->|Yes| K[shouldAutoScrollRef = true] J -->|No| L[shouldAutoScrollRef = false] C --> M[Set shouldAutoScrollRef = true] F --> M- Added
Preserve chat scroll position in
ChatViewby auto-scrolling only when near bottom using a 64px threshold#Add
AUTO_SCROLL_BOTTOM_THRESHOLD_PX(64) andisScrollContainerNearBottomin apps/web/src/chat-scroll.ts; updateChatViewto computeshouldAutoScrollRefon scroll and callscrollMessagesToBottomonly when within threshold; add tests in apps/web/src/chat-scroll.test.ts.📍Where to Start#
Start with
isScrollContainerNearBottomin apps/web/src/chat-scroll.ts, then review howChatViewuses it in apps/web/src/components/ChatView.tsx.
Macroscope summarized 0476e50.juliusmarminge merged this7fc53da
juliusmarminge closed this
Sign in to join this conversation.
Walkthrough#
This PR adds a scroll detection utility to determine when a chat scroll container is near the bottom, includes comprehensive tests for the utility, and integrates this logic into ChatView to implement conditional auto-scrolling that respects user scroll position.
Changes#
apps/web/src/chat-scroll.ts,apps/web/src/chat-scroll.test.tsAUTO_SCROLL_BOTTOM_THRESHOLD_PXconstant (64px) andisScrollContainerNearBottom()function to detect scroll proximity. Tests validate threshold handling, negative value clamping, NaN fallback behavior, and distance calculations.apps/web/src/components/ChatView.tsxshouldAutoScrollRefstate that tracks user scroll position and triggers smooth scrolling only when appropriate (near bottom or on thread change).Sequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
main✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉
🧹 Recent nitpick comments
Comment `@coderabbitai help` to get the list of available commands and usage tips.