Repository navigation
perf(HoverCard): keep trigger refs stable across rerenders - #6606
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
PR Analysis Report📚 Storybook Preview🧪 Sandbox PreviewView Sandbox for this PR No new or modified components detected. Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Visual Regression25 of 420 shot(s) changed. View the report A change here is a question, not a failure: check whether the after is the
and 5 more. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
|
AI review status for this pull request.
|
There was a problem hiding this comment.
Thanks — this is a clean, well-scoped fix.
useHoverCard() was returning a fresh ref/interactionRef on every render because its callbacks depended on the whole layer and touch objects, so React kept detaching and re-attaching the trigger listeners and rewriting the trigger's anchor-name. Destructuring the stable members and depending on those instead fixes the churn while leaving the hook's API, defaults, and hover/focus/tap/Escape behavior unchanged — consistent with the current guidance to keep callback refs reference-stable when practical.
The two new regression tests are a good fit: one pins ref identity across an unchanged rerender, and one proves a mounted trigger gets no listener rebinds. The full HoverCard suite passes (46 tests), and type and lint checks on the touched files are clean. The patch Changeset is included and correctly scoped.
Approving — no changes requested.
[Automated review]
…rs (#9130) ## User impact A `DropdownMenu` whose trigger also has a tooltip (a custom trigger in `Tooltip`, or the default button's `tooltip`) rewrites that button's inline style on every render around it. The menu's anchor comes off and goes back, so `anchor-name` is rewritten and its two names swap order. A page that renders often never settles. We found it moving one app onto Astryx. ## Problem and solution fit - `DropdownMenu`'s trigger ref was a new function each render, so React called it again on every commit (`null`, then the element), moving the anchor each time. - Stabilizing those refs alone was not enough. They wrap `usePopover`'s `triggerRef`, keyed on the whole layer object, which changes whenever `onShow` or `onHide` does; `DropdownMenu`'s `onShow` changes with an inline `renderTrigger`, `onClick` or `onOpenChange`. That version still made 2 and 4 writes per render in the tests below. - So the fix is in the hook: ```ts const layerRef = layer.ref; const triggerRef = useCallback( (el: HTMLElement | null) => { triggerElementRef.current = el; layerRef(el); }, [layerRef], ); ``` The callback reads nothing else from the layer, and `layer.ref` (`useLayer`'s anchor callback, keyed on the anchor name and anchor-clearance option) reads only refs, so nothing goes stale. ## Expected behavior and authority Tooltip (#5951) and `useHoverCard` (#6606) got the same fix: a trigger ref keeps its identity when nothing about the trigger changes. No API or record changes. ## Smallest restoration - `usePopover`: `triggerRef` is keyed on `layer.ref` instead of `layer`. - `DropdownMenu`: stable refs on its three trigger paths, a custom trigger, the default button (still forwarding the caller's `ref`) and the bottom-sheet trigger. - Triggers that take `usePopover`'s ref directly get it stable too: ComplexSelector's built-in trigger, DateTimeInput's date field and Breadcrumbs. Not chosen: stabilizing each `DropdownMenu` callback instead, leaving every other `usePopover` caller churning. Also edited by open #7677 and #8550, both of which merge cleanly with this change. ## Evidence - Before (main eae5d92): one parent render writes the trigger's style 2 times with a custom trigger in `Tooltip`, and 4 times with the default button's `tooltip` and an inline `onOpenChange`. The two anchor names swap order, and `usePopover`'s `triggerRef` changes whenever `onShow` or `onHide` does. - After: 0 writes, the same order, the same ref. A caller's `ref` on the default button still gets the element. ## Scope - [x] One defect is restored. - [x] Separable contradictory or unsettled tagalongs were removed or split. - [x] No new public API, visual representation, default, or interaction model is hidden under the bug-fix label. - [x] Public text and artifacts contain no internal Meta context. ## Testing How to check: 1. Render `<DropdownMenu button={{label: 'Actions', tooltip: 'More'}} onOpenChange={() => {}}>` inside a component that renders every second. 2. Watch the button's `style` attribute in the browser's Elements panel. Before: it flashes every second. After: it stays still. The new cases in `DropdownMenu.test.tsx` and `Popover.test.tsx` count the trigger's style writes with a MutationObserver. They fail on main and pass with the fix. Local gates at 08a5dc5: - The DropdownMenu, Popover, Tooltip and Layer suites, plus every other component that uses `usePopover`'s trigger ref (Breadcrumbs, Chat, ComplexSelector, DateInput, DateRangeInput, DateTimeInput, MultiSelector): 2,171 tests pass. The 6 new cases fail on main. - The DropdownMenu, Tooltip and Popover Chromium specs: 217 passed. - Core typecheck, ESLint with no warnings, Prettier, `pnpm check:repo` and the Storybook build. - `pnpm test:ui` and `pnpm test:node`: the same failures as on main, none in these components.









Fixes #6472
Summary
useHoverCard()returned a freshref/interactionRefon every render: its callbacks depended on the wholelayerandtouchobjects, anduseLayer/useTouchTriggerrebuild those object literals every render even when nothing inside them changed. React then called the old ref withnulland the new one with the element, so every rerender detached and re-attached all four trigger listeners and removed and re-applied the trigger's inlineanchor-namestyle.Same root cause as #5949, which #5951 fixed for Tooltip. This applies the same fix to HoverCard: destructure the stable
layer/touchmembers once and depend on those instead of the enclosing objects.Tests
renderHook(() => useHoverCard()), unchangedrerender(), compareref/interactionRef— both failed on main, pass here.addEventListeneron a mounted trigger, an unchanged rerender re-attached all four trigger listeners on main (pointerenter,mouseenter,mouseleave,pointerdown); after the fix it attaches none.Verification
tsc --noEmitclean (core); eslint 0 problems on touched files; prettier clean;check:changesetsclean. Changeset included.