ISSUE DESCRIPTION Accessibility: Focus moves to the wrong trigger when using "Open…
- User
ISSUE DESCRIPTION
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.
See:
- [Alert Dialog: Controlled mode with multiple triggers](https://base-ui.com/react/components/alert-dialog#controlled-mode-with-multiple-triggers
- Dialog: Controlled mode with multiple triggers
- Popover: Controlled mode with multiple triggers
This could cause failures against WCAG 2.2 Success Criterion 2.4.3 Focus Order when implemented.
Expected behavior
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.
Reproducible example
- Go to [Alert Dialog: Controlled mode with multiple triggers](https://base-ui.com/react/components/alert-dialog#controlled-mode-with-multiple-triggers
- Using keyboard, move focus to the "open programmatically" button
- Press Enter to activate the control and open the dialog
- Press Esc or activate the close button to close the dialog
- Note focus has moved back to the "Delete" button and not the "open programmatically" button as expected.
Base UI version
v1.2.0
Which browser are you using?
Chrome 145.0.7632.76
Which OS are you using?
Mac OS Tahoe 26.2
Which assistive tech are you using (if applicable)?
Keyboard
Additional context
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 usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto 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-$
Assistant
Let me write a temporary reproduction test.
Assistant
Now let me write a temporary repro test.
Assistant
Now let me implement the change.
Assistant
Now add the effect that captures the open-time focused element.
Assistant
Now insert the capture effect after the return-focus effect (line ~862).
Assistant
Now update
getReturnElement.Assistant
Now run the repro test.
Assistant
Now add the new ref declaration.
Assistant
Now re-run the repro tests and the full suite with this final version.
Assistant
Now update the capture logic to use the open event's target.
Assistant
Summary
Problem. When a Dialog/Alert Dialog/Popover is opened programmatically (controlled
openprop orhandle.open(triggerId)) while an associated trigger is set viatriggerId,FloatingFocusManager.getReturnElement()always returned thedomReference(the associated trigger). Closing the popup therefore moved focus to that trigger instead of the control the user actually activated ("Open programmatically"), violating WCAG 2.4.3.Fix (
packages/react/src/floating-ui-react/components/FloatingFocusManager.tsx):- When the floating element opens, the manager now records the element that had focus at that moment — but only when the reference element was not involved in the opening interaction (checked with
activeElementand the target ofdataRef.current.openEvent, which covers press/hover/focus opens, including Safari where buttons aren't focused on click). - Only connected, tabbable, non-
bodyelements outside the reference/floating elements are tracked; the value is recomputed once per open session (kept across the close→unmount gap so exit animations still work). getReturnElement()prefers that tracked element (for both the boolean default and the fallback of an explicitfinalFocus), falling back to the previous behaviour (reference element → last previously focused element) otherwise.
Checklist
- Reproduced the reported error: Yes. I wrote temporary browser (chromium/vitest) tests replicating the docs "Controlled mode with multiple triggers" demos for Dialog, Alert Dialog, Popover (plus Menu and
handle.open()), and confirmed focus went to the associated trigger ("Open 2"/"Delete") rather than the "Open programmatically" button (5/5 temp tests failed on the unmodified code, verified viagit stash). - Confirmed the fix resolves the error: Yes. All those temp tests pass after the change (keyboard
Enter+Escapeflow, Close-button flow,handle.open(), and with an exit animation), while trigger-initiated opens still return focus to their trigger. - Edge cases: Handled — hover/focus/press opens (reference element involved → unchanged), Safari click-without-focus (open-event target check), exit animations (tracked element survives until unmount, when focus is actually returned), popups that are re-opened (tracking resets per open session), originating control removed/hidden (falls back to trigger, then to the previously focused element via
isConnected+isTabbablere-check),body/non-tabbable focus at open,finalFocusref/function overrides, and iframes/shadow DOM (ownerDocument(floating),getTargetcomposed path). I also verified a discovered pitfall: Base UI creates a syntheticEvent('base-ui')for imperative changes, so a plain "is there an open event" check was insufficient — the fix checks the event's target relative to the reference element instead. - Ran existing tests: Yes. Full
@base-ui/reactsuite in bothchromium(6689 passed) andjsdom(6096 passed) environments, plusdocsand@base-ui/utilsprojects, all green;tsc -b,eslintandprettier --checkalso pass. All temporary test files were removed; only the single source file is modified.
- When the floating element opens, the manager now records the element that had focus at that moment — but only when the reference element was not involved in the opening interaction (checked with
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →
Qwen/Qwen3.5-35B-A3BCompare side by side →