A Popover or Select that is open at mount restores focus to body instead of its trigger
Provide a general summary of the issue here
A Popover (in a DialogTrigger) or a Select that is open at mount (defaultOpen) restores focus to <body> when it closes, not to its trigger.
Expected Behavior?
Closing it returns focus to the trigger, as it does when the same popover was opened by pressing the trigger.
Current Behavior
Load without focusing anything, press Escape, wait 600 ms. 5 of 5 runs each:
dialog open at mount (aria-expanded=true), Escape -> focus: BODY
select open at mount (aria-expanded=true), Escape -> focus: BODY
control, dialog opened by clicking the trigger, Escape -> focus: BUTTON#triggerPossible Solution
useRestoreFocus records the active element during render as the node to restore (react-aria/dist/private/focus/FocusScope.mjs:440-443). For a surface open at mount that is <body>, and the trigger is never considered. Falling back to the overlay's trigger when the recorded node is <body> would cover it.
Not the same as #10610 (a race between the restore frame and inert cleanup): nothing races here, and it happens on every run.
Context
Found while building a design system on React Aria Components. Observed in headless Chromium (Playwright 1.63, real key and mouse events) with react-aria-components 1.21.1 / react-aria 3.52.1 / react-stately 3.50.0 and no other code.
️ Steps to Reproduce
import { Button, Dialog, DialogTrigger, ListBox, ListBoxItem, Popover, Select, SelectValue } from "react-aria-components";
export function DialogRepro() {
return (
<DialogTrigger defaultOpen>
<Button>Open</Button>
<Popover><Dialog aria-label="d">Body <button>inside</button></Dialog></Popover>
</DialogTrigger>
);
}
export function SelectRepro() {
return (
<Select aria-label="s" defaultOpen>
<Button><SelectValue /></Button>
<Popover><ListBox><ListBoxItem id="a">a</ListBoxItem><ListBoxItem id="b">b</ListBoxItem></ListBox></Popover>
</Select>
);
}Load the page, press Escape, read document.activeElement.
Version
react-aria-components 1.21.1, react-aria 3.52.1, react-stately 3.50.0, React 19.3.0
What browsers are you seeing the problem on?
Chrome
What operating system are you using?
macOS
Your Company/Team
No response
Tracking Issue
No response
Source: adobe/react-spectrum