An open source design system that's fully customizable and agent ready
TypeScript
fix(lab): a pinch that starts on a reorder grip zooms the page (#8126)
Christopher Chedeau9effa5cb5a88
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 3ea307eb17..fbc9b9ae6c 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -1525,6 +1525,17 @@ jobs:--project chromium \--output disabled-trigger-focus-output+ # A pinch that starts on, or reaches, a Lab reorder grip (ListInput,+ # TransferList) must zoom the page (WCAG 1.4.4), while a one-finger drag+ # on the grip still reorders. touch-action and pinch-zoom only exist in a+ # real engine with two-finger touch input.+ - name: Run Lab reorder grip pinch-zoom contract+ run: |+ pnpm exec playwright test \+ packages/lab/src/__tests__/ReorderGripPinchZoom.a11y.chromium.spec.ts \+ --project chromium \+ --output reorder-grip-pinch-zoom-output+# Table merges a consumer min-width with its column floors through CSS# max(). jsdom keeps any string, so only a real engine shows whether the# emitted declaration parses; an invalid one drops the floors entirely.diff --git a/packages/lab/src/ListInput/ListInput.test.tsx b/packages/lab/src/ListInput/ListInput.test.tsxindex c3f915e3b5..f358221a2a 100644--- a/packages/lab/src/ListInput/ListInput.test.tsx+++ b/packages/lab/src/ListInput/ListInput.test.tsx@@ -1195,6 +1195,32 @@ describe('ListInput', () => {).not.toBeInTheDocument();});+ it('cancels a pointer reorder when a second pointer lands, as a pinch does', () => {+ const onChange = vi.fn();+ const {container} = renderListInput({isReorderable: true, onChange});+ const handle = screen.getByRole('button', {name: 'Reorder guest 1'});++ fireEvent.pointerDown(handle, {+ button: 0,+ pointerId: 8,+ clientX: 10,+ clientY: 10,+ });+ fireEvent.pointerMove(handle, {pointerId: 8, clientX: 10, clientY: 34});+ expect(+ container.querySelector('[data-list-input-reorder-source]'),+ ).toBeInTheDocument();++ // The second finger of a pinch lands elsewhere on the page.+ fireEvent.pointerDown(document.body, {pointerId: 9, clientX: 200});+ expect(+ container.querySelector('[data-list-input-reorder-source]'),+ ).not.toBeInTheDocument();++ fireEvent.pointerUp(handle, {pointerId: 8, clientX: 10, clientY: 90});+ expect(onChange).not.toHaveBeenCalled();+ });+it('clears a sub-threshold pointer drag without reordering', () => {const onChange = vi.fn();const {container} = renderListInput({isReorderable: true, onChange});diff --git a/packages/lab/src/ListInput/ListInput.tsx b/packages/lab/src/ListInput/ListInput.tsxindex 80f1cdbd5a..0c3cc0946a 100644--- a/packages/lab/src/ListInput/ListInput.tsx+++ b/packages/lab/src/ListInput/ListInput.tsx@@ -809,6 +809,29 @@ export function ListInput<T>({}}, [isDisabled, isLoading, announce, t]);+ // A second pointer that lands anywhere during a pointer reorder (the second+ // finger of a pinch) cancels the reorder. A pinch whose first finger slid+ // first zooms without a pointercancel, so the drag would otherwise commit on+ // release and drop the row at the end of the list.+ useEffect(() => {+ if (!isPointerReorderActive) {+ return;+ }+ const onPointerDown = (event: PointerEvent) => {+ const activeState = reorderStateRef.current;+ if (+ activeState?.mode === 'pointer' &&+ activeState.pointerId !== event.pointerId+ ) {+ setReorderState(null);+ announce(t('@astryx.listInput.announceReorderCancelled'));+ }+ };+ document.addEventListener('pointerdown', onPointerDown, true);+ return () =>+ document.removeEventListener('pointerdown', onPointerDown, true);+ }, [isPointerReorderActive, announce, t]);+useEffect(() => {const activeAnimations = activeMutationAnimationsRef.current;return () => {diff --git a/packages/lab/src/TransferList/TransferList.test.tsx b/packages/lab/src/TransferList/TransferList.test.tsxindex 020ec76249..af83934642 100644--- a/packages/lab/src/TransferList/TransferList.test.tsx+++ b/packages/lab/src/TransferList/TransferList.test.tsx@@ -534,6 +534,42 @@ describe('TransferList', () => {expect(screen.getByTestId('popover-host')).toContainElement(layer);});+ it('cancels a pointer reorder when a second pointer lands, as a pinch does', () => {+ const onValueChange = vi.fn();+ const {container} = render(+ <ControlledTransferList+ initialValue={['name', 'owner', 'status']}+ onValueChange={onValueChange}+ />,+ );+ const rows = [+ ...container.querySelectorAll<HTMLElement>('[data-transfer-list-row]'),+ ];+ mockRowBounds(rows);+ const handle = screen.getByRole('button', {name: 'Reorder Name'});++ fireEvent.pointerDown(handle, {+ button: 0,+ pointerId: 7,+ clientX: 300,+ clientY: 10,+ });+ fireEvent.pointerMove(handle, {pointerId: 7, clientX: 300, clientY: 34});+ expect(+ container.querySelector('[data-transfer-list-reorder-source]'),+ ).toBeInTheDocument();++ // The second finger of a pinch lands elsewhere on the page.+ fireEvent.pointerDown(document.body, {pointerId: 8, clientX: 600});+ expect(+ container.querySelector('[data-transfer-list-reorder-source]'),+ ).not.toBeInTheDocument();++ fireEvent.pointerMove(handle, {pointerId: 7, clientX: 300, clientY: 140});+ fireEvent.pointerUp(handle, {pointerId: 7, clientX: 300, clientY: 140});+ expect(onValueChange).not.toHaveBeenCalled();+ });+it('keeps rows stationary, locks X, follows pointer Y, and commits once', () => {const onValueChange = vi.fn();const {container} = render(diff --git a/packages/lab/src/TransferList/TransferList.tsx b/packages/lab/src/TransferList/TransferList.tsxindex c80090da65..13021ba90d 100644--- a/packages/lab/src/TransferList/TransferList.tsx+++ b/packages/lab/src/TransferList/TransferList.tsx@@ -994,6 +994,30 @@ export function TransferList<T extends string = string>({[finishReorder],);+ // A second pointer that lands anywhere during a pointer reorder (the second+ // finger of a pinch) cancels the reorder. A pinch whose first finger slid+ // first zooms without a pointercancel, so the drag would otherwise commit on+ // release and drop the row at the end of the list.+ const isPointerReorderActive = previewCloneValue != null;+ useEffect(() => {+ if (!isPointerReorderActive) {+ return;+ }+ const onPointerDown = (event: PointerEvent) => {+ const session = reorderSessionRef.current;+ if (+ session?.mode === 'pointer' &&+ session.pointerId !== event.pointerId+ ) {+ suppressClickRef.current = false;+ finishReorder(true);+ }+ };+ document.addEventListener('pointerdown', onPointerDown, true);+ return () =>+ document.removeEventListener('pointerdown', onPointerDown, true);+ }, [isPointerReorderActive, finishReorder]);+const handleSearch = useCallback((nextQuery: string) => {setQuery(nextQuery);diff --git a/packages/lab/src/__tests__/ReorderGripPinchZoom.a11y.chromium.spec.ts b/packages/lab/src/__tests__/ReorderGripPinchZoom.a11y.chromium.spec.tsnew file mode 100644index 0000000000..32de4fe463--- /dev/null+++ b/packages/lab/src/__tests__/ReorderGripPinchZoom.a11y.chromium.spec.ts@@ -0,0 +1,177 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file ReorderGripPinchZoom.a11y.chromium.spec.ts+ * @input The checked-in Lab/ListInput › Tag Options and Lab/TransferList ›+ * Default stories, a built Storybook and real Chromium touch input (CDP) on+ * a phone viewport+ * @output Browser evidence that a pinch which starts on, or reaches, a reorder+ * grip zooms the page and leaves the order as it was, while a one-finger+ * drag on the grip still reorders (WCAG 1.4.4)+ * @position Chromium proof for the shared Lab reorder grip's `touch-action`+ * (packages/lab/src/reorderStyles.ts)+ */++import {expect, test, type CDPSession, type Page} from '@playwright/test';+import {+ DEFAULT_STORYBOOK_DIR,+ serveStorybook,+ type StaticServer,+} from '@astryxdesign/a11y-spec/storybook';++let storybook: StaticServer;+test.beforeAll(async () => {+ storybook = await serveStorybook(+ process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR,+ );+});+test.afterAll(async () => {+ await storybook?.close();+});++// A phone: the viewport meta applies and the page can pinch-zoom.+test.use({+ hasTouch: true,+ isMobile: true,+ viewport: {width: 390, height: 844},+ deviceScaleFactor: 3,+});++const sleep = async (ms: number) =>+ new Promise(resolve => setTimeout(resolve, ms));++type Point = {x: number; y: number};+type Box = {x: number; y: number; width: number; height: number};++interface Surface {+ name: string;+ story: string;+ /** The first row's reorder grip. */+ grip: string;+ /** Every reorderable row, in display order, keyed by this attribute. */+ rowAttribute: string;+}++const SURFACES: Surface[] = [+ {+ name: 'ListInput',+ story: 'lab-listinput--tag-options',+ grip: 'button[data-list-input-reorder]',+ rowAttribute: 'data-list-input-row',+ },+ {+ name: 'TransferList',+ story: 'lab-transferlist--default',+ grip: '[data-transfer-list-row]:not([data-transfer-list-row=""]) button[aria-pressed]',+ rowAttribute: 'data-transfer-list-row',+ },+];++async function openStory(page: Page, surface: Surface) {+ await page.goto(+ `${storybook.origin}/iframe.html?id=${surface.story}&viewMode=story`,+ );+ const grip = page.locator(surface.grip).first();+ await expect(grip).toBeVisible();+ expect(await page.evaluate(() => window.visualViewport?.scale)).toBe(1);+ const box = (await grip.boundingBox()) as Box;+ const center = {x: box.x + box.width / 2, y: box.y + box.height / 2};+ const cdp = await page.context().newCDPSession(page);+ return {center, cdp, order: await readOrder(page, surface)};+}++async function readOrder(page: Page, surface: Surface) {+ return page.evaluate(+ attribute =>+ Array.from(document.querySelectorAll(`[${attribute}]`))+ .map(row => row.getAttribute(attribute))+ .filter((key): key is string => key != null && key !== ''),+ surface.rowAttribute,+ );+}++const zoom = async (page: Page) =>+ page.evaluate(() => window.visualViewport?.scale ?? 1);++async function touch(+ cdp: CDPSession,+ type: 'touchStart' | 'touchMove' | 'touchEnd',+ points: Point[],+) {+ await cdp.send('Input.dispatchTouchEvent', {+ type,+ touchPoints: points.map((point, index) => ({...point, id: index + 1})),+ });+}++// Two fingers land one after the other and spread apart, as a person pinches.+// The spread is wide enough to clear the browser's minimum pinch span.+async function pinch(cdp: CDPSession, first: Point, second: Point) {+ const steps = 12;+ const spread = 2.5;+ const mid = {x: (first.x + second.x) / 2, y: (first.y + second.y) / 2};+ const at = (p: Point, k: number) => ({+ x: mid.x + (p.x - mid.x) * k,+ y: mid.y + (p.y - mid.y) * k,+ });+ await touch(cdp, 'touchStart', [first]);+ await sleep(30);+ await touch(cdp, 'touchStart', [first, second]);+ for (let i = 1; i <= steps; i++) {+ await sleep(16);+ const k = 1 + ((spread - 1) * i) / steps;+ await touch(cdp, 'touchMove', [at(first, k), at(second, k)]);+ }+ await sleep(16);+ await touch(cdp, 'touchEnd', []);+ await sleep(600);+}++for (const surface of SURFACES) {+ test.describe(surface.name, () => {+ test('a pinch whose first finger lands on a grip zooms the page', async ({+ page,+ }) => {+ const {center, cdp, order} = await openStory(page, surface);+ await pinch(cdp, center, {x: center.x + 40, y: center.y + 140});+ expect(await zoom(page)).toBeGreaterThan(1.2);+ expect(await readOrder(page, surface)).toEqual(order);+ });++ test('a pinch whose second finger lands on a grip zooms the page', async ({+ page,+ }) => {+ const {center, cdp, order} = await openStory(page, surface);+ await pinch(cdp, {x: center.x + 40, y: center.y + 140}, center);+ expect(await zoom(page)).toBeGreaterThan(1.2);+ expect(await readOrder(page, surface)).toEqual(order);+ });++ test('a one-finger drag on a grip still reorders', async ({page}) => {+ const {center, cdp, order} = await openStory(page, surface);+ const rows = await page.evaluate(+ attribute =>+ Array.from(document.querySelectorAll(`[${attribute}]`))+ .filter(row => row.getAttribute(attribute))+ .map(row => row.getBoundingClientRect().top),+ surface.rowAttribute,+ );+ // Past the second row's centre: the first row lands second or later.+ const distance = (rows[2] - rows[0]) * 0.9;+ await touch(cdp, 'touchStart', [center]);+ for (let i = 1; i <= 20; i++) {+ await sleep(16);+ await touch(cdp, 'touchMove', [+ {x: center.x, y: center.y + (distance * i) / 20},+ ]);+ }+ await sleep(100);+ await touch(cdp, 'touchEnd', []);+ await sleep(600);+ expect(await zoom(page)).toBe(1);+ const after = await readOrder(page, surface);+ expect(after).not.toEqual(order);+ expect(after.indexOf(order[0])).toBeGreaterThan(0);+ });+ });+}diff --git a/packages/lab/src/reorderStyles.ts b/packages/lab/src/reorderStyles.tsindex 69da464dd3..9ae8072742 100644--- a/packages/lab/src/reorderStyles.ts+++ b/packages/lab/src/reorderStyles.ts@@ -65,18 +65,23 @@ export const reorderStyles = stylex.create({insetBlockEnd: `calc(-1 * ${spacingVars['--spacing-0-5']})`,},},+ // A finger on a grip drags its row from the first move, so the grip refuses+ // panning. It still allows pinch-zoom: a pinch whose first finger lands on a+ // grip zooms the page (WCAG 1.4.4); the browser's pointercancel then ends+ // the drag, as it would any drag the browser takes. `touch-action` is read+ // when the touch starts, so the active state keeps the same value.handle: {cursor: {default: 'grab',':is(:disabled,[aria-disabled="true"])': 'default',},- touchAction: 'none',+ touchAction: 'pinch-zoom',},handleActive: {cursor: {default: 'grabbing',':is(:disabled,[aria-disabled="true"])': 'default',},- touchAction: 'none',+ touchAction: 'pinch-zoom',},});