Skip to content

[popups] Improve mount performance - #4649

Closed
atomiks wants to merge 27 commits into
mui:masterfrom
atomiks:popups/perf-gain
Closed

atomiks wants to merge 27 commits into
mui:masterfrom
atomiks:popups/perf-gain

Conversation

@atomiks

@atomiks atomiks commented Apr 20, 2026 •

Copy link
Copy Markdown
Contributor

This PR now focuses on the popup performance work that is still in this branch after rebasing on the interaction-split changes from master. It keeps popup coordination in the popup store, removes the remaining generic interaction wrapper paths, and limits popup-root interaction prop syncing to the times a popup is actually open or mounted.

Changes

  • Make popup stores own their synced FloatingRootStore setup, including floating ids, nesting, trigger maps, and shared popup selectors.
  • Replace the remaining useInteractions/useRole wrapper paths with direct prop merging in popup roots, Select, Combobox, and Floating UI tests.
  • Centralize popup trigger ownership and ARIA state for active triggers, inactive triggers, focusable popup props, and detached/multiple-trigger cases.
  • Defer popup-root interaction prop syncing behind open || mounted where safe, while keeping Menu on its more guarded path.
  • Keep trigger element lookups fast with a mirrored trigger element set in PopupTriggerMap.
  • Add and update regression coverage for detached triggers, active trigger ownership, first-open/controlled-open behavior, open-method reset, and the Floating UI hook changes.

Notes

  • This no longer includes the older benchmark/experiment work or the broader cleanup that has since landed separately.
  • Select and Combobox are touched only to remove useInteractions from their prop plumbing; the main perf work here is still the popup store/root path.

@atomiks atomiks added performance scope: all components Widespread work has an impact on almost all components. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. labels Apr 20, 2026
@pkg-pr-new

pkg-pr-new Bot commented Apr 20, 2026 •

Copy link
Copy Markdown

commit: 7435068

@code-infra-dashboard

code-infra-dashboard Bot commented Apr 20, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react ▼-1.27KB(-0.27%) ▼-412B(-0.28%)

Details of bundle changes

Performance

Total duration: 1,323.07 ms ▼-282.75 ms(-17.6%) | Renders: 52 (🔺+2) | Paint: 2,060.24 ms ▼-410.16 ms(-16.6%)

Test Duration Renders
Select mount (200 instances) 167.05 ms 🔺+46.86 ms(+39.0%) 3 (+0)
Slider mount (300 instances) 193.78 ms 🔺+46.59 ms(+31.7%) 3 (+0)
Mixed surface mount (app-like density) 84.99 ms 🔺+17.68 ms(+26.3%) 5 (+0)
Checkbox mount (500 instances) 73.98 ms 🔺+8.76 ms(+13.4%) 1 (+0)
Scroll Area mount (300 instances) 82.69 ms 🔺+4.40 ms(+5.6%) 3 (+0)

…and 7 more — details


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

@atomiks atomiks changed the title [all components] Improve popup performance [popups] Improve mount performance Apr 20, 2026
@netlify

netlify Bot commented Apr 20, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 7435068
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69fb2bf24eaaca000827093b
😎 Deploy Preview https://deploy-preview-4649--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

Copy link
Copy Markdown
Member

Critical (must fix before merge)

C1 — Tooltip popup no longer sets role="tooltip"

File: packages/react/src/tooltip/popup/TooltipPopup.tsx
With useRole deleted, PopoverPopup (role: 'dialog') and MenuPopup (role: 'menu') got hardcoded replacements; TooltipPopup did not. The new test in TooltipRoot.test.tsx:38-55 asserts aria-describedby is gone from the trigger — so tooltips are now invisible to assistive tech. A11y regression with no loud failure.

C2 — TooltipTrigger onClick handler is either dead or double-fires

File: packages/react/src/tooltip/trigger/TooltipTrigger.tsx:143-147

onClick(event) {
  if (closeOnClick && !store.select('open')) {
    store.setOpen(false, createChangeEventDetails(REASONS.triggerPress, event.nativeEvent));
  }
},

Guard fires only when the tooltip is not open, then calls setOpen(false) — either a no-op, or (since TooltipStore.setOpen has no same-state early-return) a duplicate onOpenChange fire. The new "calls onOpenChange once" test at TooltipRoot.test.tsx:891-914 likely passes by accident. Condition looks inverted.

C3 — Render-time mutation of store.state.floatingRootContext

File: packages/react/src/utils/popups/popupStoreUtils.ts:223
usePopupRootSync assigns store.state.floatingRootContext = … during render. Only the external-store branch calls notifyAll() inside a layout effect; non-external subscribers via store.useState('floatingRootContext') are not woken. Works today only because useSyncedFloatingRootContext returns a stable reference — any future change to that ref (e.g., Strict Mode double-render) silently desyncs subscribers. Use store.set(...) or useSyncedValue to make notification explicit.

C4 — openMethod never cleared on unmount; detached-trigger click never writes it

  • popupStoreUtils.ts:236-240 resets openMethod only when !open. If root unmounts while open === true, or a handle-based consumer has no mounted root, openMethod leaks to the next opener.
  • DialogTrigger.tsx:88 and PopoverTrigger.tsx:145 route handle-equipped triggers to rootTriggerProps, which relies on a mounted root running useOpenMethodTriggerProps. Handle-only consumers get no openMethod written. Silent focus-style / iOS touch regression.

Important

I1 — MenuRoot rewritten with zero new Menu tests

MenuRoot.tsx lost useRole, gained usePopupRootSync, gated dismiss/typeahead on open, yet MenuRoot.test.tsx and MenuRoot.detached-triggers.test.tsx are untouched. Gaps:

  • Dismiss/typeahead now enabled: open && !disabled — while the close-animation plays, Escape / outside-press / typeahead silently do nothing. Inconsistent with popupProps memo at MenuRoot.tsx:513 which uses open || mounted.
  • aria-haspopup: 'menu' and popup aria-labelledby: activeTriggerId moved from useRole to manual wiring — no tests assert these remain.
  • MenuRoot.tsx:103 changed open: defaultOpen → open: openProp ?? defaultOpen (SSR-visible first-paint change) with no test.

I2 — PreviewCard refactor entirely untested

No changes to preview-card test files. With useRole deleted, the aria-expanded/trigger ARIA path is gone and no replacement is visible in the PR. Also:

  • PreviewCardRoot.tsx:85-86 uses dismiss.reference ?? EMPTY_OBJECT — Dialog/Popover roots omit the fallback, creating an inconsistent contract (see I3).
  • PreviewCardStore.ts:122-134 conditionally calls useSyncedValue('floatingRootContext', …) only for external stores; no handle-based preview-card test exercises this.

I3 — inactiveTriggerProps = dismiss.trigger may be undefined in Dialog/Popover

useDialogRoot.ts:152-153 and the equivalent in PopoverRoot.tsx:129-150 don't apply the ?? EMPTY_OBJECT fallback. useDismiss returns {} when disabled, so current behavior may be safe, but the divergence from PreviewCardRoot signals unclear contract. Either remove the preview-card fallback or add it to Dialog/Popover.

I4 — Stale open read in Menu triggers

MenuTrigger.tsx:223-225 and MenuSubmenuTrigger.tsx:161-163 call useOpenMethodTriggerProps(store.select('open'), …). store.select doesn't subscribe, so the click handler sees a stale open. DialogTrigger/PopoverTrigger use store.useState('open') here — Menu should match.

I5 — usePopupRootSync helper has no tests

popupStoreUtils.ts:199-239 is shared by Dialog/Popover/Menu/Tooltip roots but popupStoreUtils.test.tsx wasn't updated. The external-store notifyAll() path is entirely uncovered.

I6 — Duplicated trigger-prop assembly across triggers

aria-haspopup / aria-expanded / aria-controls and the controlsPopup = open && (isOpenedByThisTrigger || activeTriggerId == null || store.context.triggerElements.size === 1) expression are copy-pasted across DialogTrigger, PopoverTrigger, MenuTrigger. AGENTS.md explicitly calls out avoiding duplication — extract into a shared hook next to useTriggerDataForwarding.

I7 — Inconsistent Root context shapes after refactor

DialogRootContext.ts and PopoverRootContext.ts switched from { store } to the store directly, but MenuRootContext.ts still wraps { store, parent }. Internal types only, but confusing to readers.


Medium / Low

  • M1 — MenuRoot.tsx:372 reads store.state.floatingRootContext directly; every other consumer uses store.useState(...). Diverges from the pattern.
  • M2 — One-frame stale triggerProps after popup opens in Dialog/Popover (*Interactions renders only when open || mounted, so the first render missing dismiss handlers). Minor — one lost frame of outside-press handling.
  • L1 — useOpenChangeComplete now gated mounted && !open && !preventUnmountingOnClose (popupStoreUtils.ts:186) — intentional but changes when the completion callback fires.
  • L2 — usePopupRootSync types eventDetails as any at popupStoreUtils.ts:214. Erodes type safety around reason codes. AGENTS.md discourages as any casts.
  • L3 — Dropped sentence in the iOS Safari comment at useOpenInteractionType.ts:11-12 — the "interactionType will be '' in that case" clause explained why the || fallback trips. Restore it.
  • L4 — Nested role: 'menuitem' for nested-menu triggers previously supplied by useRole — not restored. Verify MenuSubmenuTrigger still produces the right role.
  • L5 — useRole previously used getFloatingFocusElement(floatingElement)?.id || defaultFloatingId; the replacements use plain floatingId. Minor edge case when a child owns focus.

@atomiks
atomiks requested a review from romgrk April 20, 2026 13:27
Comment on lines 184 to 199
enabled: !preventUnmountingOnClose,
enabled: mounted && !open && !preventUnmountingOnClose,
open,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This line fixes unmount/teardown perf:

Contained triggers warm unmount (all) 119.95 ms 17.25 ms -102.7 ms (-85.6%)

@github-actions github-actions Bot added PR: out-of-date The pull request has merge conflicts and can't be merged. and removed PR: out-of-date The pull request has merge conflicts and can't be merged. labels Apr 21, 2026
@atomiks
atomiks marked this pull request as ready for review April 21, 2026 15:42

@romgrk romgrk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would have been really nice to split this PR into conceptual chunks, reviewing 2k+ lines is not really fun nor safe, and we can't see the individual effect of each change (e.g. adding fastComponent(), avoiding work before mount, ...).

@atomiks

atomiks commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

@romgrk yeah totally fair 😅 I'll split this into multiple PRs so it's easier to review and we can see the perf differences incrementally. At least I got it into (what I think should be) mostly the final state for perf + bundle size though so it shouldn't be that hard to split it from here

isDrawer: boolean;
}

export function DialogInteractions({

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.

Could be worth using fastComponent on this one since it's used a lot and has at least 5 selectors

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It only affects opening, so not really. One single dialog opening is not a perf cliff.

(Also I'm drafting this PR, as we should start with a review on #4661, we'll get back to this once that lands)

@atomiks
atomiks marked this pull request as draft April 22, 2026 10:47
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Apr 28, 2026
@atomiks
atomiks force-pushed the popups/perf-gain branch from a28ce5b to 7435068 Compare May 6, 2026 11:54
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label May 6, 2026
@atomiks

atomiks commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

Creating separate PRs for:

  • Deleting useRole + useInteractions
  • Improving Menu perf

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

Labels

performance scope: all components Widespread work has an impact on almost all components. type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants