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. */