Repository navigation
perf(Tooltip): keep trigger refs stable across rerenders - #5951
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR Modified ComponentsTooltip (@astryxdesign/core) · View in Storybook
Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Visual Regression12 added · 0 removed. View the report A repository maintainer can accept these exact frames: Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
cixzhang
left a comment
There was a problem hiding this comment.
Thanks, this keeps Tooltip’s trigger wiring stable without changing its API or interaction behavior. The focused red→green regression and option-change paths pass.
[Reviewed by Robohands]
Fixes #6472 ## Summary `useHoverCard()` returned a fresh `ref`/`interactionRef` on every render: its callbacks depended on the whole `layer` and `touch` objects, and `useLayer`/`useTouchTrigger` rebuild those object literals every render even when nothing inside them changed. React then called the old ref with `null` and 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 inline `anchor-name` style. Same root cause as #5949, which #5951 fixed for Tooltip. This applies the same fix to HoverCard: destructure the stable `layer`/`touch` members once and depend on those instead of the enclosing objects. ## Tests - Identity regression: `renderHook(() => useHoverCard())`, unchanged `rerender()`, compare `ref`/`interactionRef` — both failed on main, pass here. - Listener-rebind regression: spying `addEventListener` on a mounted trigger, an unchanged rerender re-attached all four trigger listeners on main (`pointerenter`, `mouseenter`, `mouseleave`, `pointerdown`); after the fix it attaches none. - Full HoverCard suite: 46 passed (44 pre-existing, unchanged). ## Verification `tsc --noEmit` clean (core); eslint 0 problems on touched files; prettier clean; `check:changesets` clean. Changeset included.
…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.



Summary
useTooltiptrigger refs stable when an unchanged render produces new hook result objectsCloses #5949.
Evidence
Before the fix, the new regression failed because
result.current.refchanged after an unchangedrerender(). After the fix, both the combined ref and interaction ref retain their identities.The Tooltip RTL audit now measures logical
startplacement and reports:Before vs after calculation
Measured with the same Tooltip wrapper on the parent commit and this PR, across 100 semantically unchanged rerenders. The harness spies on the actual trigger element and counts only Tooltip-owned event types.
aria-describedbyremovearia-describedbysetThat is 14 measured DOM calls per rerender → 0 (100% reduction). The callback ref also removes and restores the CSS
anchor-name, so the full known rewiring is 16 DOM mutations/calls per rerender → 0.This is a deterministic operation count, not a wall-clock claim; the short Vitest/jsdom run is dominated by harness overhead.
Test plan
pnpm exec vitest run packages/core/src/Tooltip/Tooltip.test.tsx— 25 passedpnpm -F @astryxdesign/core typecheck— passedpnpm lint:strict— passed with no errorspnpm build— passed~/projects/astryx-preflight.sh <worktree> Tooltip— passed: typecheck, lint, 8,695 core tests, core + Storybook build, RTL audit, Chromium guards, and repository checksFull-workspace
pnpm testwas also run twice. Each run passed over 14,000 tests and failed only the ContextMenu BottomSheet timing cases, which pass as a 48-test isolated file. An untouchedorigin/maincontrol also fails the full suite on the existingscore-ledgertimeout assertion.Visual verification
No production rendering changed. The added default-open RTL story verifies the existing logical placement in Chromium; the scoped RTL audit passes.