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 when dialogs/popovers close with multiple triggers.
- Assistant
Let me explore the packages structure and find focus-related code for dialogs and popovers.
- Assistant
Let me look at how focus is restored when closing dialogs, and how multiple triggers work.
- Assistant
rg is not found, let me use grep instead.
- Assistant
Let me examine the detached triggers tests and focus-return handling in dialog stores.
- Assistant
Let me look at DialogStore and how focus return works, and also FloatingFocusManager.
- Assistant
Let me read the Dialog store, handle, and FloatingFocusManager return-focus logic in detail.
- Assistant
Let me continue reading the file and also look at DialogHandle and the popup store utilities.
- Assistant
I'll inspect documentation demos and related popup utilities for how programmatic open and focus return work.
- Assistant
Let me inspect the alert-dialog controlled multiple-triggers demo next.
- Assistant
Now I'll examine how FloatingFocusManager decides which element receives return focus.
- Assistant
Let me continue reading getReturnElement.
- Assistant
I'll look closer at previously-focused tracking and related tests for focus return.
- Assistant
Let me check how DialogPopup configures returnFocus and related final-focus APIs.
- Assistant
Let me check git history around return-focus logic and any prior notes on this behavior.
- Assistant
Let me look at the PR that fixed return focus when reference disconnects - that might give context. Also let me look at how earlier the preference was.
- Assistant
I'll check Floating UI source conventions and existing focus-return tests for intended preference.
- Assistant
Let me review FloatingFocusManager returnFocus tests and how previously-focused elements are chosen.
- Assistant
I'll inspect effect order and how first-tabbable return focus is resolved.
- Assistant
I'll inspect how controlled open/triggerId state is synced into the popup store.
- Assistant
I'll check how controlled triggerId/open props are applied in the popup store utilities.
- Assistant
Need to fix the path.
- Assistant
Let me look at how openProp syncs and whether openchange events go through for controlled open.
- Assistant
Let me read more of store.ts for selectors related to active trigger.
- Assistant
Let me see how DialogRoot uses controlled props and how active trigger element gets set from triggerIdProp.
- Assistant
Let me look at useRenderDialogRoot.tsx
- Assistant
I'll search for any related issues or comments and examine other return-focus paths.
- Assistant
I'll inspect DialogPopup finalFocus tests and the detached-triggers programmatic-open test next.
- Assistant
Safari may not focus buttons on click — I'll verify how that's handled and whether
domReferenceis preferred for that reason. - Assistant
There's an openInteractionTypeRef I can use; I'll inspect how it's set and used.
- Assistant
I'll dives into how controlled open changes get synced into the floating root context.
- Assistant
Let me read useSyncedFloatingRootContext
- Assistant
When controlled open becomes true via openProp, the floating store's
openis synced. But description is no openchange event with trigger from user click on "Open programmatically" - trigger associa... - Assistant
Let me check how Dialog trigger opens and whether focus is forced onto the trigger.
- Assistant
There's openMethod stored. Let me check useOpenMethodTriggerProps and how initial/final focus uses openMethod.
- Assistant
I need to check the order of the initial-focus and return-focus effects carefully.
- Assistant
Let me check if useClick or something focuses the reference.
- Assistant
Let me look at a different approach. Store the event target or activeElement more carefully when openally changes.
- Assistant
Let me look at dispatchOpenChange and how open change events fire when open is synced.
- Assistant
I'll verify how openEvent is stored and whether FloatingFocusManager can access it.
- Assistant
I'll check imports in FloatingFocusManager and how
contains/getTargetare used. - Assistant
I've identified the root cause: return focus always prefers
domReference(the associated trigger), so programmatic opens from another control restore the wrong element. I'll fix FloatingFocusManager to prefer the true originating control unless the open came from the reference. - Assistant
Let me check the rest of the function to make sure it's complete and the dependency array includes dataRef.
- Assistant
I need to add
dataRefto the dependency array. - Assistant
Let me add dataRef to the dependency array.
- Assistant
Now I'll create a temporary reproduction script and check how tests are run in this project.
- Assistant
I'll add a temporary reproduction script and run the floating focus and dialog focus-related tests.
- Assistant
Running the temporary chromium focus-return test to verify the fix.
- Assistant
The test timed out. Let me try a different approach - maybe run vitest more directly.
- Assistant
Let me try running with VITEST_ENV=chromium more carefully and amin longer timeout.
- Assistant
Need to use pnpm exec or npx
- Assistant
Need to install playwright browsers.
- Assistant
Now let me run the test.
- Assistant
All four passed. Running related existing focus-return tests to check for regressions.
- Assistant
I'll also run the detached-triggers tests for dialog, alert-dialog, and popover.
- Assistant
I'll extend the temporary house tests for handle.open and mouse-click paths.
- Assistant
I had a typo in the test file. Let me fix it.
- Assistant
All edge cases pass. Cleaning up the temp test and simplifying the return-focus selection for readability.
- Assistant
I'll re-run a quick verification and check TypeScript on the changed file.
- Assistant
The temp test was skipped due to the filter; I'll run only that repro without a name filter.
- Assistant
Verifying the final diff and that no temporary files remain.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.