Skip to content

[popups] Pin dismissal and return focus across popups - #6123

Draft
michaldudak wants to merge 16 commits into
mui:masterfrom
michaldudak:popups/dismissal
Draft

michaldudak wants to merge 16 commits into
mui:masterfrom
michaldudak:popups/dismissal

Conversation

@michaldudak

@michaldudak michaldudak commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

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 useDismiss or 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 PopupDismissal module. 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

  • The shared popup and detached-trigger conformance suites now cover a second Escape after a canceled Escape close, and return focus to a detached trigger when the Root unmounts or the trigger remounts while open. They run for every popup they apply to.
  • Popover, Dialog, Select and the trigger focus guards pin their own cases: trap-focus outside-press timing with and without a backdrop, Escape from a Menu, Select or inline Combobox nested in another popup, and tabbing past a trigger that renders a different element when closed.
  • GLOSSARY.md gains the terms: return focus and outside-press timing.

@michaldudak michaldudak self-assigned this Oct 28, 2026
@pkg-pr-new

pkg-pr-new Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

commit: 4602676

@code-infra-dashboard

code-infra-dashboard Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 0B(0.00%) 0B(0.00%)

Details of bundle changes

Performance

No regressions in 36 benchmarks · ran 3m 16s · details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 4602676
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ae322867193b3d5e506d406
😎 Deploy Preview https://deploy-preview-6123--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@michaldudak michaldudak changed the title [popups] Add a shared PopupDismissal module [popups] Pin dismissal and return focus across popups Oct 28, 2026
@michaldudak michaldudak added scope: all components Widespread work has an impact on almost all components. internal Behind-the-scenes enhancement. Formerly called “core”. labels Oct 28, 2026
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Oct 29, 2026
michaldudak and others added 16 commits October 29, 2026 11:26
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.
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Oct 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal Behind-the-scenes enhancement. Formerly called “core”. scope: all components Widespread work has an impact on almost all components.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant