Preserve chat scroll position while streaming new messages#51
Preserve chat scroll position while streaming new messages#51juliusmarminge merged 1 commit intomainfrom
Conversation
- only auto-scroll when the user is already near the bottom - centralize near-bottom detection in `chat-scroll.ts` with a 64px threshold - add unit tests covering threshold and edge-case behavior Co-authored-by: codex <codex@users.noreply.github.com>
WalkthroughThis 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
Sequence Diagram(s)sequenceDiagram
participant User
participant ChatView
participant ScrollUtil as Scroll Utility
participant DOM
User->>DOM: Scrolls manually
DOM->>ChatView: onScroll event fires
ChatView->>ScrollUtil: isScrollContainerNearBottom()
ScrollUtil->>ScrollUtil: Calculate distance from bottom
ScrollUtil-->>ChatView: Returns boolean
ChatView->>ChatView: Update shouldAutoScrollRef
Note over ChatView: New message arrives
ChatView->>ScrollUtil: isScrollContainerNearBottom()
ScrollUtil-->>ChatView: Returns true (near bottom)
ChatView->>DOM: smoothScrollToBottom()
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Comment |
Greptile SummaryThis 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:
Confidence Score: 5/5
Important Files Changed
Flowchartflowchart 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
Last reviewed commit: 0476e50 |
Preserve chat scroll position in
|
Summary
isScrollContainerNearBottomutility to detect whether the messages container is close enough to the bottom to auto-scrollChatViewwithshouldAutoScrollRefand anonScrollhandlerscrollTop/scrollIntoViewcalls with a sharedscrollMessagesToBottomhelperchat-scroll.test.tsTesting
apps/web/src/chat-scroll.test.ts: verifies near-bottom detection at bottom, within threshold, above threshold, negative threshold clamping, andNaNfallbackSummary by CodeRabbit
New Features
Tests