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 understand the issue:
- In Alert Dialog, Dialog, and Popover components, when using controlled mode with multiple triggers, there's a focus management issue.
- When using the "Open programmatically" button to open the dialog/popup, then closing it, focus moves to the wrong trigger (the first trigger in DOM order) instead of the trigger that actually opened the dialog.
- The expected behavior is that focus should return to the originating control (the one that triggered the dialog).
Let me explore the repository structure to understand how these components are implemented.
Let me start by exploring the repository structure and understanding the codebase.
Assistant
Let me explore the packages directory to understand the component structure.
Assistant
This is a monorepo with React packages. Let me explore the react package further.
Assistant
Let me look at the source directory to find Alert Dialog, Dialog, and Popover components
Assistant
Let me look at the structure of the relevant components: alert-dialog, dialog, and popover.
Assistant
Let me look at the root components for dialog and popover, and also check the shared popup.
Assistant
Let me look at the actual files.
Assistant
Let me look at the dialog root file to understand the focus management:
Assistant
I need to use the
opencommand properly.Assistant
Let me look at the actual rendering and focus logic in useRenderDialogRoot
Assistant
The focus management is likely in useDialogRoot.ts. Let me look at it
Assistant
Now let me look at the utility functions in
utils/popups- this is where the focus management likelyAssistant
Let me look at these files, especially
popupTriggerMap.tsandpopupStoreUtils.tsand the index.ts.Assistant
Let me read the popupStoreUtils.ts, popupTriggerMap.ts, and store.ts
Assistant
Now let me look at the popupStoreUtils.ts file.
Assistant
Now let me look at the store.ts file for the popup store.
Assistant
Now let me look at the
useDismisshook fromfloating-ui-reactthat manages focus returnAssistant
Let me look at the
useDismiss.tshookAssistant
Let me search for where focus is managed when closing dialogs/popups
Assistant
The focus management is in the DialogPopup. Let me look at the
FloatingFocusManagerand also theDialogPopup.tsxAssistant
Let me look at the FloatingFocusManager component
Assistant
Let me look at how
domReference(the reference/trigger element) is set in the floating root contextAssistant
Let me check how
domReferenceElementgets set. Let me look at theuseSyncedFloatingRootContexthook.Assistant
Let me look at the DialogStore to understand how setOpen works
Assistant
Let me look at the trigger component for dialog to understand how trigger IDs work.
Assistant
Let me list the directory.
Assistant
Let me look at the DialogTrigger.ts
Assistant
Let me look at the DialogHandle to understand how programmatic opening works
Assistant
Let me look at the useClick hook to understand how the trigger element is set when clicking a
Assistant
Let me look at the detached triggers test for DialogRoot
Assistant
Let me also look at the Popover root and trigger to confirm the same pattern
Assistant
Let me look at how
useControlledPropworks and what happens when openProp changesAssistant
Let me look at the ReactStore's useControlledProp method
Assistant
Let me look at the demo examples to understand the exact setup of "controlled mode with
Assistant
Let me look at the alert-dialog controlled demo to see the setup
Assistant
Let me now look at the specific test for focus return behavior in DialogPopup
Assistant
Let me look at the existing test for focus return
Assistant
Let me look at the full test file for context on default behavior
Assistant
Let me check how AlertDialogRoot works
Assistant
Let me check the PopoverPopup to see if it also uses Floating
Assistant
Let me check if the Menu component also uses FloatingFocusManager
Assistant
Let me look at the exact
getReturnElementfunction to plan theAssistant
Let me look at the full return focus effect code in Floating
Assistant
Let me find where openInteractionTypeRef is
Assistant
Now I have a clear understanding. Let me implement the fix. The issue is in
FloatingFocusManager'sgetReturnElementfunction. When the dialog/popover was not opened by a direct trigger interaction (i.e.,openInteractionTypeis null), focus should return to the previously focused element rather than thedomReference(active trigger element).Let me implement the fix
Assistant
Let me open the file in
Assistant
Now I'll edit the
getReturnElementfunction to prefer the previously focused element when the dialog wasn't opened by a triggerAssistant
Let me use the correct syntax for the
Assistant
Let me try a different syntax for
Assistant
I see there's a duplicate closing brace. Let me fix that
Assistant
Let me remove the duplicate brace
Assistant
Good, the code looks correct now. Let me run the existing tests to make sure this doesn't break anything.
Assistant
Let me look at the failing test to understand the
Assistant
Let me see the rest of the
Assistant
Let me look at the initial focus effect in FloatingFocus
Assistant
Let me look at the initial focus effect
Assistant
Let me look at the handlePointerDown function
Assistant
Let me search for all assignments to preventReturnFocus
Assistant
Let me look at line 539
Assistant
Let me reconsider my approach. The issue with my current fix is that
openInteractionTypedefaults to''when not provided, which is also falsy. Let me use a different condition: prefer the previously focused element only when it differs fromdomReference.Assistant
Let me run
Assistant
Let me check if this test was already failing before my changes by reverting and
Assistant
So the test was passing before my changes. Let me restore my changes and investigate the failure
Assistant
Let me check
useTestInteractionsto understand the test setupAssistant
Let me find and check the useTestInteractions implementation
Assistant
Let me search for the definition of useTestInteractions
Assistant
The function is imported from
#test-utils. Let me find it.Assistant
Let me check the vitest config for the
Assistant
Let me check the shared vitest config for the #test-
Assistant
Let me find the vite config with aliases
Assistant
Let me check the test utils alias
Assistant
Let me check the vitest setup and find where test-utils is defined
Assistant
Let me check the package.json for any alias definitions or check the floating-ui-react test for how test-utils is used
Assistant
Let me find the test-utils file
Assistant
Let me check the test index file
Assistant
Let me check the react package.json for the #test-utils
Assistant
Let me look at the useTestInteractions
Assistant
Let me add some debug logging to understand the test
Assistant
Let me run the failing test to see the debug output
Assistant
Let me check what flushMicrotasks does
Assistant
Let me check the flushMicrotasks implementation
Assistant
Let me add more detailed debugging to understand the flow better
Assistant
Let me revert the file and start fresh with my fix
Assistant
Let me implement a refined fix. I need to check that the previously focused element is NOT inside the floating element before preferring it.
Assistant
Now let me apply the fix with the proper check
Assistant
There's a duplicate brace at line 814. Let me fix it
Assistant
Now let me run the tests again
Assistant
All tests pass. Let me now run the dialog and popover tests to make sure nothing is broken
Assistant
Let me run popover tests too
Assistant
Let me also run the alert-dialog tests
Assistant
All tests pass. Let me run the remaining component tests that use FloatingFocusManager
Assistant
All 1639 tests pass. Let me verify my changes look
Assistant
The diff looks clean. Let me verify there are no TypeScript errors.
Assistant
These are pre-existing errors unrelated to my change. Let me check specifically my file for
Assistant
Let me check the
containsfunction to ensure it handles null safelyAssistant
Let me find the contains function
Assistant
Let me find the contains import
Assistant
Let me check the contains function in shadowDom
Assistant
Let me check the AlertDialog popup to
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.