Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(TabList): a press measures the selected tab in the next frame, not inside the press (#8856)

Cindy Zhang09382da26bad
diff --git a/.changeset/tablist-press-reveal-next-frame.md b/.changeset/tablist-press-reveal-next-frame.md
new file mode 100644
index 0000000000..13e1807054
--- /dev/null
+++ b/.changeset/tablist-press-reveal-next-frame.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] TabList: pressing or arrowing to a tab in a scrolling strip no longer measures the strip inside the press. Focusing the tab already scrolls it into view, so the check that keeps the selected tab whole now runs in the next frame instead of forcing the new selection's style and layout before the press can paint.
+
+@cixzhang
diff --git a/packages/core/src/TabList/TabList.test.tsx b/packages/core/src/TabList/TabList.test.tsx
index b35949ed5c..b4b454c9dc 100644
--- a/packages/core/src/TabList/TabList.test.tsx
+++ b/packages/core/src/TabList/TabList.test.tsx
@@ -9,8 +9,9 @@
* SYNC: When TabList components change, update tests to match new behavior
*/
+import {useState} from 'react';
import {describe, it, expect, vi, beforeAll, afterAll} from 'vitest';
-import {render, screen, fireEvent} from '@testing-library/react';
+import {act, render, screen, fireEvent} from '@testing-library/react';
import {__resetMenuPressForTest} from '../hooks/useMenuPress';
import userEvent from '@testing-library/user-event';
import {
@@ -1237,6 +1238,56 @@ describe('TabList overflow (scroll)', () => {
expect(scrollBy).not.toHaveBeenCalled();
});
+ it('measures a pressed tab after the press, in the next frame, and still shows it whole', async () => {
+ function Controlled() {
+ const [value, setValue] = useState('a');
+ return (
+ <TabList value={value} onChange={setValue}>
+ <Tab value="a" label="Alpha" />
+ <Tab value="b" label="Beta" />
+ <Tab value="c" label="Gamma" />
+ </TabList>
+ );
+ }
+ const {container} = render(<Controlled />);
+ const strip = container.querySelector<HTMLElement>(STRIP);
+ const tab = container.querySelector<HTMLElement>('[data-tab-value="c"]');
+ if (!strip || !tab) {
+ throw new Error('no tab strip');
+ }
+ const scrollBy = vi.fn();
+ strip.scrollBy = scrollBy;
+ const stripRect = vi.fn(
+ () => ({left: 0, right: 300, width: 300}) as DOMRect,
+ );
+ strip.getBoundingClientRect = stripRect;
+ tab.getBoundingClientRect = () =>
+ ({left: 240, right: 320, width: 80}) as DOMRect;
+
+ // Pressing a tab focuses it first, and focusing it reveals it.
+ fireEvent.focus(tab);
+ expect(scrollBy).toHaveBeenCalledTimes(1);
+ expect(scrollBy.mock.calls[0][0].left).toBe(20);
+ const readsBeforePress = stripRect.mock.calls.length;
+
+ // The selection itself reads no geometry inside the press...
+ fireEvent.click(tab);
+ expect(tab).toHaveAttribute('aria-current', 'true');
+ expect(stripRect.mock.calls.length).toBe(readsBeforePress);
+ expect(scrollBy).toHaveBeenCalledTimes(1);
+
+ // ...and the next frame measures the selected tab, now wider, so it still
+ // ends fully inside the strip.
+ tab.getBoundingClientRect = () =>
+ ({left: 220, right: 306, width: 86}) as DOMRect;
+ await act(
+ async () =>
+ new Promise<void>(resolve => requestAnimationFrame(() => resolve())),
+ );
+ expect(scrollBy).toHaveBeenCalledTimes(2);
+ expect(scrollBy.mock.calls[1][0].left).toBe(6);
+ });
+
it('reveals on a selection change, not on an unrelated overflow change', () => {
const {strip, rerender} = renderStrip({value: 'c', overflow: 'visible'});
const scrollBy = vi.fn();
diff --git a/packages/core/src/TabList/TabList.tsx b/packages/core/src/TabList/TabList.tsx
index 65b8c12086..2449661b3a 100644
--- a/packages/core/src/TabList/TabList.tsx
+++ b/packages/core/src/TabList/TabList.tsx
@@ -515,6 +515,10 @@ export function TabList({
revealSelectedTabRef.current = revealSelectedTab;
}, [revealSelectedTab]);
+ // The tab whose focus revealed it (see `handleRootFocus`), while it keeps
+ // focus.
+ const focusRevealedValueRef = useRef<string | null>(null);
+
// The tab you are on has to be visible. Selection can move without focus —
// on mount, or when the host sets `value` itself — and neither scrolls the
// strip the way clicking or arrowing to a tab does. The check is what makes
@@ -525,6 +529,14 @@ export function TabList({
return;
}
revealedValueRef.current = value;
+ if (value != null && focusRevealedValueRef.current === value) {
+ // A press or an arrow focused this tab first, and focusing it revealed
+ // it. Measuring now would force the new selection's style and layout
+ // inside the press, before it can paint. The next frame lays them out
+ // anyway, so finish there: the selected weight can still widen the tab.
+ const frame = requestAnimationFrame(() => revealSelectedTabRef.current());
+ return () => cancelAnimationFrame(frame);
+ }
revealSelectedTabRef.current();
}, [value]);
@@ -595,7 +607,9 @@ export function TabList({
// The browser scrolls a focused element into view only when it is
// entirely outside the scrollport, so arrowing onto a half-visible stop
// leaves it cut off under the fade. Finish the job it started.
- revealStop(e.target.closest(TAB_STOP_SELECTOR));
+ const stop = e.target.closest<HTMLElement>(TAB_STOP_SELECTOR);
+ revealStop(stop);
+ focusRevealedValueRef.current = stop?.dataset.tabValue ?? null;
},
[onFocusProp, onHintFocus, handleFocus, revealStop],
);
@@ -603,6 +617,7 @@ export function TabList({
const handleRootBlur = useCallback(
(e: React.FocusEvent<HTMLElement>) => {
onBlurProp?.(e);
+ focusRevealedValueRef.current = null;
if (e.defaultPrevented) {
return;
}