Skip to content

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

TypeScript

fix(MobileNav): leave pinch-zoom to the browser while open (#7193)

Cindy Zhang5b5b76f9cf5d
diff --git a/.changeset/mobilenav-pinch-zoom.md b/.changeset/mobilenav-pinch-zoom.md
new file mode 100644
index 0000000000..2a1edbecba
--- /dev/null
+++ b/.changeset/mobilenav-pinch-zoom.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] MobileNav no longer blocks pinch-zoom while it is open. The open nav covers the whole viewport and declared `touch-action: none` (with `pan-y` on its content), so a pinch anywhere on screen did nothing. It now declares `pinch-zoom` (and `pan-y pinch-zoom` on the content): pans are still kept from reaching the page behind, and a pinch zooms the page.
+
+@cixzhang
diff --git a/docs/pr-assets/7193/pinch-before-after.png b/docs/pr-assets/7193/pinch-before-after.png
new file mode 100644
index 0000000000..87653ca1d2
Binary files /dev/null and b/docs/pr-assets/7193/pinch-before-after.png differ
diff --git a/packages/core/src/MobileNav/MobileNav.tsx b/packages/core/src/MobileNav/MobileNav.tsx
index ea1c2ab0ce..5f3c238911 100644
--- a/packages/core/src/MobileNav/MobileNav.tsx
+++ b/packages/core/src/MobileNav/MobileNav.tsx
@@ -98,8 +98,10 @@ const styles = stylex.create({
// depended on it being a scroll container.
overflow: 'clip',
overscrollBehavior: 'contain',
- // Prevent touch gestures (pull-to-refresh, background scroll) passing through
- touchAction: 'none',
+ // Stop pans passing through (pull-to-refresh, background scroll), but
+ // leave pinch to the browser: the open nav covers the whole viewport, and
+ // pinch-zoom is how phone readers resize text (WCAG 1.4.4).
+ touchAction: 'pinch-zoom',
outline: 'none',
// Native <dialog> uses display:none when closed.
// Open state applied via isOpen prop to avoid :where([open]) specificity issues.
@@ -230,9 +232,9 @@ const styles = stylex.create({
overflowY: 'auto',
overflowX: 'hidden',
overscrollBehavior: 'contain',
- // Re-enable vertical touch scrolling inside the drawer content
- // (dialog root has touch-action: none to block pull-to-refresh)
- touchAction: 'pan-y',
+ // Re-enable vertical touch scrolling inside the drawer content (the
+ // dialog root allows only pinch-zoom, to block pull-to-refresh)
+ touchAction: 'pan-y pinch-zoom',
paddingInline: spacingVars['--spacing-2'],
paddingBlock: spacingVars['--spacing-2'],
},
diff --git a/packages/core/src/MobileNav/MobileNavPinchZoom.a11y.chromium.spec.ts b/packages/core/src/MobileNav/MobileNavPinchZoom.a11y.chromium.spec.ts
new file mode 100644
index 0000000000..7f11176fe4
--- /dev/null
+++ b/packages/core/src/MobileNav/MobileNavPinchZoom.a11y.chromium.spec.ts
@@ -0,0 +1,146 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file MobileNavPinchZoom.a11y.chromium.spec.ts
+ * @input Built MobileNav stories and real Chromium touch input (CDP)
+ * @output Browser evidence that a two-finger pinch on the open nav, over its
+ * content or its backdrop, zooms the page (WCAG 1.4.4)
+ * @position Chromium proof for MobileNav's touch-action declarations
+ */
+
+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));
+
+async function openNav(page: Page) {
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=core-mobilenav--default&viewMode=story`,
+ );
+ await page.getByRole('button', {name: 'Open Navigation'}).tap();
+ const nav = page.getByRole('dialog');
+ await expect(nav).toBeVisible();
+ await page.waitForTimeout(600); // the slide-in
+ const panel = nav.getByRole('link', {name: 'Dashboard'});
+ const box = await panel.boundingBox();
+ if (box == null) {
+ throw new Error('no nav item box');
+ }
+ expect(await page.evaluate(() => window.visualViewport?.scale)).toBe(1);
+ return {nav, box};
+}
+
+type Point = {x: number; y: number};
+
+// 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 = 3;
+ const mid = {x: (first.x + second.x) / 2, y: (first.y + second.y) / 2};
+ const at = (p: Point, k: number, id: number) => ({
+ x: mid.x + (p.x - mid.x) * k,
+ y: mid.y + (p.y - mid.y) * k,
+ id,
+ });
+ await cdp.send('Input.dispatchTouchEvent', {
+ type: 'touchStart',
+ touchPoints: [at(first, 1, 1)],
+ });
+ await sleep(30);
+ await cdp.send('Input.dispatchTouchEvent', {
+ type: 'touchStart',
+ touchPoints: [at(first, 1, 1), at(second, 1, 2)],
+ });
+ for (let i = 1; i <= steps; i++) {
+ await sleep(16);
+ const k = 1 + ((spread - 1) * i) / steps;
+ await cdp.send('Input.dispatchTouchEvent', {
+ type: 'touchMove',
+ touchPoints: [at(first, k, 1), at(second, k, 2)],
+ });
+ }
+ await sleep(16);
+ await cdp.send('Input.dispatchTouchEvent', {
+ type: 'touchEnd',
+ touchPoints: [],
+ });
+ await sleep(600);
+}
+
+test('a pinch on the nav content zooms the page', async ({page}) => {
+ const {nav, box} = await openNav(page);
+ const cdp = await page.context().newCDPSession(page);
+ const x = box.x + box.width / 2;
+ // A sideways spread: the content scrolls vertically, so a vertical spread
+ // would spend its first stretch panning before the zoom takes over.
+ await pinch(cdp, {x: x - 30, y: box.y + 80}, {x: x + 30, y: box.y + 80});
+ expect(
+ await page.evaluate(() => window.visualViewport?.scale ?? 1),
+ ).toBeGreaterThan(1.2);
+ await expect(nav).toBeVisible();
+});
+
+test('a pinch on the backdrop beside the nav zooms the page', async ({
+ page,
+}) => {
+ const {nav} = await openNav(page);
+ // The panel is the dialog's own child; the rest of the dialog is backdrop.
+ // Whichever side the panel sits on, pinch in the strip beside it.
+ const panel = await nav.locator(':scope > div').first().boundingBox();
+ if (panel == null) {
+ throw new Error('no nav panel box');
+ }
+ const viewport = page.viewportSize()!;
+ const before = panel.x;
+ const after = viewport.width - (panel.x + panel.width);
+ const strip =
+ before >= after
+ ? {start: 0, end: panel.x}
+ : {start: panel.x + panel.width, end: viewport.width};
+ expect(strip.end - strip.start).toBeGreaterThan(40);
+ const x = (strip.start + strip.end) / 2;
+ const fingers = [
+ {x: x - 4, y: 380},
+ {x: x + 4, y: 440},
+ ];
+ // Both fingers land on the backdrop, outside the panel.
+ for (const finger of fingers) {
+ expect(finger.x < panel.x || finger.x > panel.x + panel.width).toBe(true);
+ expect(
+ await nav.evaluate(
+ (dialog, p) => document.elementFromPoint(p.x, p.y) === dialog,
+ finger,
+ ),
+ ).toBe(true);
+ }
+ const cdp = await page.context().newCDPSession(page);
+ await pinch(cdp, fingers[0], fingers[1]);
+ expect(
+ await page.evaluate(() => window.visualViewport?.scale ?? 1),
+ ).toBeGreaterThan(1.2);
+ await expect(nav).toBeVisible();
+});