Repository navigation
[tooltip] Avoid re-rendering inactive detached triggers on open and close - #5893
willhoney7 wants to merge 2 commits into
Conversation
…lose Co-authored-by: Cursor <cursoragent@cursor.com>
commit: |
Bundle size
PerformanceTotal duration: 448.01 ms -8.22 ms(-1.8%) | Renders: 76 (+0) | Paint: 817.76 ms -15.48 ms(-1.9%)
13 tests within noise — details Metric alarms
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. |
these numbers seem off? I don't know how Select or Slider or any of these could be worse from my changes... |
|
Thanks for your contribution! Don't worry about the perf test results as they are sometimes inaccurate. |
…ggerProps Co-authored-by: Cursor <cursoragent@cursor.com>
done! should be good to go now |
|
I think this has been superseded! Going to close, will reopen if needed |
Problem
With detached triggers (one
Tooltip.Rootwith a handle, manyTooltip.Triggers), opening or closing the tooltip re-renders every trigger, not just the one that opened it.Reproduction: https://stackblitz.com/github/willhoney7/base-ui-detached-trigger-rerenders?file=src%2FApp.tsx
Each trigger subscribes to
store.useState('triggerProps', isMountedByThisTrigger), which returnsinactiveTriggerPropsfor inactive triggers. The root's interaction hooks only mount while the tooltip is open (open || mounted). While they are mounted,usePopupInteractionPropssyncsuseDismiss's trigger handlers intoinactiveTriggerProps. On unmount it resets them toEMPTY_OBJECT. So the object identity changes on every open and every close, and every inactive trigger re-renders.But inactive triggers only apply the root trigger props when
trackCursorAxis !== 'none'. OtherwiseTooltipTriggerthrows them away, so in the default case those re-renders do nothing.Fix
Override the shared
triggerProps(state, isActive)selector inTooltipStore:activeTriggerProps.inactiveTriggerPropsonly when cursor tracking is on, andundefinedotherwise.Because the selector returns a stable
undefinedin the default case, inactive triggers no longer re-render when the props object swaps. This replacesTooltipTrigger'sshouldApplyRootTriggerPropscheck, so behavior doesn't change.Impact
Measured in jsdom with 5 triggers under StrictMode, counting extra renders per inactive trigger:
We hit this in an app with about 2,000 tooltip triggers on a page sharing one handle. Each hover re-rendered all of them, twice per hover (once on open, once on close). We're currently patching around it locally.
Tests
Added "does not re-render inactive triggers when the tooltip opens and closes" to
TooltipRoot.detached-triggers.test.tsx. It runs in Chromium only, alongside the existing multiple-detached-triggers tests. It fails without the fix (inactive render counts[12, 12]vs[4, 4]) and passes with it.Related
Popover, PreviewCard, and Dialog share the same root cause: interaction hooks that mount only while open and swap
inactiveTriggerProps. Because inactive triggers there do apply the props, the fix there is less local. I'll open a separate issue for that, plus a separate PR for an additional Popover-only cause (triggers subscribing toopenChangeReasonandopenMethod).