Accessibility: Focus moves to the wrong trigger when using "Open programmatically" buttons
Current behavior
In the Alert Dialog, Dialog and Popover components, there is a variant that includes the option to use multiple triggers to open the dialogs. When using the "Open Programmatically" button to open the dialog/popup, then closing it, focus moves to a different control than the one that triggered the dialog/popup.
Keyboard focus should move back to the originating control (if available) when closing a dialog as long as a page refresh or redirection is not triggered.
If the control is no longer available, focus should move next to that originating control's position in the DOM.
Recommend keeping track of focus and returning it to the originating control.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work.
For example, if you made a mistake and want to revert your changes, you can use git checkout <file> to restore the file to its original state.
You can also reset all changes in the repository using git reset --hard command.
Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later.
Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands.
However you don't need to use git add or git commit before submitting your changes. In fact,
submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.
CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
If an error was reported in the issue description, I have successfully reproduced it.
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
I have thought about edge cases and made sure my fix handles them as well.
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.
I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /base-ui, current file: none) bash-$
1. If an error was reported in the issue description, I have successfully reproduced it.
Yes. I wrote a temporary test replicating the docs demo ("Controlled mode with multiple triggers" + "Open programmatically" button) for Dialog, Alert Dialog and Popover. In both JSDOM and real Chromium, focus returned to the associated trigger (Open 2 / P2 / Delete-equivalent) instead of the "Open programmatically" button — exactly the reported bug.
2. If an error was reported in the issue description, I have confirmed that my fix resolves the error.
Yes. After the fix, all repro cases pass. I also verified the reverse: with the fix stashed, 4 of my 9 scenario tests failed; with the fix applied, all 9 pass (Dialog controlled programmatic open via mouse and keyboard, Alert Dialog, Popover, imperative handle.open(triggerId) called from a button click, and re-verification that trigger-driven opens still return focus to the trigger).
3. I have thought about edge cases and made sure my fix handles them as well.
Trigger-driven opens (click/keyboard/hover/focus/context-menu/list navigation, etc.) are detected via a real dispatched open event with a target, so the trigger keeps owning focus return — no behavior change for Menu, Select, Combobox, Context Menu, Menubar, etc.
The opener is captured once per open cycle and re-captured for each new open, so two different programmatic openers work, and a subsequent trigger-driven open clears it (no stale opener).
Elements that can't be the opener are rejected: body/documentElement (e.g. defaultOpen on load, imperative opens with nothing focused → falls back to the trigger, as before), the trigger itself or its descendants, and anything inside the popup (protects against effect re-runs while open).
Disconnected openers fall back to the trigger and then to the previously focused element.
Explicit finalFocus/returnFocus values still take priority; the opener is only used for the default behavior.
Verified the initial focus still lands (and stays) inside the popup after a programmatic open.
4. I have run existing tests in the repository that might have been affected and confirmed that they pass.
Yes. Full @base-ui/react suite in Chromium (6690 passed) and JSDOM (6103 passed), plus @base-ui/utils and docs projects — all green. WebKit shows 52 pre-existing failures in the popup suites that are identical with and without my change (verified by stashing). ESLint, Prettier and tsc -b all pass. All temporary test files I created for verification were removed, and no existing tests were modified.
The change is complete. Here's my summary and checklist review.
What was wrong
FloatingFocusManager always returned focus to domReference (the popup's active trigger, resolved from triggerId/activeTriggerElement) when a popup closed. In the "controlled mode with multiple triggers" pattern, an "Open programmatically" button opens the popup while designating another element as the (anchor) trigger, so closing the popup moved focus to that unrelated trigger instead of the button the user actually activated — a WCAG 2.4.3 (Focus Order) failure.
The fix (packages/react/src/floating-ui-react/components/FloatingFocusManager.tsx)
Once per open cycle, capture the element that logically opened the popup:
If the open was caused by a real interaction (the recorded dataRef.current.openEvent has a target — trigger press/hover/focus, context menu, list navigation, etc.), the trigger keeps owning focus return (unchanged behavior).
Otherwise (controlled/programmatic open with no event, or an imperative handle.open() whose synthetic event was never dispatched), the currently focused element is stored as the "external opener" (ignoring body/html, the trigger and its descendants, and anything inside the floating element).
The default return-focus resolution now prefers a still-connected external opener, then falls back to the trigger (domReference), then to the previously focused element — so an explicit finalFocus still wins, and removed openers gracefully fall back.
This automatically covers Dialog, Alert Dialog (shares DialogPopup), Drawer, Popover, Menu, Select and Combobox.
Checklist
Reproduced the reported error: Yes. I wrote a temporary test replicating the docs demo (controlled dialog + multiple detached triggers + "Open programmatically" button) and confirmed focus landed on trigger Open 2 instead of the programmatic button in both JSDOM and Chromium (browser-mode) runs.
Confirmed my fix resolves the error: Yes. After the change the same scenarios pass in JSDOM and Chromium for Dialog, Alert Dialog and Popover, with close via the Close button and via Esc. I also verified (by stashing the fix) that 4 of those scenarios fail without the change, so they actually exercise the bug.
Edge cases: Covered and verified by temporary tests: trigger-driven opens still return focus to the trigger (including right after a programmatic open, i.e. no stale opener); two different programmatic openers in sequence each get focus back; imperative handle.open(id) from a button returns focus to that button, but returns focus to the trigger when nothing was focused (no body/html capture); a removed opener falls back to the trigger; initial focus still moves into the popup and isn't stolen back on a programmatic open (guarded against mid-open effect re-runs); explicit finalFocus still takes priority; hover-opened popups and outside-press dismissal behavior are untouched.
Ran existing tests: Yes. Full @base-ui/react suite passes in JSDOM (6103 passed) and Chromium (6690 passed), plus @base-ui/utils and docs projects. WebKit shows 52 pre-existing failures in the popup suites that are identical with and without my change (verified via git stash). tsc -b, ESLint and Prettier are clean. All temporary test files were removed, so the only change submitted is FloatingFocusManager.tsx.