Repository navigation
[popover] Avoid re-rendering inactive detached triggers when the open reason changes - #5894
willhoney7 wants to merge 1 commit into
Conversation
… reason changes Co-authored-by: Cursor <cursoragent@cursor.com>
commit: |
Bundle size
PerformanceTotal duration: 445.01 ms -59.10 ms(-11.7%) | Renders: 76 (+0) | Paint: 813.71 ms -92.91 ms(-10.2%)
12 tests within noise — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
| const stateAttributesMapping: StateAttributesMapping<{ open: boolean }> = { | ||
| open(value) { | ||
| if (value && openReason === REASONS.triggerPress) { | ||
| if (isOpenedByThisTriggerPress) { |
There was a problem hiding this comment.
🟡 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
|
This PR which I opened a few days ago - #5865 - should fix all of the components together |
|
should I proceed with fixing this one or defer to #5865? Happy to go either way |
|
woot, closing |
Follow-up to #5893, which fixed the same kind of issue for Tooltip.
Problem
With detached triggers (one
Popover.Rootwith a handle, manyPopover.Triggers), every trigger subscribes to the store'sopenChangeReasonandopenMethod. 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:
Fix
Add two derived selectors to
PopoverStoreand 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
openvalue passed to the state attributes mapping is alreadyisOpenedByTrigger, so the new per-trigger selector is equivalent to the previousvalue && openReason === REASONS.triggerPresscheck.Impact
Measured in jsdom with 5 triggers under StrictMode, counting extra renders per inactive trigger:
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
inactiveTriggerPropswhen 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. WithopenOnHoveranddelay={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.