Skip to content

[popups] Apply the same open-change rules to every popup - #6111

Merged
michaldudak merged 27 commits into
mui:masterfrom
michaldudak:popups/unify-open-changes
Oct 29, 2026
Merged

michaldudak merged 27 commits into
mui:masterfrom
michaldudak:popups/unify-open-changes

Conversation

@michaldudak

@michaldudak michaldudak commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Every popup Root now follows the same open-change rules. The shared store already ran one sequence for every popup, but some stores still overrode steps of it in ways that their interactions didn't need.

Problem

BasePopupStore.setOpen runs every open change in one order: guards, onOpenChange, stop if canceled, notify the interactions, then commit in one update. Several stores drifted from that order or from each other:

  • Menu ignored the delayed touch click that some mobile browsers fire after a focus-open, but only after onOpenChange and the interactions had heard about it. A controlled menu closed on a tap it meant to ignore.
  • Popover wrote instantType in a second update, so a hover open rendered once with the previous data-instant.
  • Dialog wrote preventUnmountOnClose() to the store as soon as it was called, even for a canceled close, so the next close never unmounted. The other popups dropped a canceled close's request but ignored a call made after an await.
  • Popover, Dialog, Tooltip and Preview Card reported a close while already closed. Menu and Combobox didn't. The Tooltip delay group relied on that redundant close to cut short an exiting tooltip's animation.
  • The trigger reported for a close request without one depended on the popup: none, only for Popover.Close, or always.

Changes for users

No API changes, but two parts of the onOpenChange contract change: it's no longer called for a close while the popup is already closed, and eventDetails.trigger is now set for closes that name no trigger. Use eventDetails.reason to tell close causes apart.

Behavior Before After
Controlled Menubar menu: delayed touch click right after a focus-open onOpenChange(false) is called and the menu closes Ignored, the menu stays open
Dialog family (Dialog, Alert Dialog, Drawer): preventUnmountOnClose() then cancel(), followed by a normal close The popup never unmounts The popup unmounts
preventUnmountOnClose() called after an await in onOpenChange The Dialog family only Every popup, controlled or not
Select, Combobox, Autocomplete: preventUnmountOnClose() on a close the parent declined without cancel(), followed by a close that doesn't call it The popup unmounts The popup stays mounted until it opens again, as the other popups already did. Decline with cancel()
onOpenChange for a close while the popup is closed, such as actions.close() or handle.close(), leaving a Tooltip trigger before its delay ends, or the delay group closing a tooltip that is already closing (Popover, the Dialog family, Tooltip, Preview Card, Select) Called Not called
eventDetails.trigger for an Escape, outside press or other close that names no trigger (Popover, Tooltip, Preview Card, Select, Combobox) undefined, or for Popover the active trigger with Popover.Close only The trigger the popup was opened from, if it's still in the document
Menu: actions.close() while a hover open is pending The pending open still happens It's cancelled
Controlled Menu or Select: a close after the parent declined an open without cancel() Not reported Reported. The store can't tell a declined open from one the parent applies later
Popover opened by hover after an Escape close Renders once with data-instant="dismiss" Renders with the hover's instantType

Changes

  • BasePopupStore drops a close request when both the store's last request and the open prop say the popup is closed. onOpenChange doesn't hear about it and no openchange is emitted, but the hover interaction still cancels a pending hover open. A change a store won't commit belongs in isOpenChangeIgnored, which runs before onOpenChange.
  • Menu's touch-click guard moved into isOpenChangeIgnored, and the isOpenChangeDropped step is gone.
  • Popover returns instantType from prepareOpenChange, and the completeOpenChange step is gone.
  • preventUnmountOnClose() is handled once in BasePopupStore. A call made during onOpenChange is committed with the close. A call made after the commit applies until the popup opens again, even if more closes commit in the meantime, and a call on an open change or a canceled close is ignored. The request lasts until the next open, as it already did for every popup but Select and Combobox, so a parent declines a close with cancel(). Dialog's and ValuePopupStore's overrides are removed.
  • TooltipStore.closeForSibling gives the delay group its own call. It closes an open tooltip. For a tooltip that is already closing, it stops hover tracking and marks the tooltip so Tooltip.Root skips the rest of its exit.
  • The default getCloseTrigger reports the active trigger, and setOpen leaves out a trigger that has left the document. Popover's and Dialog's overrides are removed. Menu keeps its own, which reads what the Root last rendered.
  • Select's override for actions.close() while closed is removed, since the shared store covers it.
  • Menu keeps committing hover changes without flushSync. The other popups need the synchronous commit for nested hover popups.

Fixes #6041

@pkg-pr-new

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

Copy link
Copy Markdown

commit: 6d4bc01

@code-infra-dashboard

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

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react ▼-449B(-0.09%) ▼-109B(-0.06%)

Details of bundle changes

Performance

No regressions in 36 benchmarks · ran 4m 02s · details


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

@netlify

netlify Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 6d4bc01
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ae3184e6da3577d6236513b
😎 Deploy Preview https://deploy-preview-6111--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 added component: menu Changes related to the menu component. component: menubar Changes related to the menubar component. component: context menu Changes related to the context menu component. component: popover Changes related to the popover component. component: dialog Changes related to the dialog component. component: alert dialog Changes related to the alert dialog component. component: drawer Changes related to the drawer component. component: tooltip Changes related to the tooltip component. component: preview card Changes related to the preview card component. component: select Changes related to the select component. component: combobox Changes related to the combobox component. component: autocomplete Changes related to the autocomplete component. labels Oct 27, 2026
@michaldudak
michaldudak force-pushed the popups/unify-open-changes branch 2 times, most recently from 13fdca3 to bc642cb Compare October 27, 2026 14:25
@michaldudak
michaldudak marked this pull request as ready for review October 27, 2026 15:32
@atomiks

atomiks commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

PR review

Nothing blocks merging. Two independent bug passes found no 🔴 regressions. They traced every removed per-component override back to the shared store and checked the drop rule against controlled popups, exit animations, focus return and the Tooltip delay group. There is one real regression. For Dialog, AlertDialog and Drawer, a preventUnmountOnClose() called after an await is now silently ignored if a second close request comes in while the handler is waiting. The PR documents this as a trade-off, but it's a footgun. The other problems are coverage gaps for the claimed "every popup" behavior, docs and JSDoc that no longer match the code, and missing labels and changelog wording for what are really changes to the onOpenChange and eventDetails.trigger contract. CI is green, and the size bot reports −74 B gz. This was a static review; nothing was run locally.

Bugs (2)

1. 🟠 A later close cancels an earlier close's preventUnmountOnClose() while a controlled open prop lags behind

Location: packages/react/src/utils/popups/store.ts:443

details.preventUnmountOnClose = () => {
  if (nextOpen) {
    return;
  }
  if (this.lastOpenChange !== details) {
    preventUnmountOnClose = true;
    return;
  }
  this.set('preventUnmountingOnClose', true);
};

The flag now belongs to a single request, not to the close as a whole. While the handler awaits, the open prop is still true, so a second close isn't dropped. If the handler skips it without calling cancel(), it commits preventUnmountingOnClose: false and replaces lastOpenChange. The first close's late call then sets only a local variable that nothing reads anymore. For the Dialog family this is a regression: master applied the late call whatever happened next. The same thing happens to a synchronous call (store.ts:307, !open && preventUnmountOnClose): if a second close commits before the prop catches up, the flag is reset to false. A matching open request, such as hovering a second trigger, also replaces lastOpenChange, and the new docs don't mention that case.

Failure scenario: <Dialog.Root open={open}> with onOpenChange={async (next, d) => { if (!next) { if (busy.current) return; busy.current = true; await save(); d.preventUnmountOnClose(); setOpen(false); } }}. The user presses Escape twice while saving. The dialog unmounts as soon as its CSS exit finishes, the JS exit animation loses its node, and actionsRef.unmount() does nothing. Nothing warns about any of this.

Fix: Tie the request to the close cycle. A late call from any close committed since the last open should still apply, and a later close that doesn't call it shouldn't clear the flag while the store is already closed. Canceled closes never commit, so the fix for the canceled-close case still holds, and the "cancel() your duplicates" rule can go. At minimum, add a dev-only warning when a committed close's late call is ignored.

2. ℹ️ eventDetails.trigger can be a disconnected element

Location: packages/react/src/utils/popups/store.ts:502

this.context.triggerElements.getById(activeTriggerId) ??
popupStoreSelectors.activeTriggerElement(this.state) ??
undefined

When the active trigger unmounts, the closeOnActiveTriggerUnmount close in popupRoot.tsx misses the registry lookup. It then falls back to the stale element and reports it to onOpenChange. On master this was undefined for Popover, Tooltip, Preview Card and Select.

Failure scenario: An open Tooltip whose trigger is removed calls onOpenChange(false, { trigger: <detached node> }), so user code that measures or focuses it gets a node that's no longer in the document.

Fix: Return undefined when the fallback element isn't connected, or document it.

Tests (5)

1. 🟠 The controlled after-await test runs only for the Dialog family

Location: packages/react/test/dialogRootSharedTests.tsx:97

This is the pattern the new handbook paragraph tells every popup to use. But the test only runs for Dialog and AlertDialog, which already passed on master. Popover, Menu, Select, Tooltip and Preview Card are covered only by a store unit test that skips Root sync.

Failure scenario: The controlled path breaks for every non-Dialog popup, and nothing fails.

Fix: The test only uses Root, Portal and Popup, so move it into popupConformanceTests under exitAnimation. It will then fail on master for the five popups it newly covers.

2. 🟡 Several rows of the "Changes for users" table are tested for only some popups or one mode

Location: packages/react/test/popupConformanceTests.tsx:231

  • "Close while closed" is tested only uncontrolled. The controlled case depends on the Root's layout effect resetting state.open.
  • "Leaving the trigger before the delay ends" is tested only in TooltipRoot.test.tsx:1690, though the table says Preview Card changed too.
  • Combobox passes exitAnimation: false, so the after-await test never runs for it. ValuePopupStore used to drop late calls, so its behavior changed as well.
  • Select's half of the "declined open, then close is reported" row has no test.

Failure scenario: Any of these paths regresses for the uncovered popup or mode, and CI stays green.

Fix: Run "close while closed" with it.each over uncontrolled and controlled. Move the before-delay leave test into the shared suite's hover branch. Add a Combobox Chromium after-await test and a Select declined-open test.

3. 🟡 Two detached-trigger paths aren't covered

Location: packages/react/test/detachedTriggersConformanceTests.tsx, packages/react/src/tooltip/trigger/TooltipTrigger.tsx:92

The new default getCloseTrigger takes the triggerElements.getById path that handle-registered triggers use, but only contained triggers are asserted. closeForSibling through TooltipHandleStore (whose inert fallback is a NOOP) is never exercised.

Failure scenario: A detached trigger reports the wrong trigger on Escape, or a handle-backed tooltip stays open when a sibling in its Provider opens. Neither would be caught.

Fix: Add an Escape-after-second-detached-trigger case to detachedTriggersConformanceTests. Add a Provider test that has one handle tooltip and one contained tooltip.

4. 🟡 The handbook's "cancel duplicates" fix isn't pinned

Location: packages/react/src/utils/popups/store.test.ts:508

The docs tell users to cancel() closes that arrive while they wait. That only works because lastOpenChange is assigned after the cancel check, and no test covers it.

Failure scenario: Moving this.lastOpenChange = details above the cancel check silently breaks the documented workaround.

Fix: Close request #1, then close request #2 canceled, then call #1's preventUnmountOnClose(), and expect preventUnmountingOnClose === true (with openProp: true). If Bug 1's fix lands, update this test to match.

5. 🟡 Two new Menu tests sit in a parametrized block they don't use

Location: packages/react/src/menu/root/MenuRoot.test.tsx:2513, :2557

Both are inside describe.for([contained, detached]) but ignore TestMenu, so each runs twice with identical setup. The test at :2557 is also under describe('prop: openOnHover') without setting it; it tests MenuPositioner's sibling-open guard.

Failure scenario: The duplicated runs add CI time without covering anything new, and the titles misdescribe what's tested, which breaks the AGENTS.md conventions.

Fix: Move both out of the describe.for, and put :2557 with the submenu tests.

Simplifications (3)

1. 🟡 Register droppedclose once in HoverIntent, not once per trigger

Location: packages/react/src/utils/popups/interactions/useHoverTriggerInteraction.ts:126

function onDroppedClose() {
  intent.cancel();
}
events.on('droppedclose', onDroppedClose);

getHoverIntent keeps one HoverIntent per store context, and that context also owns events. As written, every hover trigger adds its own listener that cancels the same intent.

Failure scenario: A popup with N hover triggers gets N listeners, N on/off pairs and a wrapper function per trigger, about 15–20 B gz for one call.

Fix: In the HoverIntent constructor, add store.context.events.on('droppedclose', () => this.cancel()). No cleanup is needed, since the emitter and the intent share a lifetime.

2. ℹ️ closeForSibling touches five places to serve one caller

Location: packages/react/src/tooltip/store/TooltipStore.ts:93

closeForSibling is a class-field arrow on every TooltipStore, plus a TooltipHandleStore Pick entry, a NOOP on the null store, and a new required useDelayGroup option whose only caller is TooltipTrigger.

Failure scenario: It adds about 15–20 B gz and some API surface for logic that's only two lines.

Fix: Keep { store, setIsInstantPhase } in currentContextRef and do the "set reason none if closing, then setOpen(false)" step inside useDelayGroup.

3. ℹ️ Smaller leftovers

Location: packages/react/src/utils/popups/store.ts:307, :497, packages/react/src/utils/popups/popupStoreUtils.ts:95

  • !open && in createPopupOpenState can't be reached, because the closure already returns early on opens.
  • getCloseTrigger reads through the exported popupStoreSelectors, so activeTriggerId and activeTriggerElement survive minification. The module-local selectors don't have that problem.
  • attachPreventUnmountOnClose now has a single caller (NavigationMenuRoot.tsx:212), and its synchronous-only rule differs from the one the stores now follow.

Failure scenario: A few dead bytes, plus a shared util that looks like the general rule but isn't.

Fix: Drop the !open &&, call the local selectors directly, and move the helper next to NavigationMenuRoot.

Docs (5)

1. 🟡 Labels and changelog say "no API changes", but two public contracts change

Location: PR metadata

The PR has only component labels: no type: label and no scope: all components. Two contract changes aren't flagged as such:

  • onOpenChange(false) is no longer called for a close while the popup is already closed. That covers actions.close(), handle.close() (not listed in the table), leaving a trigger before the delay ends, and the delay group.
  • eventDetails.trigger is now set for Escape, outside-press and imperative closes.

The table also leaves out that Menu's actions.close() now cancels a pending hover open (pinned by MenuRoot.test.tsx:2493).

Failure scenario: Code that counts onOpenChange(false) as "dismiss requested", or uses !eventDetails.trigger to detect Escape or outside press, quietly changes behavior after upgrading, and the changelog never warns about it.

Fix: Add type: bug and scope: all components. Add the missing rows. Write a changelog line naming both contract changes and pointing to reason for telling close causes apart.

2. 🟡 The trigger JSDoc is now wrong for popup closes

Location: packages/react/src/internals/createBaseUIEventDetails.ts:82

/**
 * The element that triggered the event, if applicable.
 */

For an Escape or outside-press close, the reported element didn't trigger the event; it's the trigger the popup was opened from. This text is copied into every popup's types.md.

Failure scenario: Readers assume trigger is set only when a trigger caused the close.

Fix: Override the JSDoc on PopupOpenChangeEventDetails, for example: "When closing, the trigger the popup was opened from, even if something else closed it. Use reason to tell why." Then run pnpm docs:api.

3. 🟡 The new handbook paragraph is vague and over-promises for Navigation Menu

Location: docs/src/app/(docs)/react/handbook/animation/page.mdx:193

Each close decides for itself, so a later close replaces the request: call eventDetails.cancel() on close requests that arrive while you're waiting.

"Each close decides for itself" is internal jargon. The code block that follows shows the synchronous pattern, not the await one. navigation-menu/page.mdx links here, but Navigation Menu still accepts preventUnmountOnClose() only synchronously.

Failure scenario: A reader writes the await pattern for Navigation Menu and it doesn't work, or ignores duplicate closes instead of canceling them and runs into Bug 1.

Fix: Add a short await example that includes the cancel() guard, and note the Navigation Menu exception. If Bug 1's fix lands, simplify the paragraph.

4. 🟡 The customization guide's claim that cancel() is equivalent to a guard no longer holds

Location: docs/src/app/(docs)/react/handbook/customization/page.mdx:57

This is an alternative to controlling the component with external state and guarding the state updates conditionally

Menu and Select used to drop a close that followed a declined open. Now a declined open that isn't canceled stays pending, and the next close is reported.

Failure scenario: A controlled openOnHover Menu that declines opens with a condition gets an onOpenChange(false, 'trigger-hover') call for a menu that never opened.

Fix: Add a sentence saying that cancel() is the way to decline when controlled, and that an ignored request stays pending until the open prop changes.

5. ℹ️ The store's doc block and the PR title overstate how uniform the rules are

Location: packages/react/src/utils/popups/store.ts:375

Menu still ignores requests that repeat its last rendered state, and reports its last rendered trigger, not the newest one. Combobox's isOpenChangeIgnored still drops a close that follows a declined open. Neither matches "[popups] Apply the same open-change rules to every popup".

Failure scenario: Maintainers rely on the doc block and assume the rules hold for every popup.

Fix: List the Menu and Combobox exceptions in the doc block, or retitle the PR.

Verdict

Approve after nits - no regression in normal use; the Dialog-family late preventUnmountOnClose() footgun and the labels and docs for the contract changes are worth fixing before merge.


🤖 Review generated with Claude Code

@michaldudak michaldudak removed component: select Changes related to the select component. component: menu Changes related to the menu component. component: alert dialog Changes related to the alert dialog component. labels Oct 28, 2026
michaldudak and others added 21 commits October 29, 2026 10:01
… shown, and filter detached triggers for Menu too
@michaldudak
michaldudak force-pushed the popups/unify-open-changes branch from 39fae2c to 6d4bc01 Compare October 29, 2026 09:56
@michaldudak
michaldudak merged commit 06cf9a0 into mui:master Oct 29, 2026
22 checks passed
@michaldudak
michaldudak deleted the popups/unify-open-changes branch October 29, 2026 10:23
jrandolf added a commit to vase-ui/vase-ui that referenced this pull request Nov 14, 2026
…eScript handbook pages (#343)

## Why

- Four handbook pages (Styling, Composition, Customization, TypeScript)
have no family. The parity page thus cannot show a Base UI change of
those pages as mirrored or queued.
- [Base UI #6215](mui/base-ui#6215) changes only
the Customization page. The parity page shows it as `not started`,
although the page exists.

## What

- Adds the families `handbook-styling`, `handbook-composition`,
`handbook-customization` and `handbook-typescript`. Each family names
the folder of its Base UI page in `upstreamPaths`. Each has an extract
at the tracked Base UI commit.
- Customization page: the paragraph after the `preventVaseUIHandler()`
sample now says that the listener takes over the interaction. It links
to the sections on controlled state and on `cancel()` ([Base UI
#6215](mui/base-ui#6215)). It replaces the
"escape hatch" sentence.
- Customization page: a new paragraph says that `cancel()` is the way to
decline a change of a controlled part. A request that the `open` prop
does not follow stays pending, so the part still reports a later close
([Base UI #6111](mui/base-ui#6111)). These cases
show it:
- `packages/vue/src/menu/root/MenuRoot.test.tsx`, "open change
requests": the part reports a close after a declined hover open, and
reports no close after a canceled hover open.
- `packages/vue/src/select/root/SelectRoot.test.tsx`, "closing": the
part reports a close after a declined open.
- `packages/vue/src/utils/popups/popupRoot.test.tsx`: "follows the open
prop, and keeps the popup open when the parent declines a request".
- `CONTRIBUTING.md`: each handbook page has a family. A family with no
Base UI demos has no record, so do not run `pnpm docs:demos` for it.
Compare the page with the Base UI page before you move `matchedCommit`.
- The Styling, Composition and TypeScript pages have no change. Each
page has the blocks of its Base UI page in their Vue form, with two
exceptions:
- The CSS-in-JS sample of Styling is reduced. It uses
`MenuPositionerCssVariables` in a `style` binding, and not
`MenuItemDataAttributes`. A `style` binding has no selector, and the
docs package has no CSS-in-JS library.
- The Composition page has no sentence on performance. A `#render` slot
is not a faster form of `as`, because both get their props from the same
function.

## Tests

- `pnpm parity:check` passes with the four new families. Each has no
source, no case and no exception row.
- The built parity page shows `handbook-customization: mirrored` for
Base UI #6215. No change has the state `not started`. Before this
change, Base UI #6215 was `not started`.
- `pnpm docs:build` passes. The two new links of the Customization page
resolve to headings of that page.
- `pnpm lint`, `pnpm build` and `pnpm docs:typecheck` pass.
- Each of the four pages was read against its Base UI page at the
tracked commit, block by block.
- The pull request adds no test: it changes data and text.
- `pnpm parity:check` already fails when an extract is absent, and when
`matchedCommit` is not the commit of the extract. `pnpm test:docs`
already fails when a family has a folder in `docs/demos` and no record.
flaviendelangle added a commit to michaldudak/base-ui that referenced this pull request Nov 19, 2026
Master (mui#5960, mui#6111) moved `useOpenStateTransitions` into popupRoot.tsx
and replaced its `onUnmount` callback with the store's `resetOnUnmount`.
Ported the deferred close check there: when an open request is pending at
unmount, `resetOnUnmount` and `onOpenChangeComplete(false)` now wait until
the request turns out to be ignored, and are skipped if it reopens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: all components Widespread work has an impact on almost all components. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[popover][select][dialog] onOpenChange(false) fires again with reason "focus-out" for a popup that is already closed

2 participants