Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-5· anthropicHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION Accessibility: Focus moves to the wrong trigger when using "Open…

  1. 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:

    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

    1. Go to [Alert Dialog: Controlled mode with multiple triggers](https://base-ui.com/react/components/alert-dialog#controlled-mode-with-multiple-triggers
    2. Using keyboard, move focus to the "open programmatically" button
    3. Press Enter to activate the control and open the dialog
    4. Press Esc or activate the close button to close the dialog
    5. 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.

    Interface

    No new interfaces are introduced.

    ADDITIONAL ADVICE

    Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use git checkout <file> to restore the file to its original state. You can also reset all changes in the repository using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.

    CHECKLIST

    Before submitting your solution, please go over the following checklist and make sure you've done everything:

    • If an error was reported in the issue description, I have successfully reproduced it.
    • If an error was reported in the issue description, I have confirmed that my fix resolves the error.
    • I have thought about edge cases and made sure my fix handles them as well.
    • I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.

    Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!

    (Current directory: /base-ui, current file: none) bash-$

  2. Claude Assistant

    Let me write a temporary reproduction test.

  3. Claude Assistant

    Now let me write a temporary repro test.

  4. Claude Assistant

    Now let me implement the change.

  5. Claude Assistant

    Now add the effect that captures the open-time focused element.

  6. Claude Assistant

    Now insert the capture effect after the return-focus effect (line ~862).

  7. Claude Assistant

    Now update getReturnElement.

  8. Claude Assistant

    Now run the repro test.

  9. Claude Assistant

    Now add the new ref declaration.

  10. Claude Assistant

    Now re-run the repro tests and the full suite with this final version.

  11. Claude Assistant

    Now update the capture logic to use the open event's target.

  12. Claude Assistant

    Summary

    Problem. When a Dialog/Alert Dialog/Popover is opened programmatically (controlled open prop or handle.open(triggerId)) while an associated trigger is set via triggerId, FloatingFocusManager.getReturnElement() always returned the domReference (the associated trigger). Closing the popup therefore moved focus to that trigger instead of the control the user actually activated ("Open programmatically"), violating WCAG 2.4.3.

    Fix (packages/react/src/floating-ui-react/components/FloatingFocusManager.tsx):

    • When the floating element opens, the manager now records the element that had focus at that moment — but only when the reference element was not involved in the opening interaction (checked with activeElement and the target of dataRef.current.openEvent, which covers press/hover/focus opens, including Safari where buttons aren't focused on click).
    • Only connected, tabbable, non-body elements outside the reference/floating elements are tracked; the value is recomputed once per open session (kept across the close→unmount gap so exit animations still work).
    • getReturnElement() prefers that tracked element (for both the boolean default and the fallback of an explicit finalFocus), falling back to the previous behaviour (reference element → last previously focused element) otherwise.

    Checklist

    • Reproduced the reported error: Yes. I wrote temporary browser (chromium/vitest) tests replicating the docs "Controlled mode with multiple triggers" demos for Dialog, Alert Dialog, Popover (plus Menu and handle.open()), and confirmed focus went to the associated trigger ("Open 2"/"Delete") rather than the "Open programmatically" button (5/5 temp tests failed on the unmodified code, verified via git stash).
    • Confirmed the fix resolves the error: Yes. All those temp tests pass after the change (keyboard Enter+Escape flow, Close-button flow, handle.open(), and with an exit animation), while trigger-initiated opens still return focus to their trigger.
    • Edge cases: Handled — hover/focus/press opens (reference element involved → unchanged), Safari click-without-focus (open-event target check), exit animations (tracked element survives until unmount, when focus is actually returned), popups that are re-opened (tracking resets per open session), originating control removed/hidden (falls back to trigger, then to the previously focused element via isConnected + isTabbable re-check), body/non-tabbable focus at open, finalFocus ref/function overrides, and iframes/shadow DOM (ownerDocument(floating), getTarget composed path). I also verified a discovered pitfall: Base UI creates a synthetic Event('base-ui') for imperative changes, so a plain "is there an open event" check was insufficient — the fix checks the event's target relative to the reference element instead.
    • Ran existing tests: Yes. Full @base-ui/react suite in both chromium (6689 passed) and jsdom (6096 passed) environments, plus docs and @base-ui/utils projects, all green; tsc -b, eslint and prettier --check also pass. All temporary test files were removed; only the single source file is modified.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.