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.
- Assistant
I'll start by exploring the repository structure and understanding how focus is managed for Dialog, Alert Dialog, and Popover components.
- Assistant
Let me explore the dialog and related packages more closely, especially looking for focus management and multiple triggers.
- Assistant
rg is not available, let me use grep.
- Assistant
There's a detached-triggers test which seems related. Let me look at focus restoration code and the store.
- Assistant
Let me look at the DialogStore more carefully and how programmatic open works with focus return.
- Assistant
Let me continue reading the file and also look at DialogHandle and popup store utils.
- Assistant
Let me look at how the demo for "Open programmatically" works and how return focus is handled.
- Assistant
Let me look at the demo and the FloatingFocusManager for return focus, as well as how activeTriggerElement is set.
- Assistant
The demo associates a Dialog.Trigger for payload, so focus returns there instead of the actual originating button. I'll inspect FloatingFocusManager next.
- Assistant
So FloatingFocusManager uses
domReferencefor return focus. ThedomReferenceis the active trigger. When opening programmatically by setting triggerId to a different trigger, the return focus goes... - Assistant
Let me understand the controlled mode open path better. When the user does:
- Assistant
Let me look more carefully at how reference/domReference is set and if there are tests related to focus return with programmatic open. Also check git history/CHANGELOG for related fixes.
- Assistant
Let me look at how activeTriggerElement gets set when controlled triggerId is used. When opening via setOpen(true) + setTriggerId without clicking the trigger.
- Assistant
Let me check existing tests for return focus behavior and look at FloatingFocusManager returnFocus logic more carefully.
- Assistant
There's a test "returns focus to last connected element". Let me also look at how activeTriggerElement is synced based on triggerIdProp whenopeningcontrolledd.
- Assistant
Looking at when activeTriggerElement is set with contolled triggerId, and git history for related focus changes.
- Assistant
Let me look at the PR "Fix return focus when reference disconnects" which seems related, and also DialogRoot test for focus return when trigger unmounts.
- Assistant
Looking at previouslyFocusedElements management and some edge cases more carefully.
- Assistant
We're in detached HEAD. Let me also look at how internal returns might work and look at SETTINGS/AGENTS.
- Assistant
Looking for multi-trigger focus behavior when triggers switch while open.
- Assistant
Looking at the FloatingFocusManager effect vars -
domReferenceis a dependency, so when switching triggers while open, the effect cleans up and re-runs. That cleanup would try to return focus when d...I understand the issue you're describing. TheFloatingFocusManagereffect is re-running cleanup whendomReferencechanges while the dialog is open, which can trigger unwanted focus return logic to the previous trigger.Let me think through this carefully. The cleanup function is trying to handle focus return based on the old
dom reference, but when we switch triggers while the dialog is open, we don't want to return focus to the old trigger at all. Instead, we should only return focus to the trigger that actually opened the dialog.The key insight here is that we need to distinguish between:
- The trigger that initiated the dialog opening ( the one we want to return focus to)
- The current
domReference(which might be different when switching triggers)
- Assistant
A better approach may be storing the element that had focus at open time as
returnFocusElement. Let me check if something like that already exists. - Assistant
Lookingk at floating-ui's original behavior - maybe I should check floating-ui package or if they fixed this.
Actually, re-reading theExpected behavior more carefully:
"Keyboard focus should ...
- Assistant
Interesting! For Popover:
disabled={!mounted || openReason === REASONS.triggerHover}So FloatingFocusManager is disabled when opened by hover! That means the hover→esc tests might be for Navigation Menu style components or the...
- Assistant
Let me look at the Navigation component definition.
- Assistant
Let me look at that Navigation test component and understand how hover opens with return focus.
- Assistant
The import was
../../../test/floating-ui-tests/Navigationfrompackages/react/src/floating-ui-react/components/so that'spackages/react/test/... - Assistant
So for Navigation with hover: initialFocus is false. When hover opens, FFM still captures previously focused and on ESC returns to domReference.
If we change to prefer previouslyfocused, examination when nothing was focused specially, activeE...
- Assistant
Let me look at how when activeTriggerElement is resolved when controlled triggerId isset without going through setOpen. There might be logic that looks up the element by ID.
- Assistant
Let me look at PopoverStore which seems to have special handling for activeTriggerId:
- Assistant
Let me read the setOpen of PopoverStore
- Assistant
Let me look at how floating-ui handles this and also check if there's a
returnFocuselement stored when opening. - Assistant
I'll fix FloatingFocusManager to return focus to the element that had focus when the popup opened, falling back to the reference only if that element is gone.
- Assistant
Let me read the full return focus effect again carefully to edit it properly.
- Assistant
Implementing the fix: capture the originating focused element once when the floating element opens, and prefer it for return focus.
- Assistant
I have a typo "floatinged" - let me fix that and update the return focus effect.
- Assistant
Now update the return focus effect.
- Assistant
Need to navigate to the line first.
- Assistant
Now I can see the lines and edit.
- Assistant
Now update getReturnElement and the cleanup to reset capture.
- Assistant
Need to goto first again.
- Assistant
Now I can edit.
- Assistant
I need to check if
getNodeNameis already imported in FloatingFocusManager. - Assistant
getNodeName is already imported. Running related focus and dialog tests next.
- Assistant
The test timed out. Let me try jsdom first which is faster, or check how to run tests properly.
- Assistant
Tests are timing out. Let me check package.json for test commands.
- Assistant
Let me try running vitest more directly without pipes that might hang.
- Assistant
Let me try running from packages/react with a timeout.
- Assistant
Something is hanging. Let me check what's going on - maybe pnpm is waiting for something.
- Assistant
Ah, corepack is waiting for confirmation to download pnpm. I need to set up the environment properly.
- Assistant
Good. Now let me run the tests.
- Assistant
All FloatingFocusManager tests passed in jsdom. Many focus tests are skipped in jsdom though. Let me try chromium for FloatingFocusManager and Dialog tests.
- Assistant
Chromium/playwright isn't available. Let me run jsdom dialog tests and create a temporary reproduction to verify the fix logic as much as possible.
- Assistant
Good - tests pass. Let me also.try installing playwright browsers for chromium tests, or write a temporary node script... Actually many important tests are skipped in jsdom with isJSDOM.
- Assistant
Good, playwright installed. Now run chromium tests.
- Assistant
Interesting - chromium skips most tests and jsdom runs most. That seems inverted from the usual pattern looking at skipIf(isJSDOM). Let me check - maybe isJSDOM is true even in chromium mode due to how environment works? Or tests that aren't skipIf a...
- Assistant
Excellent! All dialog and popover tests pass including detached-triggers. Let me write a temporary test that reproduces the exact issue to verify the fix.
- Assistant
The repro testpasses. Let me also verify that clicking a regular trigger still returns focus to that trigger, and that handle.open from another button works.
- Assistant
All edge cases pass. Let me also run Menu tests and Alert dialog, and FloatingFocusManager more carefully. Also remove the temporary test file.
- Assistant
All relevant tests pass. Checking formatting and reviewing the final diff.
- Assistant
The fix looks clean. Running a few more shared popup tests that use FloatingFocusManager.
- Assistant
All tests passed. Submitting the fix.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.