fix: preserve scroll position in multi-select on option toggle#143
Open
govindarajug wants to merge 1 commit into
Open
fix: preserve scroll position in multi-select on option toggle#143govindarajug wants to merge 1 commit into
govindarajug wants to merge 1 commit into
Conversation
When selecting or deselecting options in a multi-select dropdown, the menu list would jump back to the top. This happened because the children-change effect unconditionally called scrollToItem(currentIndex), resetting the scroll position on every re-render. This fix tracks the current scroll offset via a ref (updated through react-window's onScroll callback) and restores it after re-render in multi-select mode, while preserving the existing keyboard-navigation scroll behavior for single-select. Fixes jacobworrel#72, jacobworrel#106, jacobworrel#142
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.
Summary
When selecting or deselecting options in a multi-select dropdown, the menu list jumps back to the top. This is a long-standing issue that makes multi-select unusable when working with large option lists.
Root cause: The
useEffectinMenuListthat handles scroll behavior unconditionally callslist.current.scrollToItem(currentIndex)wheneverchildrenchanges. In multi-select mode, every option toggle triggers a children re-render, resetting the scroll to the focused item (often index 0).Fix:
react-window'sonScrollcallbackcurrentIndexChanges
scrollOffsetRefto track scroll position viaList'sonScrollpropuseEffectto checkisMulti— if true and a scroll offset exists, restore it; otherwise fall back to the existingscrollToItembehaviorTest plan
tsc --noEmit --skipLibCheck)Fixes #72, #106, #142