Skip to content

perf(Tooltip): keep trigger refs stable across rerenders - #5951

Merged
freddymeta merged 3 commits into
mainfrom
fix/tooltip-ref-stability
Sep 3, 2026
Merged

freddymeta merged 3 commits into
mainfrom
fix/tooltip-ref-stability

Conversation

@freddymeta

@freddymeta freddymeta commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • keep useTooltip trigger refs stable when an unchanged render produces new hook result objects
  • depend on the specific layer and touch callbacks each handler uses instead of the enclosing objects
  • add a failing-first identity regression and RTL coverage for Tooltip's logical placement

Closes #5949.

Evidence

Before the fix, the new regression failed because result.current.ref changed after an unchanged rerender(). After the fix, both the combined ref and interaction ref retain their identities.

The Tooltip RTL audit now measures logical start placement and reports:

COV : 1 measured / 0 verified N-A / 0 gap / 0 stale

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.

DOM wiring operation Before After
listener detach 600 0
listener attach 600 0
aria-describedby remove 100 0
aria-describedby set 100 0
measured total 1,400 0

That 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 passed
  • pnpm -F @astryxdesign/core typecheck — passed
  • pnpm lint:strict — passed with no errors
  • pnpm build — passed
  • ~/projects/astryx-preflight.sh <worktree> Tooltip — passed: typecheck, lint, 8,695 core tests, core + Storybook build, RTL audit, Chromium guards, and repository checks

Full-workspace pnpm test was 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 untouched origin/main control also fails the full suite on the existing score-ledger timeout 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.

@vercel

vercel Bot commented Sep 3, 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 3, 2026 12:31am UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 3, 2026
@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 3, 2026
@freddymeta
freddymeta marked this pull request as ready for review September 3, 2026 00:38
github-actions Bot added a commit that referenced this pull request Sep 3, 2026
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

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

🧪 Sandbox Preview

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

Modified Components

Tooltip (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 544 -
Complexity N/A Very High (56) -

Bundle Size Summary

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

Accessibility Audit

Status: No accessibility violations detected.

Visual Regression

12 added · 0 removed. View the report

A repository maintainer can accept these exact frames: /accept-visual 33699573388/1 <why every changed frame is correct>

Added — After
Added — After visual regression frame

Added — After
Added — After visual regression frame

Added — After
Added — After visual regression frame


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

@cixzhang cixzhang 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 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]

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Sep 3, 2026
@freddymeta
freddymeta merged commit 4ec74d9 into main Sep 3, 2026
30 of 31 checks passed
@freddymeta
freddymeta deleted the fix/tooltip-ref-stability branch September 3, 2026 01:41
cixzhang pushed a commit that referenced this pull request Oct 14, 2026
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.
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 — d7cb09aa Deployed Sep 3, 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.

Tooltip callback ref resets on unchanged rerenders

2 participants