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
- Assistant
I'll start by exploring the repository structure to understand the codebase and locate relevant files. Let's first get a high-level view.
- Assistant
Let's explore the directory structure more systematically.
- Assistant
Let me explore the packages directory structure.
- Assistant
Let me look at the packages structure to understand how the components are organized.
- Assistant
The issue is about focus management in Alert Dialog, Dialog, and Popover components when using "Open programmatically" buttons. Let me look at these components and understand how they handle focus restoration.
Let me first look at the Alert Dialog, Dialog, and Popover components.
- Assistant
Let me look at the root files for all three components.
- Assistant
Now let me look at the dialog root implementation since AlertDialogRoot uses it:
- Assistant
Now let me look at the DialogStore:
- Assistant
Let me look at the trigger component to understand how focus is managed:
- Assistant
Now let me look at the popup utilities that handle focus management:
- Assistant
Let me explore the popup store utilities to understand how focus is managed.
- Assistant
Now let me look at how the dialog handles focus restoration on close. Let me find the DialogInteractions component:
- Assistant
Let me search for where DialogInteractions is defined.
- Assistant
Let me look at the useDialogRoot.ts file to see how DialogInteractions is defined and how focus management works.
- Assistant
Now let me understand the focus management. The issue is that when closing a dialog/popover that was opened programmatically, focus returns to the wrong trigger. Let me look at how focus is managed when closing. I need to find where focus is restored.
- Assistant
Let me search for focus restoration logic.
- Assistant
Let me understand the issue better. The problem is about focus restoration when using "Open programmatically" buttons. When a dialog (Alert Dialog, Dialog, or Popover) is opened programmatically via a handle and then closed, focus should return to the button that opened it, but instead it returns to a different control.
Let me look at the focus management code in the dialog and popover components.
- Assistant
Let me look at how the focus manager handles return focus:
- Assistant
Let me look at how the dialog popup component handles focus management:
- Assistant
The issue is about focus restoration when a dialog/popover is opened programmatically and then closed. When using the "Open Programmatically" button, the dialog opens but when closed, focus should return to the button that opened it (the "open programmatically" button), not to a different trigger (like the "Delete" button).
Let me understand the flow:
- There are multiple triggers (e.g., "Delete" button and "Open Programmatically" button)
- The "Open Programmatically" button uses a handle to open the dialog
- When the dialog is closed, focus should return to the "Open Programmatically" button, not the "Delete" button
The issue seems to be that when a dialog is opened programmatically (via a handle or external control), the active trigger isn't being properly tracked, so when the dialog closes, focus returns to the wrong trigger.
Let me look at the handle implementation and how triggers are registered.
- Assistant
Let me look at the dialog handle to understand how it works.
- Assistant
Let me look at how the "Controlled mode with multiple triggers" example works. Let me check the docs:
- Assistant
Let me search for the "controlled mode with multiple triggers" example code:
- Assistant
Let me search for the demo files that show "Controlled mode with multiple triggers".
- Assistant
Let me search more broadly for the demo code.
- Assistant
Let me look at one of the demo files to understand the pattern.
- Assistant
The path has parentheses. I need to quote it.
- Assistant
Now I understand the issue. Looking at the demo code, I can see the problem:
When the "Open programmatically" button is clicked, it sets
triggerIdto'alert-trigger-2'(the Delete button's id) and opens the dialog. When the dialog closes, focus returns to the trigger with that id (the Delete button), not the "Open programmatically" button.Let me examine how focus restoration works in the
FloatingFocusManager: - Assistant
Let me continue reading the FloatingFocusManager to understand the return focus logic.
- Assistant
Let me search for where the return focus is actually handled in the FloatingFocusManager:
- Assistant
Let me look at how the store handles
triggerIdPropand its relationship toactiveTriggerId: - Assistant
Let me look at the
useControlledPropmethod and howtriggerIdPropis used: - Assistant
Let me search for the
useControlledPropimplementation more broadly. - Assistant
Let me understand the complete flow better. Let me read the store more carefully:
- Assistant
Let me look more closely at the focus restoration logic in
FloatingFocusManager: - Assistant
Let me look more carefully at how
domReferenceis used in focus restoration: - Assistant
Now let me look at how the
domReference(reference element) gets set in the floating context. The issue is that when the dialog opens programmatically via the "open programmatically" button, the reference element is set to the trigger with idalert-trigger-2(the Delete button), but the actual button that was clicked/focused was the "open programmatically" button. Let me look at the useSyncedFloatingRootContext: - Assistant
Let me look at how the
FloatingRootStoreworks: - Assistant
Let me trace through the actual flow when the user clicks "Open programmatically" button:
triggerIdis set to'alert-trigger-2'andopenis set totrue- The store's
triggerIdPropbecomes'alert-trigger-2'
- Assistant
Let me look at the complete return focus logic in FloatingFocusManager:
- Assistant
Let me look at the
addPreviouslyFocusedElementandgetPreviouslyFocusedElementfunctions more closely: - Assistant
Now I understand the issue. Let me also look at how
domReferenceis synced in theuseSyncedFloatingRootContext: - Assistant
Let me look at the existing tests related to this issue:
- Assistant
Let me look at the existing detached-triggers tests:
- Assistant
Let me understand the focus tracking better:
- Assistant
Let me look at the cleanup effect that handles return focus:
- Assistant
Let me look at the
setOpenTriggerStatefunction more carefully: - Assistant
Let me look at the
setOpenmethod inDialogStoreagain: - Assistant
Let me look at how
useSyncedFloatingRootContextdetermines the reference element whentriggerIdPropis set. This is crucial: - Assistant
Now I understand the issue. The problem is in the
getReturnElement()function inFloatingFocusManager.tsx. When a dialog is opened "programmatically" (by settingtriggerIdandopenfrom a non-trigger button), thedomReferenceis set to the registered trigger element (e.g., the "Delete" button), but thepreviouslyFocusedElement(the "Open programmatically" button) should be the return focus target.Let me look at the existing test to understand the issue better:
- Assistant
Let me look at the existing test for "sets the payload when opening programmatically with a controlled triggerId":
- Assistant
Now I have a clear understanding of the issue. The problem is in the
getReturnElement()function inFloatingFocusManager.tsx. When a dialog is opened "programmatically" (by settingtriggerIdandopenfrom a non-trigger button), thedomReferenceis set to the registered trigger element (e.g., the "Delete" button), but focus should return to the element that was actually focused when the dialog opened (the "Open programmatically" button).Let me implement the fix:
- Assistant
Now I understand the issue clearly. Let me also look at the same pattern in the
returnFocusfunction case: - Assistant
Now I understand the issue completely. The problem is in the
getReturnElement()function inFloatingFocusManager.tsx. When a dialog is opened programmatically by clicking a non-trigger button that setstriggerIdandopen, thedomReferenceis the registered trigger element associated with thetriggerId, but thepreviouslyFocusedElement(the button that was actually focused/clicked) should be the return target.Let me check the popover implementation too since it's mentioned in the issue:
- Assistant
Let me look at the Popover popup component to understand how it handles focus restoration.
- Assistant
Now I understand the full picture. The issue is in the
getReturnElement()function inFloatingFocusManager.tsx. When a dialog/popover is opened "programmatically" by clicking a button that setstriggerIdandopen, thedomReferenceis set to the registered trigger element (the one with that ID), but thepreviouslyFocusedElement(the button that was actually clicked) should be the return target.The fix: In
getReturnElement(), whenreturnFocusis true, preferpreviouslyFocusedElementoverdomReferencewhen they differ (indicating a programmatic open from a different element).Let me implement the fix:
- Assistant
Now I understand the issue completely. The
getReturnElement()function always returnsdomReferencewhen it's connected, butdomReferenceis set to the trigger associated with thetriggerIdprop. When using "Open Programmatically" buttons, thetriggerIdpoints to a different trigger (e.g., "Delete" button), but the actual element that had focus when the dialog opened was the "Open programmatically" button (captured aspreviouslyFocusedElement).Let me implement the fix:
- Assistant
Now I understand the issue completely. The problem is in the
getReturnElement()function inFloatingFocusManager.tsx. When a dialog is opened "programmatically" by clicking a button that setstriggerIdandopen(which is not a registered trigger), thedomReferenceis set to the registered trigger element (e.g., the "Delete" button), but thepreviouslyFocusedElement(the "Open programmatically" button that was focused when the dialog opened) should be the return target.Let me implement the fix:
- Assistant
Now let me verify the changes are correct:
- Assistant
Now let me run the existing tests to make sure I haven't broken anything:
- Assistant
Let me check how to run tests in this repository:
- Assistant
Let me look at the test configuration to understand how to run tests.
- Assistant
Let me run the dialog tests to check for regressions:
- Assistant
Most tests are skipped (JSDOM environment). Let me run the FloatingFocusManager test file too:
- Assistant
There's a test failure. Let me look at that test to understand what's happening:
- Assistant
Let me look at the full failing test:
- Assistant
Let me look at the failing test more closely to understand what's happening:
- Assistant
Let me understand the test flow better by looking at the test interactions utility:
- Assistant
Wait, let me first check if this test was already failing before my change:
- Assistant
Good - the test was passing before my change. Let me restore my change:
- Assistant
Let me understand the failing test better. The issue is that my fix changes behavior for the "keepMounted" case. Let me look at the
PopupTriggerMapto see if I can check whether an element is a registered trigger: - Assistant
Now I understand the issue with my fix. The problem is that when focus moves inside the floating element before it closes (like in the keepMounted test), the
previouslyFocusedElementis inside the floating element, and my change incorrectly returns it instead of thedomReference.The fix should only return
previouslyFocusedElementwhen it's outside the floating element (i.e., it was focused before the floating opened, and is not a child of the floating element). Let me update my fix: - Assistant
Let me update my fix to also check that the previously focused element is not inside the floating element:
- Assistant
Let me verify the fix by running the failing test again:
- Assistant
The test passes now. Let me run the full FloatingFocusManager test suite:
- Assistant
All tests pass now. Let me also run the dialog tests:
- Assistant
Good, all tests pass. Now let me run a broader set of tests to make sure I haven't broken anything:
- Assistant
Let me also run the popover tests:
- Assistant
Let me also run the alert dialog tests:
- Assistant
All tests pass. Let me also run the complete FloatingFocusManager test suite again to make sure everything is green:
- Assistant
All 49 tests pass (2 skipped as before). Let me now run a broader set of tests to check for any regressions:
- Assistant
All tests pass. Let me now verify the final state of my changes:
- Assistant
The changes look correct. Let me run the complete test suite one more time to confirm everything is green:
- Assistant
All tests pass. Let me now write a summary of what was done and submit the changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.