fix(select): keep scroll position when scrolling content with scroll buttons#3978
Open
thatssoheil wants to merge 1 commit into
Open
fix(select): keep scroll position when scrolling content with scroll buttons#3978thatssoheil wants to merge 1 commit into
thatssoheil wants to merge 1 commit into
Conversation
…buttons When Select.ScrollUpButton/Select.ScrollDownButton are present, the buttons mount and unmount as the user scrolls across the top/bottom thresholds. Each mount re-ran scrollIntoView on the focused (selected) item, snapping the viewport back mid-scroll. The compensation now only runs when keyboard focus moves to a different item, so pointer/wheel/touch scrolling is left alone while keyboard navigation still keeps the active item visible. Fixes radix-ui#3686 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 78feb7b The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a
SelectrendersSelect.ScrollUpButton/Select.ScrollDownButton, scrolling the open listbox with the pointer, wheel, or touch snaps the viewport back to the selected item once you reach the top or bottom of the list. Without the scroll buttons, scrolling works as expected.Fixes #3686.
Cause
The scroll buttons mount and unmount as the user scrolls across the top/bottom thresholds (
SelectScrollUpButtononly renders whilescrollTop > 0,SelectScrollDownButtononly while not at the bottom). On every mount,SelectScrollButtonImplran auseLayoutEffectthat calledscrollIntoView({ block: 'nearest' })ondocument.activeElement— which in an openSelectis the selected item. So each remount during a manual scroll yanked the (just-scrolled-away) selected item back into view.That compensation exists for a reason: when a scroll button first appears it consumes vertical space, which during keyboard navigation could push the active item partly out of view.
Fix
Only re-run the compensation when keyboard focus has moved to a different item. Keyboard navigation moves
document.activeElement; pointer/wheel/touch scrolling does not. The "last scrolled-into-view item" is tracked via a ref lifted into the content context (mirroring the existingsearchRef/shouldExpandOnScrollRefpattern) so it survives the scroll buttons remounting.Tests
Added a
CypressScrollButtonsstory and two e2e tests inSelect.cy.ts:Local checks pass:
tsc --noEmit,oxlint, and the existing Select unit suite (7/7). The new behavior is covered by the Cypress e2e (run in CI). Changeset included (patch).🤖 Drafted with AI assistance (Claude); reviewed, tested, and owned by me.