[React] FloatingFocusManager returns focus to the old reference when a modal replaces another in the same render (regression of #2607 in 0.27)

Author: andrew-berg-klaviyoCreated Sep 25, 2026Updated Sep 26, 2026
LabelsNEEDS: triage

Describe the bug

When one modal FloatingFocusManager unmounts in the same React commit that another mounts (e.g. a step flow {step === 'a' && <ModalA />}{step === 'b' && <ModalB />}), focus ends up back on modal A's reference — outside modal B — after modal B has already focused its initial element. This worked in 0.26.23.

To Reproduce

Both are forked from the @floating-ui/react issue template. Each shows the currently focused element and the 0.27 failure has a checkbox that applies a returnFocus={false} workaround to modal A.

Steps to reproduce the behavior:

  1. Click "Open modal A" (a modal FloatingFocusManager with default returnFocus).
  2. Click "Continue". Modal A is unmounted in the same render that modal B is mounted; modal B's input has autoFocus.
  3. See that focus is on "Open modal A", behind the open modal B, instead of on modal B's input.

Expected behavior Focus stays on modal B's input, as in 0.26.23.

Screenshots N/A — the sandboxes show the focused element live.

Context:

  • OS: macOS
  • Browser: Chrome
  • Version: @floating-ui/react 0.27.19 and 0.27.20 (0.26.23 is fine); React 18.2 (sandboxes, from the template) and 18.3.1 (also reproduces in our app's jsdom test suite)

Additional context It looks like a regression of the fix for #2607. The unmount path still has the guard:

javascript
// If the focus moved somewhere else after mount, avoid returning focus
// since it likely entered a different element which should be
// respected: https://github.com/floating-ui/floating-ui/issues/2607
tabbableReturnElement !== activeEl && activeEl !== doc.body ? isFocusInsideFloatingTree : true

but in 0.27 the effect that owns this cleanup is useModernLayoutEffect (it was React.useEffect in 0.26.23). As a layout-effect cleanup it now runs before the replacing modal focuses anything, so activeEl is still the element inside the closing modal, isFocusInsideFloatingTree is true, and the queued microtask moves focus back to the old reference after the new modal's initial focus / autoFocus.

It doesn't reproduce if modal A stays mounted and closes via open={false} with a transition: the unmount is then deferred until focus is already in modal B, and the guard correctly skips the return.

Related, but not the same:

  • #2607 / #2613 — the original bug and fix; the guard is still there but no longer effective because of when the cleanup runs.
  • #3337 — also switching between two floating elements, but about leftover inert/aria-hidden cleanup rather than where focus is returned.

Workaround: returnFocus={false} on the closing modal's FloatingFocusManager, restoring focus manually on unmount from a layout-effect cleanup (which runs before the replacing modal's initial focus, so the new modal wins).