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
Let me explore the repository structure to understand the codebase.
Assistant
Let me understand the structure. There's a common pattern. Let me look at the dialog root and trigger, and the focus management.
Assistant
Let me look at the popups utilities and the trigger/store mechanisms, focusing on how the active trigger is tracked.
Assistant
Now let me look at how
useImplicitActiveTriggerworks and how the active trigger is set when opening programmatically.Assistant
Now let me look at the focus return mechanism. Let me find where focus is restored to the active trigger on close.
Assistant
Now let me look at where the focus is returned. Let me look at the usePopupViewport and where focus restoration happens on close.
Assistant
Let me look at where focus is restored to the trigger on close - probably in the popup focus management.
Assistant
Now let me look at the FloatingFocusManager to understand how returnFocus works.
Assistant
Let me look at the docs example to understand the structure.
Assistant
Now I understand the issue. When you click "Open programmatically", it sets
triggerIdto'trigger-2'andopentotrue. The dialog opens withactiveTriggerId=trigger-2. So when you close, focus goes back to "Open 2" button (trigger-2), NOT to the "Open programmatically" button.Wait, the issue says focus moves to the "Delete" button. In the alert-dialog demo, the trigger is "Delete". Let me check the alert-dialog demo.
Actually the issue is about returning focus to the originating control. When you click "Open programmatically", the originating control is the "Open programmatically" button itself. But the dialog's
triggerIdis set to one of the triggers (trigger-2). So when closing, focus returns to thedomReferencewhich is the activeTriggerElement (trigger-2 / "Open 2" / "Delete"), not the "Open programmatically" button which actually triggered the open.So the fix: when opening programmatically, we should track the element that actually had focus / initiated the open, and return focus to it on close, rather than returning to the
triggerIdtrigger.Let me look at the alert-dialog demo to confirm.
Assistant
Now let me look at how
domReferenceis derived in FloatingFocusManager.Assistant
Let me look at how
triggerIdPropsyncs toactiveTriggerId.Assistant
Let me look at how
openMethodis set, to distinguish trigger press from programmatic open.Assistant
So
openMethodis set to the interactionType when a trigger is pressed (in DialogTrigger.tsx). When opened programmatically,openMethodstays null.Now, let me look at the DialogTrigger to understand how it opens the dialog and sets the active trigger.
Assistant
Let me check what components use FloatingFocusManager and their returnFocus settings.
Assistant
Let me look at the popover controlled demo to understand its triggerId usage.
Assistant
Let me look at the FloatingFocusManager tests around returnFocus to understand expected behavior.
Assistant
So the test "when true" focuses the reference, clicks it, and expects focus to return to the reference when closed. This is the trigger-press case where domReference === previouslyFocusedElement. So this test would still pass if we prefer previouslyFocusedElement.
Let me think about the nested test "always returns to the reference for nested elements". Let me read that.
Assistant
Let me look at the PopoverTrigger and how openMethod is set for hover.
Assistant
Let me look at more FloatingFocusManager tests to ensure I don't break them.
Assistant
Let me look at PopoverPopup to understand modal and initialFocus for hover popovers.
Assistant
Let me look at the initialFocus effect and its order relative to the returnFocus tracking effect.
Assistant
Let me look at the initialFocus effect that focuses the popup.
Assistant
Now let me look at the returnFocus tracking effect setup more carefully (the one with
addPreviouslyFocusedElement).Assistant
Let me check the test helper
Dialogused in those tests and whether it passes openInteractionType.Assistant
Let me set up a reproduction. First, let me check how tests are run in this repo.
Assistant
Let me write a temporary reproduction test to confirm the bug.
Assistant
These tests run in jsdom. Let me create a temporary reproduction test for the programmatic open case.
Assistant
So
#test-utilsmaps to./test/index.tsin packages/react. Let me check the test-utilsrenderanduser. Let me check the API:const { render, user } = createRenderer();- isuseravailable that way? In the existing tests, they useconst { user } = await render(...)orconst { render } = createRenderer(). Let me check the existing pattern. In DialogRoot.detached-triggers.test.tsx,const { user } = await render(...). In DialogPopup.test.tsx,const { render } = createRenderer()and thenconst { user } = await render(...).So
renderreturns{ user }. Let me fix my temp test to use that pattern.Assistant
Let me rewrite the temp test cleanly.
Assistant
The bug is confirmed. After closing, focus goes to the "Two" button (trigger-2, the triggerId), not the "Open programmatically" button. Now let me apply the fix in FloatingFocusManager.
Assistant
Now let me apply the fix to the boolean case in
getReturnElement.Assistant
Let me run the repro test to verify the fix.
Assistant
The fix works. Now let me test with the Close button (instead of Escape) and also test the popover case. Let me add more scenarios to the temp test.
Assistant
All passing. Now let me run the existing FloatingFocusManager tests and dialog/popover tests to ensure no regressions.
Assistant
One test failed. Let me look at it.
Assistant
The keepMounted test reveals an edge case: when the effect re-runs while focus is inside the floating element,
previouslyFocusedElementwould be an element inside the popup, which is not a valid return target. Let me check thecontainshelper.Assistant
containshandles null safely. Let me update the condition to exclude elements inside the floating element.Assistant
All FloatingFocusManager tests pass now. Let me re-run my repro test and then run the broader dialog/popover test suites.
Assistant
Now let me run the broader dialog, alert-dialog, and popover test suites.
Assistant
All passing. Now let me run the other components that use FloatingFocusManager: select, combobox, menu, drawer, tooltip, preview-card.
Assistant
All passing. Let me also run the floating-ui-react hooks tests and the popups utils tests.
Assistant
Let me check the focus-guard attribute to potentially exclude guards.
Assistant
The guard attribute is
data-base-ui-focus-guard. There'screateAttribute('focus-guard')used in the existing code. Let me check what that produces.Assistant
Let me check the type of
activeElementto ensure my added checks typecheck.Assistant
Let me run a proper typecheck to verify my changes don't introduce type errors.
Assistant
Let me check
getNextTabbableto potentially handle the disconnected originating control case.Assistant
getNextTabbableusesownerDocument(referenceElement).bodyand finds the next tabbable based on the ACTIVE element index, not the referenceElement's position. So it wouldn't reliably find the next tabbable after a disconnected originating control.getTabbableNearElementusestabbable(ownerDocument(referenceElement).body)and the element's position in the list. Let me look at it fully.Assistant
Let me add an alert-dialog case to the temp test to be thorough, then run.
Assistant
All passing. Now let me remove the temporary test file (per instructions, no new tests).
Assistant
Now let me review my final change with git diff.
Assistant
The change is clean and minimal. Let me run the affected test suites one final time to confirm everything passes.
Assistant
Let me also run navigation-menu and context-menu (which use popovers/menus) to be safe.
Assistant
The change is clean and minimal. Let me verify the imports (
getNodeName,contains) are in scope.Assistant
Both
getNodeNameandcontainsare imported, andfloating,domReference, andpreviouslyFocusedElementare all in scope ingetReturnElement. The fix is complete and verified.Let me do a final review of the complete modified section.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.