Repository navigation
[popups] Pin dismissal and return focus across popups - #6123
Draft
michaldudak wants to merge 16 commits into
Draft
michaldudak wants to merge 16 commits into
michaldudak wants to merge 16 commits into
Conversation
commit: |
Bundle size
PerformanceNo regressions in 36 benchmarks · ran 3m 16s · details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
One PopupDismissal per popup, kept in a WeakMap keyed by the store's context like HoverIntent. It holds whether the current interaction started inside the popup's React tree, owns the press, touch and composition state of useDismiss and the interaction and return focus state of the focus manager (as one session per adapter instance, so their lifetimes don't change), and subscribes once to openchange on behalf of both. The return focus policy moves in as pure rules: recordCloseForReturnFocus, resolveReturnFocusElement and returnFocusOnClose. getModalOutsidePressEvent names the sloppy press timing of trap-focus popups. Nothing uses the module yet.
useDismiss keeps its listeners and timers and attaches them at the same times, but its press, touch and composition state is a session the popup's PopupDismissal creates, and it hears accepted closes through the dismissal's single openchange subscriber. Whether an interaction started inside the popup's React tree moves from the interaction context into the dismissal, for useDismiss and the focus manager alike. FloatingFocusManager.test reads it through the dismissal.
FloatingFocusManager keeps its listeners and still returns focus in its effect cleanup, but its interaction and return focus state is a session the popup's PopupDismissal creates, it records closes through the dismissal's single openchange subscriber, and the return focus decision comes from the module's rules (recordCloseForReturnFocus, resolveReturnFocusElement and returnFocusOnClose).
Popover and Dialog get their outsidePressEvent from getModalOutsidePressEvent: a trap-focus popup is dismissed as soon as a mouse press starts outside it, keyed on the Root's modal prop (so a trap-focus Popover without a Close part still qualifies), and a Dialog with a backdrop waits for the click. Select and Combobox keep their own timing.
The guards before and after an open popup's trigger, and the guard at the start of its content, move out of PopoverStore's and MenuStore's contexts into the popup's PopupDismissal. The rules for focusing them are plain functions in useTriggerFocusGuards, the Triggers' adapter, so popups without trigger guards don't bundle them, and each Trigger keeps its own condition for rendering the guards. PopupDismissal also gets smaller: whether an interaction started inside the React tree is a plain field, the dismiss and focus return sessions come from module functions, and it no longer multiplexes openchange: useDismiss and the focus manager subscribe to it directly, as before. The dismissal keeps no store.
Pins today's dismissal and return focus where coverage was thin. All of these pass before the PopupDismissal module too. - A mouse press outside a trap-focus Popover, and a trap-focus Dialog without a backdrop, closes it on pointerdown, so aria-hidden is gone before the press can move focus outside. - Focus returns to a detached Popover trigger when the Root unmounts while open, and to the owning trigger after it remounts while open. - Escape closes only a Menu nested in a Popover with focus in the Menu, and only a Select nested in a Dialog, returning focus to their triggers. Escape from an inline Combobox closes the Popover around it, pinned as current behavior. - Select and Combobox still close on Escape, with today's focus, after a canceled Escape close. - Tabbing forward out of a Popover or Menu onto the guard after its trigger focuses the element after the trigger, even when the closed trigger renders a different element.
useDismiss and FloatingFocusManager keep their press, touch and return-focus state in their own refs again, insideReactTree lives on the interaction context, and the trigger focus guards stay with the stores that own them, so the PopupDismissal module goes away. The trap-focus outside press timing that Popover and Dialog share, getModalOutsidePressEvent, moves next to useDismiss.
Popover and Dialog spell out their outside press timing at the useDismiss call again; a shared helper cost more bytes than the literal it replaced.
Popups that take focus now check in popupConformanceTests that a second Escape after a canceled one returns focus to the trigger, in place of the Select and Combobox copies.
The trap-focus outside press tests sit with each file's outside press tests and say what they show: the popup closes on pointerdown, before the click. Dialog also checks that a trap-focus dialog with a backdrop waits for the click. The nested Select Escape test runs inside both a Popover and a Dialog, next to the existing Popover case. It and the nested Menu test check the parent's onOpenChange, which a closing parent would call.
detachedTriggersConformanceTests checks, for popups that move focus into the popup, that focus returns to a detached trigger when the root unmounts while open, and to the trigger that owns the popup after it remounts. Popover, Dialog, AlertDialog and Menu run them.
…ement The trigger focus guard test reads the trigger after opening, so it compares against the element the guards were placed around, and also covers Shift+Tab.
Escape from a default, modal Select inside a Dialog closes only the Select and returns focus to its trigger, in both test environments.
michaldudak
force-pushed
the
popups/dismissal
branch
from
October 29, 2026 10:40
048a5f5 to
4602676
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pins how popups dismiss and return focus across popup families, and names the terms in
GLOSSARY.md. No runtime code changes.Problem
Dismissal and return focus behave slightly differently in each popup family, and several of those differences were only implied by the code: the outside-press timing of trap-focus popups, Escape inside a popup nested in a popup of another family, a canceled Escape close, and return focus to a detached trigger that unmounts or remounts while its popup is open. A later change to
useDismissor the focus manager could alter them without anyone noticing.An earlier version of this PR also moved the dismissal and return-focus state into a shared
PopupDismissalmodule. That part is dropped: #5537 replaces the return-focus decision and the trigger focus guards with its own design, and the module cost about 0.2–0.5 KB gzip per popup without changing behavior.Changes for users
None.
How it works
GLOSSARY.mdgains the terms: return focus and outside-press timing.