fix: don't close menu on blur when focus moves to an element inside the menu#6103
Open
shxwat wants to merge 1 commit into
Open
fix: don't close menu on blur when focus moves to an element inside the menu#6103shxwat wants to merge 1 commit into
shxwat wants to merge 1 commit into
Conversation
…he menu
onInputBlur only checked document.activeElement to detect focus moving
to a focusable element inside the menu (e.g. a checkbox in a custom
Option component). This check is not reliable across browsers within a
synchronous blur handler, so the menu would sometimes close even with
closeMenuOnSelect={false}. Also check the blur event's relatedTarget,
which reflects the element about to receive focus.
Closes JedWatson#5508
🦋 Changeset detectedLatest commit: 6cb978c The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
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
Fixes #5508 — with
closeMenuOnSelect={false}(commonly used for a checkbox-style custom multi-selectOption), clicking a focusable element inside a customOption(e.g. a<label>/<input type="checkbox">) moves DOM focus onto that element. This firesonInputBluron the hidden search input, which unexpectedly closes the menu even thoughcloseMenuOnSelectisfalse.Root cause
onInputBluralready had a guard meant to handle exactly this case — refocus the input instead of closing the menu if focus moved to something inside the menu list:The problem is that
document.activeElementis not guaranteed to already reflect the newly-focused element at the point the blur handler runs synchronously — this is inconsistent across browsers/timing, which is why the bug is intermittent and hard to pin down (see the multiple "+1, same issue" reports on #5508 over the last few years).Fix
Also check the blur event's
relatedTarget, which the browser sets to the element about to receive focus, and is reliable for this purpose:Test plan
Select.test.tsx) that renders an open menu, fires abluron the input withrelatedTargetset to an option element inside the menu, and assertsonMenuCloseis not called. Confirmed this test fails without the fix and passes with it.Select/Async/AsyncCreatable/Creatable/StateManagedjest suites locally (npx jest packages/react-select/src/__tests__/) — 256 passed, 3 skipped (pre-existing), no regressions.