An open source design system that's fully customizable and agent ready
TypeScript
fix(TopNav): keep the menu toggle in view when a long heading meets a narrow screen (#7834)
Joey8f392ecc7d48
diff --git a/.changeset/top-nav-mobile-long-heading.md b/.changeset/top-nav-mobile-long-heading.mdnew file mode 100644index 0000000000..0c35dab542--- /dev/null+++ b/.changeset/top-nav-mobile-long-heading.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] `TopNav` on narrow screens: a long `TopNavHeading` now gives way (it truncates with an ellipsis, or wraps when the heading is a link) instead of pushing the end content and the menu toggle past the edge of the bar, and the mobile drawer's close button is back on screen.++@josephfarinadiff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex fbea8db502..b8ad549ae1 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -1415,6 +1415,17 @@ jobs:--project chromium \--output token-remove-target-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+ # them out.+ - name: Run TopNav mobile long heading contract+ run: |+ pnpm exec playwright test \+ packages/core/src/TopNav/__tests__/TopNavMobileLongHeading.a11y.chromium.spec.ts \+ --project chromium \+ --output top-nav-mobile-long-heading-output+# The RichTextEditor "Markdown parity" stories render one Markdown# document with core Markdown and RichTextEditor and measure where each# block lands; geometry, the read/edit switch and the scroll anchor onlydiff --git a/apps/storybook/stories/AppShell.stories.tsx b/apps/storybook/stories/AppShell.stories.tsxindex 5bfa79e8e8..ab51291648 100644--- a/apps/storybook/stories/AppShell.stories.tsx+++ b/apps/storybook/stories/AppShell.stories.tsx@@ -91,13 +91,19 @@ function MockContent({paragraphs = 3}: {paragraphs?: number}) {* Standard TopNav used across multiple stories.* Provides app identity (logo + heading) and top-level navigation.*/-function AppTopNav({endContent}: {endContent?: React.ReactNode}) {+function AppTopNav({+ endContent,+ heading = 'Acme App',+}: {+ endContent?: React.ReactNode;+ heading?: string;+}) {return (<TopNavlabel="Main navigation"heading={<TopNavHeading- heading="Acme App"+ heading={heading}logo={<NavIcon icon={<CubeIcon style={{width: 16, height: 16}} />} />}/>}@@ -310,6 +316,42 @@ export const TopNavWithSideNav: Story = {),};+/**+ * A long app name on a phone. The heading truncates with an ellipsis so the+ * end content and the menu toggle stay inside the bar.+ */+export const LongHeadingOnMobile: Story = {+ globals: {viewport: {value: 'mobile1', isRotated: false}},+ render: () => (+ <AppShell+ contentPadding={6}+ topNav={+ <AppTopNav+ heading="Northwind Internal Usage Analytics"+ endContent={+ <>+ <Button+ label="Notifications"+ variant="ghost"+ icon={<BellIcon style={{width: 16, height: 16}} />}+ isIconOnly+ />+ <Button+ label="Profile"+ variant="ghost"+ icon={<UserCircleIcon style={{width: 16, height: 16}} />}+ isIconOnly+ />+ </>+ }+ />+ }+ sideNav={<SideNavWithoutHeader />}>+ <MockContent />+ </AppShell>+ ),+};+/*** SideNav with its own heading (icon + heading) and no TopNav.* Use this layout for simpler apps where a full top bar isn't needed.diff --git a/packages/core/src/TopNav/TopNav.tsx b/packages/core/src/TopNav/TopNav.tsxindex eb83716aaf..12bb04a3de 100644--- a/packages/core/src/TopNav/TopNav.tsx+++ b/packages/core/src/TopNav/TopNav.tsx@@ -94,10 +94,18 @@ const styles = stylex.create({padding: spacingVars['--spacing-2'],boxSizing: 'border-box',},+ // The heading gives up space first so the end content and toggle stay in view+ mobileBarHeading: {+ display: 'flex',+ alignItems: 'center',+ flex: '0 1 auto',+ minWidth: 0,+ },mobileBarEnd: {display: 'flex',alignItems: 'center',gap: spacingVars['--spacing-1'],+ flexShrink: 0,marginInlineStart: 'auto',},// Drawer mode — vertical list of nav items@@ -215,7 +223,9 @@ export function TopNav({style,)}{...props}>- {heading && <div {...stylex.props(styles.heading)}>{heading}</div>}+ {heading && (+ <div {...stylex.props(styles.mobileBarHeading)}>{heading}</div>+ )}<div {...stylex.props(styles.mobileBarEnd)}>{endContent}{hasMobileDrawerContent && hasAutoToggle && <MobileNavToggle />}diff --git a/packages/core/src/TopNav/TopNavHeading.tsx b/packages/core/src/TopNav/TopNavHeading.tsxindex 43fec8ff1d..2636d4f803 100644--- a/packages/core/src/TopNav/TopNavHeading.tsx+++ b/packages/core/src/TopNav/TopNavHeading.tsx@@ -52,6 +52,7 @@ const styles = stylex.create({alignItems: 'center',gap: spacingVars['--spacing-2'],minHeight: spacingVars['--spacing-8'],+ minWidth: 0,paddingInlineStart: {default: spacingVars['--spacing-2'],':has(.astryx-nav-icon)': 0,diff --git a/packages/core/src/TopNav/__tests__/TopNavMobileLongHeading.a11y.chromium.spec.ts b/packages/core/src/TopNav/__tests__/TopNavMobileLongHeading.a11y.chromium.spec.tsnew file mode 100644index 0000000000..4c5873f7c2--- /dev/null+++ b/packages/core/src/TopNav/__tests__/TopNavMobileLongHeading.a11y.chromium.spec.ts@@ -0,0 +1,74 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file TopNavMobileLongHeading.a11y.chromium.spec.ts+ * @input The `Core/AppShell` Long Heading On Mobile story in a built Storybook+ * at a 320px phone viewport.+ * @output Real-engine proof that a long TopNav heading gives way on a narrow+ * screen: the menu toggle stays inside the compact bar, and the drawer's+ * close button stays inside the drawer.+ * @position Run with `pnpm test:a11y-contract`. jsdom has no layout, so where+ * the toggle lands is only observable here.+ */++import {expect, test, type Locator} 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();+});++const STORY = 'core-appshell--long-heading-on-mobile';++async function boxOf(locator: Locator) {+ const box = await locator.boundingBox();+ if (box == null) {+ throw new Error('the element has no layout box');+ }+ return box;+}++test('a long heading keeps the menu toggle and the drawer close button on screen', async ({+ page,+}) => {+ await page.setViewportSize({width: 320, height: 568});+ await page.goto(`${storybook.origin}/iframe.html?id=${STORY}&viewMode=story`);++ const bar = page.getByRole('navigation', {name: 'Main navigation'});+ const toggle = page.getByRole('button', {name: 'Open navigation'});+ await expect(toggle).toBeVisible();++ const barBox = await boxOf(bar);+ const toggleBox = await boxOf(toggle);+ expect(toggleBox.x).toBeGreaterThanOrEqual(barBox.x);+ expect(toggleBox.x + toggleBox.width).toBeLessThanOrEqual(+ barBox.x + barBox.width,+ );++ await toggle.click();++ const drawer = page.getByRole('dialog', {name: 'Navigation'});+ const close = drawer.getByRole('button', {name: 'Close navigation'});+ await expect(close).toBeVisible();++ // The drawer slides in from the inline end; measure once it has settled.+ await expect(close).toBeInViewport({ratio: 1});+ const drawerBox = await boxOf(drawer);+ const closeBox = await boxOf(close);+ expect(closeBox.x).toBeGreaterThanOrEqual(drawerBox.x);+ expect(closeBox.x + closeBox.width).toBeLessThanOrEqual(+ drawerBox.x + drawerBox.width,+ );+});