Skip to content

[popover] Avoid re-rendering inactive detached triggers when the open reason changes - #5894

Closed
willhoney7 wants to merge 1 commit into
mui:masterfrom
willhoney7:fix/popover-detached-trigger-rerenders
Closed

willhoney7 wants to merge 1 commit into
mui:masterfrom
willhoney7:fix/popover-detached-trigger-rerenders

Conversation

@willhoney7

@willhoney7 willhoney7 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #5893, which fixed the same kind of issue for Tooltip.

Problem

With detached triggers (one Popover.Root with a handle, many Popover.Triggers), every trigger subscribes to the store's openChangeReason and openMethod. Both are global to the popover, so every change to either re-renders every trigger, including the ones that didn't open it. This happens when the popover opens or closes, and when the reason changes while it stays open (for example, clicking a trigger after it opened on hover).

Reproduction: https://stackblitz.com/github/willhoney7/base-ui-detached-trigger-rerenders?file=src%2FApp.tsx

The trigger only uses these values to derive two things:

  • Whether hover should be disabled because the popover was opened by a touch press. This is global.
  • Whether this trigger should get the pressed-state attributes. This only matters for the trigger that opened the popover.

Fix

Add two derived selectors to PopoverStore and subscribe to those instead of the raw values:

  • openedByTouchPress: openMethod === 'touch' && openChangeReason === REASONS.triggerPress.
  • isOpenedByTriggerPress(state, triggerId): isOpenedByTrigger(state, triggerId) && openChangeReason === REASONS.triggerPress.

Both return booleans, so a trigger only re-renders when the result for that trigger actually changes. Behavior doesn't change. The open value passed to the state attributes mapping is already isOpenedByTrigger, so the new per-trigger selector is equivalent to the previous value && openReason === REASONS.triggerPress check.

Impact

Measured in jsdom with 5 triggers under StrictMode, counting extra renders per inactive trigger:

Scenario Before After
Click to open 4 2
Escape to close 6 2
Hover open, then click (in test) 4 0

The remaining 2 renders on open and close come from a separate cause shared with PreviewCard and Dialog. The root's interaction hooks mount only while the popup is open, and they swap inactiveTriggerProps when they mount and unmount. Inactive Popover triggers apply those props, so the fix isn't local to Popover. I'll open a separate issue for it.

Tests

Added "does not re-render inactive triggers when the open reason changes" to PopoverRoot.detached-triggers.test.tsx. It runs in Chromium only, alongside the existing multiple-detached-triggers tests. With openOnHover and delay={0}, it opens the popover by hovering trigger 1, then clicks trigger 1. The click keeps the popover open but changes the reason from hover to press. The test then checks that the inactive triggers didn't re-render. It fails without the fix (inactive render counts [14, 14] vs [10, 10]) and passes with it.

… reason changes

Co-authored-by: Cursor <cursoragent@cursor.com>
@pkg-pr-new

pkg-pr-new Bot commented Oct 5, 2026

Copy link
Copy Markdown

commit: dddbbe4

@code-infra-dashboard

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

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+140B(+0.03%) 🔺+26B(+0.02%)

Details of bundle changes

Performance

Total duration: 445.01 ms -59.10 ms(-11.7%) | Renders: 76 (+0) | Paint: 813.71 ms -92.91 ms(-10.2%)

Test Duration Renders
Select mount (200 instances) 53.92 ms ▼-15.21 ms(-22.0%) 3 (+0)
Scroll Area mount (300 instances) 34.44 ms ▼-12.86 ms(-27.2%) 3 (+0)
Select open (500 options) 25.37 ms ▼-7.19 ms(-22.1%) 14 (+0)

12 tests within noise — details


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

@netlify

netlify Bot commented Oct 5, 2026

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

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

Comment on lines 111 to +113
const stateAttributesMapping: StateAttributesMapping<{ open: boolean }> = {
open(value) {
if (value && openReason === REASONS.triggerPress) {
if (isOpenedByThisTriggerPress) {

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.

🟡 Pick the state mapping directly instead of wrapping both mappings in a closure

The wrapper used to be needed because the old condition depended on value (value && openReason === REASONS.triggerPress). The new condition, isOpenedByThisTriggerPress, doesn't depend on value anymore, and when it's true value is always true (isOpenedByTriggerPress implies isOpenedByTrigger). Both mappings return null for false. So the closure only re-implements choosing one of two shared constants. As it is, every render of every trigger allocates a fresh mapping object and closure. The other triggers (Menu, Dialog, PreviewCard, Tooltip) all pass a shared mapping constant instead.

const element = useRenderElement('button', componentProps, {
  // ...
  stateAttributesMapping: isOpenedByThisTriggerPress
    ? pressableTriggerOpenStateMapping
    : triggerOpenStateMapping,
});

Then the StateAttributesMapping type import can go too. I tried this locally: the PopoverTrigger, PopoverRoot and PopoverRoot.detached-triggers suites pass in both Chromium and jsdom, including the existing data-pressed / data-popup-open tests for click, hover and hover-then-click.


🤖 Review generated with Claude Code

@michaldudak michaldudak added performance component: popover Changes related to the popover component. labels Oct 6, 2026
@atomiks

atomiks commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

This PR which I opened a few days ago - #5865 - should fix all of the components together

@willhoney7

Copy link
Copy Markdown
Contributor Author

should I proceed with fixing this one or defer to #5865? Happy to go either way

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Oct 7, 2026
@willhoney7

Copy link
Copy Markdown
Contributor Author

woot, closing

@willhoney7 willhoney7 closed this Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: popover Changes related to the popover component. performance PR: out-of-date The pull request has merge conflicts and can't be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants