Repository navigation
Description
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.138Link 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.
- Click
Edit. The form mounts and its#nameinput keeps focus. - Click
Reset, then click the search field to open the popper. - Click
Add custom product. Its@selectswaps to the same form and closes the popper in the same
tick. - The form focuses the same input, but
document.activeElementis#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:
- Vue unmounts view one and mounts view two in one flush. The element
lastFocusedElementRef
points at is removed. - View two's
onMountedfocuses#name. The resultingfocusinis ignored, because the trap is
still paused, solastFocusedElementRefkeeps pointing at the removed element. handleMutationsfires, sees the remembered element is gone, and callsfocus(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
fix(FocusScope): confirm focus is orphaned before pulling it to the c…
test(focus-scope): keep shadow-root suite clear of the unovue#2886 te…
fix(focus-scope): trap focus inside a shadow root (#2780)