Skip to content

[popups] Keep transition state consistent when reopening a retained popup - #5738

Merged
LukasTy merged 7 commits into
mui:masterfrom
LukasTy:fix/menu-reopen-transition-state
Sep 24, 2026
Merged

LukasTy merged 7 commits into
mui:masterfrom
LukasTy:fix/menu-reopen-transition-state

Conversation

@LukasTy

@LukasTy LukasTy commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Noticed while working on mui/material-ui#48823.

When a retained popup reopens, open reaches the store before mounted and transitionStatus synchronize in a layout effect. A transition driven by open && transitionStatus !== 'starting' can then enter twice, and onOpenChangeComplete can fire before entry finishes.

Derive starting from the effective open state while the lifecycle is not mounted. The shared selector covers Menu, Popover, Dialog, Tooltip, and Preview Card. Select and Combobox use separate selectors and synchronize these lifecycle values in one store update.

Also initialize the store's mounted value from useTransitionStatus before child parts render. This makes the initial store state agree with the hook: an ordinary initially-open Root is already mounted and does not expose a spurious starting state to a direct-Root Backdrop. Intentional initial submenu transitions still start normally. This initialization runs once; later openings, exit reversals, and completion tracking keep their existing lifecycle.

Tests

  • Shared selector tests cover controlled and uncontrolled opening, mounted states, reversed exits, and closed states.
  • Menu browser tests cover controlled and uncontrolled state, both initial open states, both portal modes, exit reversal, and animation-completion timing. The initially-open retained Menu cases also repeat the full close/reopen cycle.
  • Popover browser coverage checks a full close/reopen cycle on the same retained element.
  • Direct-Root Menu and Popover Backdrop tests assert that ordinary initial-open content never commits starting, with controlled and uncontrolled state. These check public state consistency.

The browser tests assert the committed state sequence and real animation-completion timing. They finish long-running animations explicitly; the Menu reversal case waits for data-open before finishing the animation so the exit cannot finish before the reopen handler runs. Redundant synthetic onEnter tracking and repeated cycles outside the original initially-open retained Menu scenario have been removed.

Validation

Local validation after merging master (53e533a48) in f3519722a, using React 19.3.0:

  • 222 committed Backdrop and transition cases pass across Chromium, Firefox, and WebKit, including the existing submenu enter-transition suite.

  • Related Chromium suites: 3,083 passed, 67 skipped.

  • Related jsdom suites: 2,332 passed, 818 skipped.

  • TypeScript, ESLint, stylelint, Prettier, and diff whitespace checks pass.
    Earlier regression checks: removing the selector fix fails the same 15 retained-popup cases across the three browsers. Before the initialization correction, the new Menu and controlled Popover Backdrop assertions fail on the unexpected starting state. A temporary six-component Backdrop probe passes all 36 initial-open cases with the correction.

  • I have followed the PR section of the contributing guide.

@pkg-pr-new

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

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@5738
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5738
    

commit: f351972

@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 🔺+62B(+0.01%) 🔺+21B(+0.01%)

Details of bundle changes

Performance

Total duration: 1,224.72 ms -123.87 ms(-9.2%) | Renders: 76 (+0) | Paint: 1,997.33 ms -209.23 ms(-9.5%)

Test Duration Renders
Checkbox mount (500 instances) 64.38 ms ▼-38.05 ms(-37.1%) 1 (+0)
Slider mount (300 instances) 92.71 ms ▼-27.90 ms(-23.1%) 2 (+0)
Dialog mount (300 instances) 27.34 ms ▼-8.44 ms(-23.6%) 1 (+0)

12 tests 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!

Name Link
🔨 Latest commit f351972
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6ab4e2299b61fb00082d2758
😎 Deploy Preview https://deploy-preview-5738--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.

@LukasTy LukasTy added component: menu Changes related to the menu component. type: bug It doesn't behave as expected. labels Sep 18, 2026
LukasTy and others added 2 commits September 18, 2026 12:10
`open` reaches the popup store synchronously, while `mounted` and
`transitionStatus` sync in a layout effect. A retained popup therefore
renders one commit as open but not yet starting, so a transition driven
by `open && transitionStatus !== 'starting'` enters twice, and completion
can fire before the enter animation finishes.

Derive the starting status in `popupStoreSelectors` instead of `MenuStore`.
The defect is in the shared ordering, so this fixes Menu, Popover, Dialog,
Tooltip and Preview Card together. Select and Combobox keep their own
selectors and do not have the defect.

Add unit coverage for the selector and a Popover browser regression.

Co-Authored-By: Claude Opus 5 <[email protected]>
`waitFor` on a running 200ms transition needed a poll to land inside that
window. Once it closed, `getAnimations()` stayed empty and the wait failed at
timeout instead of retrying. Use a transition that outlasts every phase, then
finish the animations to end the phase. This also removes the `pause()`
workaround for the reversed exit.

Stop asserting inside the `onOpenChangeComplete` mock. The library calls it
from `flushSync` inside a promise continuation, so a throw escaped as an
unhandled rejection rather than failing the test, and the check was vacuous on
close. The test body already asserts the same thing.

Co-Authored-By: Claude Opus 5 <[email protected]>
@LukasTy LukasTy changed the title [menu] Keep transition state consistent when reopening a retained popup [popups] Keep transition state consistent when reopening a retained popup Sep 18, 2026
@LukasTy LukasTy self-assigned this Sep 18, 2026
@LukasTy
LukasTy requested a balanced review from Copilot September 18, 2026 10:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused selector correction matches the lifecycle behavior and is comprehensively covered by unit and browser regressions.

Pull request overview

Keeps popup transition state consistent when reopening retained popups.

Changes:

  • Derives starting while an open popup lifecycle is not mounted.
  • Adds selector unit tests and browser regressions for Menu and Popover.
File summaries
File Description
packages/react/src/utils/popups/store.ts Corrects the shared transition selector.
packages/react/src/utils/popups/store.test.ts Covers selector lifecycle states.
packages/react/src/popover/popup/PopoverPopupTransitionState.test.tsx Tests retained Popover reopen cycles.
packages/react/src/menu/popup/MenuPopupTransitionState.test.tsx Tests Menu lifecycle combinations and exit reversal.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copy link
Copy Markdown
Member

Nit (non-blocking): simplify the two new browser test files.

In MenuPopupTransitionState.test.tsx and PopoverPopupTransitionState.test.tsx, the committed state-sequence assertion already detects duplicate entry. We could remove the separate onEnter callback/mock, previousEnter ref, layout effect, and associated assertions. One complete close/reopen cycle should also be sufficient; the second repeats the same path.

This keeps all nine cases, including the Menu controlled/uncontrolled, keepMounted, and initial-open combinations, plus exit-reversal and animation-completion checks. I tried this simplification locally: it removes 36 lines, all nine Chromium cases pass, and reverting the selector fix still causes the same five retained-popup cases to fail. ESLint and formatting also pass.


🤖 Review generated with Codex

@atomiks

atomiks commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

PR review

One non-blocking regression.

Bugs (1)

🟠 Initial-open Backdrops can incorrectly get starting

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

transitionStatus: (state) =>
  openSelector(state) && !state.mounted ? 'starting' : state.transitionStatus

open && !mounted also occurs briefly on an initially-open Root before the layout-effect sync. Non-portaled Backdrops render during that window, so they can see transitionStatus: 'starting' and incorrectly play an initial enter animation.

The new Popup tests miss this because Portal children render after the store has synchronized.

Fix: distinguish a real reopen from the initial unsynced store state, and add an initially-open direct-Root Backdrop regression test.

Verdict

Approve after nits — the reopen fix looks correct otherwise; bundle impact is negligible (+29 B parsed / +8 B gzip).

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Sep 24, 2026
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Sep 24, 2026
@LukasTy
LukasTy merged commit 052e20a into mui:master Sep 24, 2026
24 checks passed
@LukasTy
LukasTy deleted the fix/menu-reopen-transition-state branch September 24, 2026 08:54
@jjenzz jjenzz added the scope: store Related to shared store state, subscriptions, or synchronization with React renders. label Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: menu Changes related to the menu component. scope: store Related to shared store state, subscriptions, or synchronization with React renders. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants