mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-19 06:23:32 +00:00
⚡ perf: Render Each Tooltip Anchor Once on Mount (#18092)
`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.
This commit is contained in:
parent
2e6b23cb08
commit
5b9d6f2226
4 changed files with 60 additions and 10 deletions
|
|
@ -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(
|
||||
<TooltipAnchor description="Once" aria-label="Once" side={side}>
|
||||
<span>icon</span>
|
||||
</TooltipAnchor>,
|
||||
);
|
||||
|
||||
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(
|
||||
<TooltipAnchor description="Opens" aria-label="Opens">
|
||||
<span>icon</span>
|
||||
</TooltipAnchor>,
|
||||
);
|
||||
|
||||
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 () => {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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<T extends string>(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. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue