Skip to content

perf(HoverCard): keep trigger refs stable across rerenders - #6606

Merged
cixzhang merged 1 commit into
meta:mainfrom
ManoharPaturi:fix/hovercard-stable-trigger-refs
Oct 14, 2026
Merged

cixzhang merged 1 commit into
meta:mainfrom
ManoharPaturi:fix/hovercard-stable-trigger-refs

Conversation

@ManoharPaturi

Copy link
Copy Markdown
Contributor

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.

Copilot AI lite review requested due to automatic review settings September 24, 2026 18:52
@vercel

vercel Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Sep 24, 2026 6:58pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Sep 24, 2026
github-actions Bot added a commit that referenced this pull request Sep 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.9KB 1.2KB

Accessibility Audit

Status: No accessibility violations detected.

Visual Regression

25 of 420 shot(s) changed. View the report

A change here is a question, not a failure: check whether the after is the
picture you intended. Record that review on the PR. Baseline maintenance is an
explicit dispatch of CI; this report never rewrites the baseline or adds a release gate.

component story theme mode pixels
Chat Full AI Chat probe light 42,708
Chat Empty State probe light 42,708
Chat Full AI Chat probe dark 41,786
Chat Empty State probe dark 41,786
Slider Default probe light 15,617
Slider Default probe dark 15,615
Chat With Attachments probe light 12,964
Chat With Attachments probe dark 12,114
InputGroup With Typeahead probe light 11,012
InputGroup With Typeahead probe dark 9,189
ComplexSelector Tree list with search probe light 6,245
TextArea Vertical Form Alignment probe light 5,634
ComplexSelector Tree list with search probe dark 5,602
TextArea Vertical Form Alignment probe dark 5,047
ComplexSelector Fruit ripeness selector neutral light 4,007
ComplexSelector Fruit ripeness selector neutral dark 3,825
ToggleButton Group Single probe dark 1,538
ToggleButton Group Single probe light 1,538
MediaTheme On Dark neutral dark 838
Banner Collapsible Content (Expanded) probe dark 92

and 5 more.

Chat — Full AI Chat — probe light
BeforeAfterDiff
Before visual regression frame After visual regression frame Pixel difference frame
Chat — Empty State — probe light
BeforeAfterDiff
Before visual regression frame After visual regression frame Pixel difference frame
Chat — Full AI Chat — probe dark
BeforeAfterDiff
Before visual regression frame After visual regression frame Pixel difference frame

Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@astracat-bot

astracat-bot Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Sep 24, 2026, 11:53 PM UTC

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — this is a clean, well-scoped performance fix. The regression tests cover the ref-stability and listener-rebind behavior directly, and the full HoverCard suite, type checks, and lint are clean. Approving with no changes requested.

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Oct 14, 2026
@cixzhang
cixzhang merged commit 9b386fa into meta:main Oct 14, 2026
44 checks passed
cixzhang pushed a commit that referenced this pull request Oct 31, 2026
…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.

This branch was successfully deployed

1 active deployment
Preview — 38319af0 Deployed Sep 24, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HoverCard trigger ref resets on every rerender (useTouchTrigger returns a new object)

3 participants