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 what we're working with.
Assistant
Now let me look at theFloatingFocusManager and how it handles focus restoration:
Assistant
Let me search for the domReference handling in the FloatingFocusManager:
Assistant
Now let me see how the floating UI is used by the dialog. I need to understand how the
domReferenceElementis set:Assistant
Now let me look at the detached triggers documentation and how "Open Programmatically" buttons work:
Assistant
Let me look at how the trigger is set when
triggerIdprop is used:Assistant
Let me look at the git log of FloatingFocusManager and the popupTriggerMap:
Assistant
Now let me look at how the
triggerIdprop flows through to determine if it should setactiveTriggerElement:Assistant
Let me look at the actual page demos to understand the structure:
Assistant
Let me check the existing test to understand what currently happens:
Assistant
Let me search for any focus-related tests for the popover/dialog:
Assistant
Let me check what
getPreviouslyFocusedElementreturns by looking at how it gets called:Assistant
Let me check
enqueueFocus:Assistant
Let me check
useTransitionStatus:Assistant
Let me create a script to reproduce the issue first:
Assistant
Let me create a temporary test script to reproduce the issue:
Assistant
Let me create a quick test to reproduce the issue:
Assistant
Let me modify the test to also run in jsdom:
Assistant
Let me check the order of effects in FloatingFocusManager:
Assistant
Let me look at how
useClickinteracts with the store:Assistant
Let me check
useControlledProp:Assistant
Let me check DialogPortal:
Assistant
Let me look at how the open reason is tracked in the store:
Assistant
Let me check if Dialog has
openChangeReason:Assistant
Assistant
Let me look at
openMethod:Assistant
Let me check
useOpenMethodTriggerProps:Assistant
Let me look at
useDismiss:Assistant
Let me check the menu:
Assistant
Let me check popover props:
Assistant
Let me look at the various triggers to see what
stateUpdatesthey pass:Assistant
Let me also check
useImplicitActiveTrigger:Assistant
Now let me implement the fix. First, let me look at the exact area of the fix:
Assistant
Let me check the menu trigger:
Assistant
Menu uses
useOpenInteractionTypeat the root level, not per-trigger. Letme check the tooltip:Assistant
Assistant
Let me check if there are other places that add to the stack:
Assistant
Let me also check where
domReferenceis used in FloatingFocusManager:Assistant
Let me check the FloatingFocusManager tests to understand the expected behavior:
Assistant
Let me find a test that checks focus returning to the reference:
Assistant
Let me look at the teststhat test focus returning to the reference:
Assistant
Let me check
floatingFocusElement:Assistant
Let me check how the floating root context is synced:
Assistant
Let me check if the bug affects tooltip and preview-card too:
Assistant
OK now I have a good understanding of the problem. Let me implement the fix in the
FloatingFocusManager. The fix should:- Prefer the originating element (most recent in
previouslyFocusedElements) overdomReferencefor focus return. - Not add elements inside the popup to the
previouslyFocusedElementsstack (to avoid the popup itself being in the stack).
Let me also look at how
containsis imported:- Prefer the originating element (most recent in
Assistant
OK let me first try the simpler fix in
useTriggerDataForwarding:Assistant
Let me implement the fix in
useTriggerDataForwarding:Assistant
Let me make the fix in
useTriggerDataForwarding.Assistant
Now let me run the test to see if it passes:
Assistant
Let me now also create tests for the related components and scenarios:
Assistant
Let me check the popover trigger code to understand the difference:
Assistant
Let me look at PopoverStore's setOpen:
Assistant
Let me check the actual failure in detail:
Assistant
Let me check what element is actually focused:
Assistant
Let me look at how
setOpenis called in PopoverStore more carefully:Assistant
Let me fix the popover's setOpen to be similar to the dialog's:
Assistant
Now let me run the tests again:
Assistant
All 4 tests pass. Let me also run the existing detached-triggers tests to make sure I didn't break anything:
Assistant
Now let me run the full Dialog and Popover tests:
Assistant
All Popover tests pass. Let me run a broader test suite to make sure I haven't broken anything else:
Assistant
All tests pass. Now let me run the full test suite to make sure nothing else is broken:
Assistant
Let me also check the menu tests are passing:
Assistant
Let me now think about more edge cases. Let me write a test for switching triggers while popup is open:
Assistant
Let me remove the extra test since it's not the core scenario:
Assistant
Let me also verify the test that verifies the original detached-triggers behavior still works:
Assistant
Let me check the existing detached-trigger tests to verify the existing behavior still works:
Assistant
According to the checklist, I should NOT add new tests. Let me remove the test file I added:
Assistant
Let me verify the changes I made:
Assistant
Let me also verify that the menu tests pass (since menu also uses triggerIdProp):
Assistant
Let me run the linter and typescript checks:
Assistant
Hmm, the eslint command is still running or has been killed. Let me check the status and try a different approach:
Assistant
Let me also run typescript check:
Assistant
Let me look at the issue with the TypeScript types:
Assistant
Let me fix the typescript error:
Assistant
TypeScript compiles cleanly. Let me re-run the tests to ensure everything still works:
Assistant
Let me also run a broader test suite to make sure nothing is broken:
Assistant
Let me look at the chromium tests that might be affected:
Assistant
Let me also check the docs example to ensure my fix works there:
Assistant
The docs example works correctly. Let me also verify the AlertDialog test:
Assistant
Let me also test the Popover docs example:
Assistant
Let me check the chromium tests that might be affected:
Assistant
Let me check what happens when triggerId is switched during open state:
Assistant
Let me also check the chromium tests work. First, let me check if my fix has any other places to update:
Assistant
Let me also look at my changes one more time and verify they're correct:
Assistant
Let me check if tooltip's store has
openMethod:Assistant
Let me also run all tests one final time to make sure everything is good:
Assistant
All 6198 tests pass. Let me also run the lint check:
Assistant
Let me also run the TypeScript check:
Assistant
Let me also double-check the final state of my changes:
Assistant
Let me also create one final verification test to make sure my fix works:
Assistant
All 3 tests (Dialog, Popover, AlertDialog) pass. The fix is working correctly.
Let me verify the final state of changes:
Assistant
Let me run the linter on the full project to ensure no issues:
Assistant
Let me also run the menu tests one more time:
Assistant
Let me also look at my final changes once more to make sure they are clean:
Assistant
The changes are clean and minimal. Let me also check thatthe package.json hasn't been modified:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.