Skip to content

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

TypeScript

fix(MobileNav): the drawer is never wider than 85% of the viewport (#8063)

Christopher Chedeaud0c4bf26f2ab
diff --git a/.changeset/mobile-nav-width-cap.md b/.changeset/mobile-nav-width-cap.md
new file mode 100644
index 0000000000..5cf0b5d556
--- /dev/null
+++ b/.changeset/mobile-nav-width-cap.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `MobileNav` is never wider than 85% of the viewport, as its `width` documentation says. A 320 px drawer on a 320 px phone used to cover the whole screen and leave no backdrop to tap; it is now 272 px wide and leaves a 48 px strip of the page beside it, and a tap on that strip closes it. On wider screens the drawer keeps its `width`.
+
+@vjeux
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index af9a2f19ac..29929cd25a 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1773,6 +1773,17 @@ jobs:
--project chromium \
--output token-remove-target-output
+ # On a phone narrower than the drawer, the drawer must still leave a
+ # strip of the page to tap to close it (it is capped at 85vw). Viewport
+ # units, the backdrop's hit testing and a real tap only exist in a real
+ # engine.
+ - name: Run MobileNav width contract
+ run: |
+ pnpm exec playwright test \
+ packages/core/src/MobileNav/__tests__/MobileNavWidth.a11y.chromium.spec.ts \
+ --project chromium \
+ --output mobile-nav-width-output
+
# On a phone-width screen AppShell shows TopNav's compact bar. A long
# heading must give way so the menu toggle stays inside the bar and the
# drawer's close button stays inside the drawer; only a real engine lays
diff --git a/packages/core/src/MobileNav/MobileNav.test.tsx b/packages/core/src/MobileNav/MobileNav.test.tsx
index 8942d20385..9658795cae 100644
--- a/packages/core/src/MobileNav/MobileNav.test.tsx
+++ b/packages/core/src/MobileNav/MobileNav.test.tsx
@@ -12,6 +12,7 @@
import {describe, it, expect, vi, beforeAll} from 'vitest';
import {render, screen, fireEvent} from '@testing-library/react';
import {MobileNav} from './MobileNav';
+import {declaredValue} from '../__tests__/stylexDeclarations';
// jsdom doesn't implement showModal/close on <dialog>, so we mock them
beforeAll(() => {
@@ -133,10 +134,7 @@ describe('MobileNav', () => {
it('forwards data-testid', () => {
render(
- <MobileNav
- isOpen={true}
- onOpenChange={() => {}}
- data-testid="custom-nav">
+ <MobileNav isOpen={true} onOpenChange={() => {}} data-testid="custom-nav">
<span>Content</span>
</MobileNav>,
);
@@ -202,10 +200,7 @@ describe('MobileNav', () => {
it('uses native dialog element', () => {
render(
- <MobileNav
- isOpen={true}
- onOpenChange={() => {}}
- data-testid="mobile-nav">
+ <MobileNav isOpen={true} onOpenChange={() => {}} data-testid="mobile-nav">
<span>Content</span>
</MobileNav>,
);
@@ -251,14 +246,36 @@ describe('MobileNav', () => {
expect(dialog).not.toHaveAttribute('open');
rerender(
+ <MobileNav isOpen={true} onOpenChange={() => {}} data-testid="mobile-nav">
+ <span>Content</span>
+ </MobileNav>,
+ );
+
+ expect(dialog).toHaveAttribute('open');
+ });
+
+ it('caps the drawer at 85 % of the viewport, as its docs promise', () => {
+ // On a 320 px phone a 320 px drawer used to cover the whole screen and
+ // leave no backdrop to tap; the cap keeps a 48 px strip of the page.
+ render(
<MobileNav
- isOpen={true}
+ isOpen
onOpenChange={() => {}}
+ width={300}
data-testid="mobile-nav">
<span>Content</span>
</MobileNav>,
);
- expect(dialog).toHaveAttribute('open');
+ const drawer = screen
+ .getByTestId('mobile-nav')
+ .querySelector<HTMLElement>('[tabindex="-1"]');
+ if (drawer == null) {
+ throw new Error('no drawer panel');
+ }
+ expect(declaredValue(drawer, 'max-width')).toBe('85vw');
+ // The requested width is the width, not a cap on a full-viewport one.
+ expect(drawer.style.getPropertyValue('--x-width')).toBe('300px');
+ expect(declaredValue(drawer, 'width')).toBe('var(--x-width)');
});
});
diff --git a/packages/core/src/MobileNav/MobileNav.tsx b/packages/core/src/MobileNav/MobileNav.tsx
index f5736fb099..0f342f725c 100644
--- a/packages/core/src/MobileNav/MobileNav.tsx
+++ b/packages/core/src/MobileNav/MobileNav.tsx
@@ -10,7 +10,8 @@
*
* Full-height slide-out drawer overlay for mobile navigation.
* The mobile counterpart to SideNav — accepts the same children
- * (SideNavSection, SideNavItem, or any ReactNode).
+ * (SideNavSection, SideNavItem, or any ReactNode). The drawer is never wider
+ * than 85% of the viewport, so a strip of the page stays beside it to tap.
*
* Uses the native `<dialog>` element with `showModal()` for top-layer rendering.
* This eliminates z-index stacking issues — the drawer renders above everything
@@ -250,9 +251,12 @@ const styles = stylex.create({
});
const dynamicStyles = stylex.create({
+ // The requested width, never more than 85 % of the viewport: on a narrow
+ // phone a strip of the page stays visible beside the drawer, as a target
+ // for the tap that closes it.
width: (w: number) => ({
- width: '100vw',
- maxWidth: `${w}px`,
+ width: `${w}px`,
+ maxWidth: '85vw',
}),
});
diff --git a/packages/core/src/MobileNav/__tests__/MobileNavWidth.a11y.chromium.spec.ts b/packages/core/src/MobileNav/__tests__/MobileNavWidth.a11y.chromium.spec.ts
new file mode 100644
index 0000000000..b3c18548ac
--- /dev/null
+++ b/packages/core/src/MobileNav/__tests__/MobileNavWidth.a11y.chromium.spec.ts
@@ -0,0 +1,102 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file MobileNavWidth.a11y.chromium.spec.ts
+ * @input Built MobileNav stories and real Chromium touch input in phone
+ * contexts
+ * @output Browser evidence that the drawer is never wider than 85% of the
+ * viewport: on a 320 px phone it leaves a 48 px strip of the page, and a
+ * tap on the strip closes it; on a 402 px phone it keeps its 320 px width
+ * @position Chromium proof for MobileNav's width cap (`dynamicStyles.width`
+ * in MobileNav.tsx); the jsdom layer is MobileNav.test.tsx
+ */
+
+import {expect, test, 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();
+});
+
+/** Opens the default story's drawer with a tap and waits for the slide-in. */
+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();
+ const drawer = nav.locator('[tabindex="-1"]').first();
+ await drawer.evaluate(async element => {
+ await Promise.all(element.getAnimations().map(async a => a.finished));
+ });
+ const box = await drawer.boundingBox();
+ const viewport = page.viewportSize();
+ if (box == null || viewport == null) {
+ throw new Error('the drawer did not render');
+ }
+ // The backdrop strip is on whichever side the drawer left free.
+ const strip =
+ box.x > 0.5
+ ? {from: 0, to: box.x}
+ : {from: box.x + box.width, to: viewport.width};
+ return {nav, box, strip, viewport};
+}
+
+test.describe('on a 320 px phone', () => {
+ test.use({
+ hasTouch: true,
+ isMobile: true,
+ viewport: {width: 320, height: 568},
+ deviceScaleFactor: 1,
+ });
+
+ test('the drawer leaves a strip of the page, and a tap on it closes the drawer', async ({
+ page,
+ }) => {
+ const {nav, box, strip, viewport} = await openNav(page);
+
+ // 85% of 320 px: the default 320 px drawer is capped at 272 px.
+ expect(box.width).toBeCloseTo(272, 0);
+ expect(strip.to - strip.from).toBeCloseTo(48, 0);
+
+ // The strip is backdrop (the dialog itself), not drawer.
+ const at = {x: (strip.from + strip.to) / 2, y: viewport.height / 2};
+ expect(
+ await page.evaluate(
+ ({x, y}) => document.elementFromPoint(x, y)?.tagName.toLowerCase(),
+ at,
+ ),
+ ).toBe('dialog');
+
+ await page.touchscreen.tap(at.x, at.y);
+ await expect(nav).toBeHidden();
+ });
+});
+
+test.describe('on a 402 px phone', () => {
+ test.use({
+ hasTouch: true,
+ isMobile: true,
+ viewport: {width: 402, height: 714},
+ deviceScaleFactor: 1,
+ });
+
+ test('the drawer keeps its 320 px width', async ({page}) => {
+ const {box, strip} = await openNav(page);
+
+ // 85% of 402 px is 341.7 px: the cap does not bind.
+ expect(box.width).toBeCloseTo(320, 0);
+ expect(strip.to - strip.from).toBeCloseTo(82, 0);
+ });
+});