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 start by exploring the repository structure to understand the codebase.
Assistant
Let me explore the packages directory to understand the structure better.
Assistant
Let me explore the dialog root directory to understand the focus management.
Assistant
Let me look at the popup component to understand how focus is returned when closing.
Assistant
Let me look at the FloatingFocusManager to understand its
returnFocusbehavior.Assistant
Let me check how
domReferenceis set up since this is what is used for focus return.Assistant
Let me look at the useClick hook and the active trigger to understand how the focus is set up.
Assistant
Let me look at how the dialog is set up when used with multiple triggers.
Assistant
Let me look at how the focus manager handles the active trigger.
Assistant
Now let me look at how the dialog handles the open and focus management. Let me trace what happens when a dialog opens programmatically:
Assistant
Let me look at how the triggerId is handled.
Assistant
Let me look at the test files to understand expectations:
Assistant
Let me look at the entire flow of how the dialog close is handled and how focus returns:
Assistant
Let me check the open change handler:
Assistant
Now let me look at the close functionality to understand where focus returns. The
domReferenceis used to focus when returning. Let me checkgetReturnElement():Assistant
Let me look at how
referenceElementis synced withtriggerId:Assistant
Let me look at the DialogHandle to understand programmatic opening:
Assistant
Let me search for existing focus tracking patterns:
Assistant
Let me check what tests are present to understand the focus flow:
Assistant
Let me look at the useTriggerFocusGuards:
Assistant
Let me try to reproduce the issue first:
Assistant
Let me look at how to test this:
Assistant
Let me write a reproduction script to confirm the bug:
Assistant
Let me search for uses of
activeTriggerElement:Assistant
Let me look at the popper to understand focus:
Assistant
Let me examine the floating-ui code more carefully:
Assistant
Let me implement the fix. First, let me check the imports in FloatingFocusManager:
Assistant
Let me look at the FloatingFocusManager.tsx effect more carefully:
Assistant
Now let me make the modification. I'll add a
originElementRefand modifygetReturnElementto use it:Assistant
Let me check what the relevant cleanup dependencies are:
Assistant
Now let me make the actual changes to the FloatingFocusManager. Let me first plan:
- Add
originElementRefto track the active element when the popup mounted. - Update
getReturnElementto preferoriginElementRef.currentoverdomReference.
- Add
Assistant
Now let me make the modifications. First, I'll add the
originElementRef:Assistant
Now let me think about this more carefully. The right approach is to track the actual originating element when the popup opens (which is the active element at the time of popup mount, captured via
addPreviouslyFocusedElement), and use that for focus return:Assistant
Let me make the modifications. The cleanest fix is to track the originating element per-popup using a ref, and use that for focus return:
Assistant
Now I'll make the changes. The approach:
- Add a per-popup
originElementRefto track the originating element when the popup mounts. - Modify
getReturnElement()to preferoriginElementRef.currentoverdomReference.
Let me look at the imports first to make sure I use the right React APIs:
- Add a per-popup
Assistant
Now I'll make the actual code change. Let me apply the modifications:
Assistant
The replacement worked. Let me check:
Assistant
Let me write the patch to a file:
Assistant
Good, the changes are applied. Now let me run the reproduce test to verify the fix:
Assistant
The file seems to have an issue with the "Block 1 replaced" step. Let me check the file:
Assistant
I see the issue. The "Block 1 replaced." line was printed by the perl script but the sed/awk in bash interpreted
Block 1 replaced.\nas part of the file. Let me remove it.Assistant
The reproduce test now passes. Let me run the existing tests to make sure I haven't broken anything:
Assistant
There's a failing test. Let me examine it to understand what the test expects:
Assistant
Let me look at the test more carefully and check the dismiss behavior:
Assistant
Let me look at the test more carefully:
Assistant
Let me check the useDismiss.ts for the escapeKey:
Assistant
Let me run the failing test with more verbose output:
Assistant
Let me add some debug logging to understand what's happening:
Assistant
Let me examine the initialFocus flow:
Assistant
Let me look at my fix and make it more robust. The issue is the captured origin element is inside the popup if the popup was already focused. Let me make sure we only use the origin if it's outside the popup:
Assistant
Let me update the fix to only capture the origin element if it's not inside the popup:
Assistant
Let me update the fix to check if the previously focused element is outside the popup:
Assistant
The fix works for the existing test. Let me run all the floating-ui tests now:
Assistant
Now let me run the full test suite for dialog, popover, alert-dialog, and reproducer:
Assistant
All tests pass. Let me also run the menu, tooltip, and other popup-related tests:
Assistant
All tests pass. Now let me think about edge cases and write a more comprehensive test:
Assistant
Let me test more edge cases. First, let me also run tests for tooltip, menu, combobox, and select to make sure nothing is broken:
Assistant
All tests pass. Let me also test using a more comprehensive reproducer that matches the issue description exactly:
Assistant
All 4 tests pass. Let me now check edge cases. Let me also test the case where the originating element is no longer in the DOM:
Assistant
Let me clean up the test file (it's a temporary test for my validation) and review my changes:
Assistant
Let me clean up the temporary reproduce test:
Assistant
Let me run all the tests one more time to make sure everything is working:
Assistant
All tests pass. Let me also run the full test suite for the entire packages/react to be thorough:
Assistant
Let me look at the
getFirstTabbableElementfunction:Assistant
Let me check existing utilities for finding tabbable elements near a specific position:
Assistant
Let me check how strict mode is handled in the existing code:
Assistant
Let me also verify the fix is in place by running the reproducer test:
Assistant
The fix is verified to work. Let me clean up the temporary test file:
Assistant
Let me also re-verify the fix is complete by reading the relevant sections:
Assistant
Let me run all tests one more time to confirm everything works:
Assistant
All 6103 tests pass. Let me also check the diff one more time and see the final state:
Assistant
Let me now go through the final checklist before submitting:
Checklist Review:
-
If an error was reported in the issue description, I have successfully reproduced it.
- Yes, I wrote a reproduction test that confirmed the bug exists in the original code (focus returns to the active trigger instead of the "Open programmatically" button).
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.