Skip to content

[combobox][select] Decouple automatic unmounting from actionsRef - #5734

Merged
michaldudak merged 14 commits into
mui:masterfrom
michaldudak:codex/decouple-popup-unmount-actions-ref
Sep 22, 2026
Merged

michaldudak merged 14 commits into
mui:masterfrom
michaldudak:codex/decouple-popup-unmount-actions-ref

Conversation

@michaldudak

@michaldudak michaldudak commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Fixes #5587.

Passing actionsRef to Combobox, Autocomplete, Select, or Navigation Menu currently disables automatic unmounting and the closing onOpenChangeComplete(false) callback. This makes accessing imperative actions implicitly require managing the popup lifecycle.

Keep automatic unmounting enabled and add eventDetails.preventUnmountOnClose() to the open-change event details, matching the other popup components. The opt-out applies to one close cycle and resets on reopening.

Migration

Breaking for consumers that pass actionsRef to keep the popup mounted during an externally controlled exit animation. This may affect projects using 3rd party animation libraries (like Motion) with Base UI. Explicitly prevent unmounting when closing:

// Combobox, Autocomplete, Select
onOpenChange={(open, eventDetails) => {
  if (!open) {
    eventDetails.preventUnmountOnClose();
  }
  setOpen(open); // If controlling the open state.
}}

// Navigation Menu
onValueChange={(value, eventDetails) => {
  if (value == null) {
    eventDetails.preventUnmountOnClose();
  }
  setValue(value); // If controlling the value.
}}

Continue calling actionsRef.current?.unmount() when your animation finishes. onOpenChangeComplete(false) then fires at unmount; it is not the signal to invoke unmount(). Explicitly typed open-change handlers can use Root.OpenChangeEventDetails from the Combobox, Autocomplete, or Select namespace; Navigation Menu adds preventUnmountOnClose to Root.ChangeEventDetails directly.

actionsRef also gains a close() action, matching the other popups. Setting open={false} (or value={null} for Navigation Menu) directly never calls the change handler, so there is no opportunity to opt out of unmounting. When closing programmatically, call actionsRef.current?.close() instead: it closes with the imperative-action reason and runs the handler above. The Actions type now requires close, so hand-written { unmount() {} } ref stubs need a close member.

The opt-out survives a controlled close that is applied later, for example inside startTransition. Calling unmount() more than once in the same batch completes closing once, and calling it while the popup is open (or in the same batch as a reopen, as a late exit-animation callback might) does not affect later closes.

Consumers relying on automatic CSS closing animations/transitions need no migration.

Implementation

The close-cycle lifecycle (transition status, the opt-out flag, the guarded unmount, and the close-completion hook) now lives in one internal useUnmountAfterClose hook. useOpenStateTransitions (Popover, Menu, Dialog, Tooltip, Preview Card, Drawer) and the Combobox, Select, and Navigation Menu roots all call it, so the four popups share one implementation instead of three copies.

@michaldudak michaldudak added type: bug It doesn't behave as expected. breaking change Introduces changes that are not backward compatible. labels Sep 17, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

commit: 3ba790d

@michaldudak michaldudak added component: select Changes related to the select component. component: combobox Changes related to the combobox component. labels Sep 17, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+880B(+0.19%) 🔺+326B(+0.22%)

Details of bundle changes

Performance

Total duration: 1,083.60 ms -212.13 ms(-16.4%) | Renders: 76 (+0) | Paint: 1,769.76 ms -379.68 ms(-17.7%)

Test Duration Renders
Checkbox mount (500 instances) 93.69 ms 🔺+21.28 ms(+29.4%) 1 (+0)
Tabs mount (200 instances) 189.05 ms ▼-82.71 ms(-30.4%) 3 (+0)
Select mount (200 instances) 113.29 ms ▼-40.16 ms(-26.2%) 3 (+0)
Menu mount (300 instances) 74.42 ms ▼-25.55 ms(-25.6%) 1 (+0)
Menu open (500 items) 85.94 ms ▼-21.92 ms(-20.3%) 11 (+0)

…and 3 more (+7 within noise) — details


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

@netlify

netlify Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 3ba790d
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ab2616f99179000080200fa
😎 Deploy Preview https://deploy-preview-5734--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 the component: autocomplete Changes related to the autocomplete component. label Sep 17, 2026
michaldudak and others added 2 commits September 17, 2026 10:10
…ntOnClose docs

- Make the unmount action a no-op once the popup has already unmounted, so a consumer that keeps calling it after the automatic unmount no longer receives onOpenChangeComplete(false) twice.
- Attach preventUnmountOnClose through a cast like the other popup stores instead of a throwaway Object.assign.
- Declare preventUnmountOnClose as a property so the generated API docs render it inline instead of leaking the AriaCombobox namespace and a bogus external type.
- Update the animation handbook's manual unmounting example to call preventUnmountOnClose, link the component pages to it, and regenerate the components index.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
- Make the shared forceUnmount a no-op once the popup is already unmounted, matching the guard added to Select and Combobox, so every popup's unmount action ignores calls made after the automatic unmount.
- Clear the Select and Combobox opt-out flag when setOpen opens, mirroring createPopupOpenState, so a same-batch reopen starts a new close cycle without relying on a render.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@michaldudak
michaldudak marked this pull request as ready for review September 17, 2026 08:27
@michaldudak michaldudak removed the breaking change Introduces changes that are not backward compatible. label Sep 17, 2026
jjenzz
jjenzz previously approved these changes Sep 17, 2026

@jjenzz jjenzz left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving from a DX perspective. I agree this is the correct direction. Sounds like a bugfix tbh, the coupling seems unexpected. I wonder if it's worth adding a dev warning for those who might now be calling unmount when the component is already unmounted though? e.g.:

Base UI: unmount() was called after the popup had already unmounted. To control unmounting manually, call eventDetails.preventUnmountOnClose() in onOpenChange before calling unmount(). Learn more: https://docs...

I don't feel strongly here, so i'll leave it up to you :)

@jjenzz

jjenzz commented Sep 17, 2026

Copy link
Copy Markdown
Member

PR review

The new opt-out is lost for common controlled-state timing, so external exit animations can still be cut off. Repeated synchronous unmount() calls also remain non-idempotent. Both issues survive despite green CI because the tests omit deferred controlled updates and same-batch calls.

Bugs (2)

1. 🔴 Controlled closes can lose or never receive the unmount opt-out

Location: packages/react/src/select/root/SelectRoot.tsx:143

if (open && preventUnmountingOnClose) {
  setPreventUnmountingOnClose(false);
}

// ...

enabled: mounted && !open && !preventUnmountingOnClose,

The opt-out is only established during an internal onOpenChange request and is cleared whenever a render still observes open. A controlled handler that defers its state update—through startTransition, a microtask, or similar—first renders with open === true, immediately clearing the opt-out. The later open={false} render then automatically unmounts.

Direct programmatic closes have the same outcome: changing the controlled prop to false does not emit onOpenChange, leaving no opportunity to call preventUnmountOnClose(). This regresses consumers whose actionsRef previously kept Select, Combobox, or Autocomplete mounted for an external animation. The equivalent logic exists in AriaCombobox.tsx:577.

Failure scenario: A controlled Select calls preventUnmountOnClose() in its close handler, but applies setOpen(false) in startTransition. The popup unmounts before Motion or another external animation completes. An external close button calling setOpen(false) cannot opt out at all.

Fix: Preserve the opt-out until an actual reopen transition occurs, rather than clearing it merely because the current render is open. Also provide a persistent/manual mechanism that can be activated before prop-driven closes. Add regression coverage for deferred controlled updates and direct programmatic closes.

2. 🟠 The unmount guard is not synchronous

Location: packages/react/src/utils/popups/popupStoreUtils.ts:601

const forceUnmount = useStableCallback(() => {
  if (!mounted) {
    return;
  }
  setMounted(false);
  // ...
  store.context.onOpenChangeComplete?.(false);
});

mounted is captured from the latest render. Calling unmount() twice before React commits the first state update sees mounted === true both times, repeating cleanup and onOpenChangeComplete(false). Select and AriaCombobox duplicate the same guard.

Failure scenario: Two animation completion or cleanup callbacks invoke actionsRef.current.unmount() in the same batch, causing the consumer’s completion callback to run twice.

Fix: Guard with state updated synchronously before callbacks run, such as a ref or the immediately updated store state. Add a test that calls unmount() twice within one act().

Verdict

Request changes - controlled external-animation flows remain broken.


🤖 Review generated with Codex

@jjenzz
jjenzz dismissed their stale review September 17, 2026 15:52

it was a DX review without a PR review

…nd add a close action

- Store the opt-out flag in the Select and Combobox stores and derive it the way useOpenStateTransitions does, so an opt-out recorded while a controlled close is still pending (for example inside startTransition) is no longer cleared by the render that still sees open.
- Add a close action to the Select, Combobox, and Autocomplete actionsRef, matching the other popups, so a programmatic close goes through onOpenChange and can opt out of unmounting.
- Guard every unmount action on store state that is updated synchronously, so two calls in one batch complete closing once.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@michaldudak

Copy link
Copy Markdown
Member Author

@jjenzz Thanks, both reproduced and are fixed in f62085c.

1. Controlled closes losing or never receiving the opt-out

  • Deferred closes: the flag now lives in the Select and Combobox stores and is derived the same way useOpenStateTransitions does for the other popups. setOpen writes it synchronously, and a dedicated useSyncedValues call only writes back when the derived value changes, so the render that still sees open === true during a startTransition (or any later-applied) close no longer clears it. The render-phase reset is gone.
  • Prop-driven closes: actionsRef gains a close() action on Select, Combobox, and Autocomplete, matching the other popups. It closes with the imperative-action reason through onOpenChange, so the handler can opt out. The component docs and the PR description now point to it. Flipping the controlled prop from elsewhere still bypasses onOpenChange, which is the same behavior as Popover and friends, so I didn't add a separate persistent mechanism for that.

2. Non-synchronous unmount guard

  • forceUnmount and the Select and Combobox handleUnmount now check store.state.mounted, which they update synchronously in the same call, instead of the last rendered mounted. Two unmount() calls in one batch complete closing once.

Tests

  • Select, Combobox, and Autocomplete each get a startTransition controlled close, a close() call with opt-out, and a double unmount() in one batch.
  • Popover gets the transition and double-unmount cases across its three trigger fixtures, since the guard change is in the shared hook.

michaldudak and others added 2 commits September 18, 2026 10:01
…us unmount guard

- Guard repeated unmount() calls with a ref that mirrors the rendered mounted state instead of writing mounted: false into the Select and Combobox stores synchronously. The extra store notification re-rendered every subscriber outside React's batch and made the Combobox input-inside-popup tests emit act() warnings in Chromium.
- Add close to the hand-rolled actionsRef mocks in the Select tests and the public-types harnesses now that the Actions types require it.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@jjenzz

jjenzz commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

i'll leave it to you to determine how pragmatic this is

PR review

One merge-blocking lifecycle regression affects Select, Combobox, and Autocomplete. Targeted JSDOM reproductions failed for all three; 30 selected existing tests passed. Browser tests were not run.

Bugs (1)

1. 🔴 Batched reopening permanently disables unmounting

Location: SelectRoot.tsx:314, also AriaCombobox.tsx:953.

if (!mountedRef.current) {
  return;
}
mountedRef.current = false;
setMounted(false);

When unmount() and reopening occur in the same batch, useTransitionStatus restores mounted to true before commit. The committed value remains true, so the [mounted] layout effect never resets mountedRef.current. Every subsequent automatic or manual unmount returns early, leaving the closed popup mounted and suppressing closing completion.

Failure scenario: An external animation finishes and its callback calls unmount() followed by setOpen(true). On the next close, even explicitly calling unmount() cannot remove the popup. A delayed exit callback arriving after reopening causes the same problem.

Fix: Resynchronize the guard on every committed render, including when reopening keeps mounted unchanged. Add regression coverage for unmounting and reopening within one batch in all three components.

Verdict

Request changes — reopening can permanently break subsequent popup cleanup.


🤖 Review generated with Codex

…ecycle and fix the unmount guard

Extract the close-cycle lifecycle into a `useUnmountAfterClose` hook used by
`useOpenStateTransitions`, Combobox, Select, and Navigation Menu. The mounted
mirror now resyncs on every commit, so calling `unmount()` while the popup is
open (or in the same batch as a reopen) no longer blocks every later unmount.

Port Navigation Menu to the same opt-out model: automatic unmounting with
`actionsRef`, `preventUnmountOnClose()` on the change event details, and a
`close()` action. Build the Autocomplete open-change details on its own
`ChangeEventDetails` so `isItemPress` no longer leaks into its docs, and
update the TypeScript handbook, Select page, and `actionsRef` JSDoc.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@jjenzz

jjenzz commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

PR review

One merge-blocking issue: Navigation Menu can discard the manual-unmount opt-out during an exit animation. Existing affected suites passed in jsdom and Chromium; two focused reproductions failed in both.

Bugs (1)

1. 🔴 Preserve the opt-out across repeated close requests

Location: NavigationMenuRoot.tsx:224–225

if (nextValue == null) {
  setPreventUnmountOnClose(shouldPreventUnmountOnClose());

When the value is already null, another close request skips onValueChange but still resets the opt-out to false. Automatic unmounting resumes before the external animation finishes.

Failure scenario: Close with preventUnmountOnClose(), then move focus from the trigger outside the menu. The trigger’s blur handler requests another close, prematurely removing the popup. Calling actionsRef.current.close() twice also reproduces it.

Fix: Preserve the existing opt-out for redundant close requests until reopening or explicit unmounting. Add regression coverage for both repeated imperative closes and focus leaving during a manually managed exit.

Verdict

Request changes — ordinary focus movement can break the promised manual-unmount lifecycle.


🤖 Review generated with Codex

…uests

A close requested while the value is already `null` (the trigger's blur after
an imperative close, or a repeated `close()`) skips `onValueChange`, so the
consumer can't opt out again. Don't overwrite the recorded opt-out in that
case; it resets on reopen or on `unmount()` as before.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
michaldudak and others added 3 commits September 21, 2026 15:09
A stale exit-animation callback can call `unmount()` after a quick reopen.
Running the host's unmount cleanup and the close completion against a live
popup cleared the positioner anchor, replayed the enter transition, and fired
`onOpenChangeComplete(false)` while open. Make `forceUnmount` a no-op while
open; the later close still unmounts automatically.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ch commits

Guarding `forceUnmount` on the rendered `open` dropped a close and an
`unmount()` issued in the same batch, which the Select trigger's deferred
close hits in Chromium. Force a re-render instead and unmount in the commit
if the popup closed, otherwise drop the call.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@jjenzz

jjenzz commented Sep 21, 2026

Copy link
Copy Markdown
Member

PR review

No findings. The previous Navigation Menu issue is fixed, and both reproductions now pass.

Verified 856 jsdom tests, 1,007 Chromium tests, and 46 additional shared-popup lifecycle tests. The full repository suite was not run.

Verdict

Approve — the updated lifecycle handling resolves the reported bug without any new verified regressions.


🤖 Review generated with Codex

michaldudak and others added 3 commits September 22, 2026 08:26
Restructure the JavaScript animations section of the animation handbook
around the two choices a consumer makes: where the popup goes when closed
and who ends the closing phase. Reduce the per-component "Closing
animations" sections to a link, and reword the `unmount` action's JSDoc on
Combobox, Autocomplete, Select and Navigation Menu to say it ends the
closing phase rather than removes the DOM.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Combobox, Autocomplete and Select now mention the closing phase as a usage
guideline bullet instead of a two-sentence section. Drop the `close()`
sentence from the handbook's manual unmounting text.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@michaldudak michaldudak changed the title [combobox][select] Decouple automatic unmounting from actionsRef [combobox][select] Decouple automatic unmounting from actionsRef Sep 22, 2026
@michaldudak
michaldudak merged commit 2142c2b into mui:master Sep 22, 2026
24 checks passed
@michaldudak
michaldudak deleted the codex/decouple-popup-unmount-actions-ref branch September 22, 2026 11:16
michaldudak added a commit to michaldudak/base-ui that referenced this pull request Sep 22, 2026
…lightItem

mui#5734 decoupled automatic unmounting from `actionsRef`, so the notes,
the demo's manual `unmount()` call and the regression guard that pinned
the old coupling no longer apply. Keep `close` alongside `highlightItem`
on the Combobox and Select actions.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
michaldudak added a commit to michaldudak/base-ui that referenced this pull request Sep 23, 2026
Resolves conflicts with mui#5586 (highlightItem) and mui#5734 (unmount decoupling):
- AriaCombobox: keep scrollItemIntoView alongside the new onNavigate source/inline handling;
  drop the now-unused useOpenChangeComplete import.
- Combobox virtualized demos keep the built-in Virtualizer; the `imperative-action` scroll
  handling moves to the TanStack demos, and the docs note to the third-party section.
- Add `imperative-action` to ListHighlightReason so imperative highlights scroll the
  built-in virtualizer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Thegreatsura pushed a commit to Thegreatsura/fluid-functionalism that referenced this pull request Oct 12, 2026
Base UI only waits for animations it can detect on the popup element, so
the framer-motion exit spring was cut on the Base flavor: the dropdown,
dialog, color picker and tooltip disappeared in about 20ms at full
opacity. Select and combobox kept theirs only because passing actionsRef
used to hold the unmount, which 1.9 removed (mui/base-ui#5734). Installs
already resolve 1.9, so they lost those animations too and failed to
typecheck (Actions now requires close).

- Every Base popup now calls eventDetails.preventUnmountOnClose() on
  close and releases the unmount from onAnimationComplete (plus the
  existing fallback timeout).
- Popups with an open prop replay a close made by setting it through
  actionsRef.close(), so programmatic closes (the command menu picking a
  row, a form closing its dialog) animate too.
- Select's acknowledge delay holds the unmount at press time; 1.9 keeps
  the opt-out until the delayed close lands.
- Tooltip's exit target names x and y, so motion runs one exit animation
  instead of an instant reset that released the unmount early.
- Checkbox group: Base UI 1.7+ stops its replayed click at the hidden
  input, so the square no longer stops the original click (it was
  swallowing the only click the row would get).
- package.json and every registry item that uses Base UI now require
  @base-ui/react@^1.9.0, so installs and the site run the same version.

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

component: autocomplete Changes related to the autocomplete component. component: combobox Changes related to the combobox component. component: select Changes related to the select component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[combobox][select] Decouple auto-unmount from actionsRef presence

2 participants