Skip to content

[Bug]: Dialog's FocusScope steals focus from a freshly mounted input when a Combobox popper closes in the same tick #2886

Closed

Description

@mkeyy0

Environment

Development/Production OS: macOS 26.5.2 (arm64)
Node version: 22.23.2
Package manager: npm@10.9.8
Reka UI version: 2.10.3 (also reproduced on 2.10.1)
Vue version: 3.5.41
Nuxt version: not used
Nuxt mode: not used
Nuxt target: not used
CSS framework: none
Client OS: macOS 26.5.2
Browser: Chrome 151.0.7922.138

Link to minimal reproduction

https://stackblitz.com/edit/vitejs-vite-sp214vnj?file=src%2FApp.vue

Steps to reproduce

A modal DialogRoot swaps its content between a ComboboxRoot view and a form view that focuses its
own input in onMounted. Both the combobox item and a plain Edit button run the same swap.

  1. Click Edit. The form mounts and its #name input keeps focus.
  2. Click Reset, then click the search field to open the popper.
  3. Click Add custom product. Its @select swaps to the same form and closes the popper in the same
    tick.
  4. The form focuses the same input, but document.activeElement is #dialog-content, and typing
    goes nowhere.

The only difference between 1 and 3 is whether a popper was open during the swap. A red outline on
the dialog and a live document.activeElement readout on the page show it without devtools.

Describe the bug

FocusScope moves focus to its container even though focus is, at that moment, already on a valid
element inside that container.

Summary by Claude of the library behaviour that cause this:

The app side is unremarkable — FormView.vue is only this:

const nameInput = useTemplateRef('name-input');

onMounted(() => {
  nameInput.value?.focus();
});

handleMutations in FocusScope.vue decides on the strength of lastFocusedElementRef alone:

function handleMutations(mutations) {
  const lastFocusedElement = lastFocusedElementRef.value;
  if (lastFocusedElement === null) return;
  const anyNodesRemoved = mutations.some(m => m.removedNodes.length > 0);
  if (!anyNodesRemoved) return;
  const isLastFocusedElementExist = container.contains(lastFocusedElement);
  if (!isLastFocusedElementExist) focus(container);
}

lastFocusedElementRef is only written by handleFocusIn, and only while the scope is not
paused
. An open popper pauses it: ComboboxContentImpl renders its own FocusScope, which joins
the global stack and pauses the ancestor trap, and that pause is lifted in the popper scope's
cleanup — inside a setTimeout(..., 0).

So on the failing path:

  1. Vue unmounts view one and mounts view two in one flush. The element lastFocusedElementRef
    points at is removed.
  2. View two's onMounted focuses #name. The resulting focusin is ignored, because the trap is
    still paused, so lastFocusedElementRef keeps pointing at the removed element.
  3. handleMutations fires, sees the remembered element is gone, and calls focus(container) —
    moving focus off #name, which is inside the container and perfectly focusable.

On the control path the trap is never paused, so step 2 updates lastFocusedElementRef and step 3
returns early. The bug is not the tracking itself, but that the handler never asks where focus
actually is.

For comparison, the same handler in @radix-ui/react-focus-scope@1.1.1:

handleMutations = function (mutations) {
  const focusedElement = document.activeElement;
  if (focusedElement !== document.body) return;   // <- no equivalent in reka-ui
  for (const mutation of mutations) {
    if (mutation.removedNodes.length > 0) focus(container);
  }
};

Both are answering "did the browser orphan focus onto body because the focused element was
removed?". Radix checks that condition directly; reka-ui infers it from a remembered element, which
I understand was deliberate (#519, so that document.activeElement lag could not confuse it) but
leaves out the confirmation that focus is orphaned at all. While the trap is paused the remembered
element is stale by design, so the inference cannot be right there.

Relevant history, as far as I can trace it: #518/#519 introduced the tracking, #2546 added the
null early return, and #2752 restored stack membership for scopes without present, which is what
makes a popper pause an ancestor trap again — so this combination looks reachable only since #2752.

Suggested fix. Keep the tracking and restore Radix's guard; getActiveElement is already
imported in the file:

const activeElement = getActiveElement();
if (activeElement && activeElement !== document.body && container.contains(activeElement)) return;

I patched an installed 2.10.3 with exactly this and the reproduction goes from failing to passing,
with the control path unchanged; reverting the patch brings the failure back. Happy to open a PR
with it (plus a test) if you agree with the direction.

Expected behavior

Focusing an element inside the focus trap should hold. FocusScope should restore focus to its
container only when focus was actually orphaned — i.e. when nothing inside the container holds it —
not whenever a previously remembered element happens to be removed.

Context & Screenshots (if applicable)

No response

Activity

  1. added
    bug
    on Aug 20, 2026
  2. added a commit that references this issue on Aug 25, 2026
    f6f75d0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions