Skip to content

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
JedWatson:masterfrom
shxwat:fix/menu-close-on-blur-when-focus-in-menu
Open

fix: don't close menu on blur when focus moves to an element inside the menu#6103
shxwat wants to merge 1 commit into
JedWatson:masterfrom
shxwat:fix/menu-close-on-blur-when-focus-in-menu

Conversation

@shxwat

@shxwat shxwat commented Jul 23, 2026

Copy link
Copy Markdown

Summary

Fixes #5508 — with closeMenuOnSelect={false} (commonly used for a checkbox-style custom multi-select Option), clicking a focusable element inside a custom Option (e.g. a <label>/<input type="checkbox">) moves DOM focus onto that element. This fires onInputBlur on the hidden search input, which unexpectedly closes the menu even though closeMenuOnSelect is false.

Root cause

onInputBlur already 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:

if (this.menuListRef && this.menuListRef.contains(document.activeElement)) {
  this.inputRef!.focus();
  return;
}

The problem is that document.activeElement is 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:

if (
  this.menuListRef &&
  (this.menuListRef.contains(document.activeElement) ||
    this.menuListRef.contains(event.relatedTarget as Node))
) {
  this.inputRef!.focus();
  return;
}

Test plan

  • Added a regression test (Select.test.tsx) that renders an open menu, fires a blur on the input with relatedTarget set to an option element inside the menu, and asserts onMenuClose is not called. Confirmed this test fails without the fix and passes with it.
  • Ran the full Select/Async/AsyncCreatable/Creatable/StateManaged jest suites locally (npx jest packages/react-select/src/__tests__/) — 256 passed, 3 skipped (pre-existing), no regressions.
  • Added a changeset.

…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-bot

changeset-bot Bot commented Jul 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6cb978c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
react-select Patch

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

@codesandbox-ci

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AsyncSelect with multiselect with closeMenuOnSelect unexpectedly closes menu.

1 participant