From 5b9d6f22266b9050bdd080d4b6dc8143dc9bf53d Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Thu, 12 Nov 2026 11:18:48 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20perf:=20Render=20Each=20Tooltip=20A?= =?UTF-8?q?nchor=20Once=20on=20Mount=20(#18092)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `TooltipAnchor` subscribed to its store's `anchorElement` and `open` to resolve a physical `left`/`right` side against the anchor's direction. `anchorElement` changes from null to the element as soon as the anchor attaches, so every anchor rendered its provider tree a second time right after mounting, and again on every open, even though the resolved side only ever differs for a `left`/`right` side under RTL. The anchor now selects the resolved side itself. Ariakit re-runs the selector on every store change, so opening still re-resolves after a language switch flips the direction without a remount, but the anchor only re-renders when the answer changes. `resolveSide` also returns a `top`/`bottom` side before reading the computed direction, since those never mirror and the selector now runs on each store change. Message rows mount hundreds of these anchors (copy, edit, fork, regenerate, feedback). On the conversation-navigation benchmark's warm switch this removes about 1,450 of 10,700 component renders. --- .../client/src/components/Tooltip.spec.tsx | 38 +++++++++++++++++++ packages/client/src/components/Tooltip.tsx | 15 +++++--- packages/client/src/utils/direction.spec.ts | 9 +++++ packages/client/src/utils/direction.ts | 8 ++-- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/packages/client/src/components/Tooltip.spec.tsx b/packages/client/src/components/Tooltip.spec.tsx index 2df97af83e..aeaaa9b6de 100644 --- a/packages/client/src/components/Tooltip.spec.tsx +++ b/packages/client/src/components/Tooltip.spec.tsx @@ -1,9 +1,47 @@ import React from 'react'; import '@testing-library/jest-dom'; +import * as Ariakit from '@ariakit/react'; import { render, screen, fireEvent } from '@testing-library/react'; import { TooltipAnchor } from './Tooltip'; describe('TooltipAnchor', () => { + /** Message rows mount hundreds of anchors in one commit; an anchor that renders its provider + * tree again once its own element attaches doubles the cost of that whole commit. */ + describe('render cost', () => { + test.each(['top', 'right'] as const)( + 'renders a %s anchor once on mount in a left-to-right layout', + (side) => { + const provider = jest.spyOn(Ariakit, 'TooltipProvider'); + render( + + icon + , + ); + + expect(screen.getByLabelText('Once')).toBeInTheDocument(); + expect(provider).toHaveBeenCalledTimes(1); + }, + ); + + test('opens its tooltip without rendering the anchor again', async () => { + const provider = jest.spyOn(Ariakit, 'TooltipProvider'); + render( + + icon + , + ); + + const anchor = screen.getByLabelText('Opens'); + fireEvent.mouseEnter(anchor); + fireEvent.mouseMove(anchor); + + expect(await screen.findByRole('tooltip', undefined, { timeout: 3000 })).toHaveTextContent( + 'Opens', + ); + expect(provider).toHaveBeenCalledTimes(1); + }); + }); + describe('zIndex', () => { /** Lets a tooltip clear a host layer above the stylesheet's default (150). */ test('applies the zIndex prop to the tooltip popup', async () => { diff --git a/packages/client/src/components/Tooltip.tsx b/packages/client/src/components/Tooltip.tsx index d62e786dd6..7a02a17f15 100644 --- a/packages/client/src/components/Tooltip.tsx +++ b/packages/client/src/components/Tooltip.tsx @@ -150,15 +150,18 @@ export const TooltipAnchor: ForwardRefExoticComponent< ref, ) { const tooltip = Ariakit.useTooltipStore({ placement: side }); - const anchorElement = Ariakit.useStoreState(tooltip, 'anchorElement'); - const open = Ariakit.useStoreState(tooltip, 'open'); /** `side` is physical; resolve it against the anchor's own direction so `right` stays the - * inline-end side in an RTL layout without every caller having to know. Re-resolved on each - * open, since a language switch flips the document direction without remounting. */ + * inline-end side in an RTL layout without every caller having to know. The selector re-runs + * on every store change, opening included, so a language switch that flips the direction + * without remounting is still picked up. Selecting the resolved side, not the anchor element, + * keeps each anchor to one render on mount: message rows mount hundreds of them at once. */ + const placement = Ariakit.useStoreState(tooltip, (state) => + resolveSide(side, state.anchorElement), + ); useLayoutEffect(() => { - tooltip.setState('placement', resolveSide(side, anchorElement)); - }, [tooltip, side, anchorElement, open]); + tooltip.setState('placement', placement); + }, [tooltip, placement]); /** * `role="button"` renders a plain element with no native activation, so Enter and diff --git a/packages/client/src/utils/direction.spec.ts b/packages/client/src/utils/direction.spec.ts index 42e538a8e1..ff2ebe61eb 100644 --- a/packages/client/src/utils/direction.spec.ts +++ b/packages/client/src/utils/direction.spec.ts @@ -37,6 +37,15 @@ describe('direction helpers', () => { expect(resolveSide('left-end', undefined)).toBe('left-end'); }); + it('reads no styles for a top or bottom side, which never mirrors', () => { + const rtl = mount('rtl'); + const getComputedStyle = jest.spyOn(window, 'getComputedStyle'); + expect(resolveSide('top', rtl)).toBe('top'); + expect(resolveSide('bottom-start', rtl)).toBe('bottom-start'); + expect(getComputedStyle).not.toHaveBeenCalled(); + getComputedStyle.mockRestore(); + }); + it('swaps horizontal arrow keys for an RTL element only', () => { const rtl = mount('rtl'); const ltr = mount('ltr'); diff --git a/packages/client/src/utils/direction.ts b/packages/client/src/utils/direction.ts index 9689ad587d..c0f0e6e5bb 100644 --- a/packages/client/src/utils/direction.ts +++ b/packages/client/src/utils/direction.ts @@ -19,12 +19,12 @@ export function isRtl(element?: Element | null): boolean { * is visually equivalent under RTL; `top`/`bottom` and LTR anchors pass through unchanged. * The `-start`/`-end` suffix is left alone: floating positioning already resolves it per direction. */ export function resolveSide(side: T, element?: Element | null): T { - if (!isRtl(element)) { - return side; - } const [base, ...rest] = side.split('-'); const opposite = OPPOSITE[base]; - return (opposite ? [opposite, ...rest].join('-') : side) as T; + if (!opposite || !isRtl(element)) { + return side; + } + return [opposite, ...rest].join('-') as T; } /** Swaps ArrowLeft and ArrowRight for horizontal navigation when the element is RTL. */