An open source design system that's fully customizable and agent ready
TypeScript
fix(BreadcrumbItem): a crumb in the AppShell mobile drawer closes it (#9683)
Cindy Zhangab905553a324
diff --git a/.changeset/breadcrumb-closes-mobile-drawer.md b/.changeset/breadcrumb-closes-mobile-drawer.mdnew file mode 100644index 0000000000..986a4522ca--- /dev/null+++ b/.changeset/breadcrumb-closes-mobile-drawer.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A `BreadcrumbItem` link or button inside the AppShell mobile drawer now closes the drawer after it runs `onClick`, like the `TopNavItem` and `SideNavItem` rows beside it. Before, a crumb navigated and left the drawer open over the new page. A crumb marked `isCurrent`, which goes nowhere, leaves the drawer open. (#9465)++@cixzhang @duytbpdiff --git a/apps/storybook/stories/AppShell.stories.tsx b/apps/storybook/stories/AppShell.stories.tsxindex 56f40cdf94..8e718f6e4b 100644--- a/apps/storybook/stories/AppShell.stories.tsx+++ b/apps/storybook/stories/AppShell.stories.tsx@@ -13,6 +13,7 @@ import {Icon} from '@astryxdesign/core/Icon';import {Text} from '@astryxdesign/core/Text';import {MobileNav} from '@astryxdesign/core/MobileNav';import {TopNav, TopNavHeading, TopNavItem} from '@astryxdesign/core/TopNav';+import {BreadcrumbItem, Breadcrumbs} from '@astryxdesign/core/Breadcrumbs';import {NavIcon} from '@astryxdesign/core/NavIcon';import {SideNav,@@ -361,6 +362,37 @@ export const LongHeadingOnMobile: Story = {),};+/**+ * Breadcrumbs in TopNav's start content on a phone. Below the breakpoint they+ * render in the mobile drawer; a crumb link closes the drawer, like the+ * TopNav and SideNav rows beside it, and the current crumb leaves it open.+ */+export const BreadcrumbsInMobileDrawer: Story = {+ globals: {viewport: {value: 'mobile1', isRotated: false}},+ render: () => (+ <AppShell+ contentPadding={6}+ topNav={+ <TopNav+ label="Main navigation"+ heading={<TopNavHeading heading="Acme App" headingHref="#/" />}+ startContent={+ <>+ <Breadcrumbs>+ <BreadcrumbItem href="#/projects">Projects</BreadcrumbItem>+ <BreadcrumbItem isCurrent>Detail</BreadcrumbItem>+ </Breadcrumbs>+ <TopNavItem label="Docs" href="#/docs" />+ </>+ }+ />+ }+ 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/docs/specs/components/Breadcrumbs/BreadcrumbItem.spec.md b/docs/specs/components/Breadcrumbs/BreadcrumbItem.spec.mdindex e2ef78c65f..61f5f7f1f5 100644--- a/docs/specs/components/Breadcrumbs/BreadcrumbItem.spec.md+++ b/docs/specs/components/Breadcrumbs/BreadcrumbItem.spec.md@@ -125,18 +125,19 @@ Consumer migration instructions belong in consumer docs and release notes.## Behavioral and layout contract-| ID | Invariant | Basis |-| ---- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |-| FR1 | The item root is one `<li>` carrying the public ref, the `breadcrumb-item` target with the trail's variant, `data-testid`, styling escape hatches, and other DOM props. | Released types, source, docs, and tests; `architecture:public-component-api` |-| FR2 | A non-current item with `href` and no menu uses the shared link renderer, with `onClick` on the link; an item with only `onClick` uses a native button styled as a link; an item with neither renders text content. | Released source, docs, and tests |-| FR3 | Explicit `isCurrent=true` renders the content as text (or the menu trigger) carrying `aria-current="page"` and ignores `href` and `onClick`; explicit `false` opts out; when no item in the list carries `aria-current="page"`, the list's last child, if it omits the prop, receives it on its content owner (Current-state impact records the fallback to the `<li>`); a trail whose last child sets `isCurrent={false}` has no current crumb. | Released source, docs, and tests |-| FR4 | Every item renders one `aria-hidden` separator container before its content, hidden on the first item. The built-in `/` mirrors exactly once in RTL; caller-provided separators render as given. | Source, focused tests, and `design:bidirectionality` DR4 |-| FR5 | A menu item exposes one menu button with `aria-haspopup="menu"`, its expanded state, and `aria-controls`, and one `role="menu"` surface labelled by the trigger, including non-string trigger content. Click, Enter, Space, and ArrowDown open it and focus its first item; Escape, Tab, and selection close it; arrows move between items without wrapping, and typeahead jumps to a matching item. | APG Menu Button pattern, `component:Popover`, source, tests, and browser evidence |-| FR6 | Closing the menu preserves a newly focused outside control. Focus returns to the trigger only when the closing surface would otherwise strand focus. | `component:Popover` and browser focus behavior |-| FR7 | The menu branch delegates item rendering, selection, row focus, and submenu content to the shared DropdownMenu item pipeline, from a data array or composed children. With `menu`, `href` and `onClick` are ignored and a development warning is logged. | Released source and focused tests |-| FR8 | Omitted `menuSize` resolves to `sm` under the `supporting` variant and `md` otherwise before entering the menu pipeline. | Released source and consumer docs |-| FR9 | The local targets are `breadcrumb-item` on the `<li>`, `breadcrumb-item-menu-trigger` on the menu button, and `breadcrumb-menu` on the menu surface; the first two reflect the trail's variant. Link, button, and menu trigger paint the shared focus outline. | Released docs, source, and theming tests |-| FR10 | In the open menu, a press (finger, pen or mouse) that lands on an enabled item and slides across the items acts on the one it lifts on, once, through that item's own activation, with the highlight following the pointer; a lift outside acts on nothing, and a mouse released outside closes the menu. The menu adopts `module:DropdownMenu/useMenuPress` for this (`family:action-groups` FR1–FR5; `module:DropdownMenu/useMenuPress` DEC-7). A press on caller content that is not an item, and a Ctrl-, Cmd- or Shift-click or a middle click on a link item, stays that content's or the browser's own, and keyboard and assistive-technology behavior is unchanged. | Current source and tests |+| ID | Invariant | Basis |+| ---- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |+| FR1 | The item root is one `<li>` carrying the public ref, the `breadcrumb-item` target with the trail's variant, `data-testid`, styling escape hatches, and other DOM props. | Released types, source, docs, and tests; `architecture:public-component-api` |+| FR2 | A non-current item with `href` and no menu uses the shared link renderer, with `onClick` on the link; an item with only `onClick` uses a native button styled as a link; an item with neither renders text content. | Released source, docs, and tests |+| FR3 | Explicit `isCurrent=true` renders the content as text (or the menu trigger) carrying `aria-current="page"` and ignores `href` and `onClick`; explicit `false` opts out; when no item in the list carries `aria-current="page"`, the list's last child, if it omits the prop, receives it on its content owner (Current-state impact records the fallback to the `<li>`); a trail whose last child sets `isCurrent={false}` has no current crumb. | Released source, docs, and tests |+| FR4 | Every item renders one `aria-hidden` separator container before its content, hidden on the first item. The built-in `/` mirrors exactly once in RTL; caller-provided separators render as given. | Source, focused tests, and `design:bidirectionality` DR4 |+| FR5 | A menu item exposes one menu button with `aria-haspopup="menu"`, its expanded state, and `aria-controls`, and one `role="menu"` surface labelled by the trigger, including non-string trigger content. Click, Enter, Space, and ArrowDown open it and focus its first item; Escape, Tab, and selection close it; arrows move between items without wrapping, and typeahead jumps to a matching item. | APG Menu Button pattern, `component:Popover`, source, tests, and browser evidence |+| FR6 | Closing the menu preserves a newly focused outside control. Focus returns to the trigger only when the closing surface would otherwise strand focus. | `component:Popover` and browser focus behavior |+| FR7 | The menu branch delegates item rendering, selection, row focus, and submenu content to the shared DropdownMenu item pipeline, from a data array or composed children. With `menu`, `href` and `onClick` are ignored and a development warning is logged. | Released source and focused tests |+| FR8 | Omitted `menuSize` resolves to `sm` under the `supporting` variant and `md` otherwise before entering the menu pipeline. | Released source and consumer docs |+| FR9 | The local targets are `breadcrumb-item` on the `<li>`, `breadcrumb-item-menu-trigger` on the menu button, and `breadcrumb-menu` on the menu surface; the first two reflect the trail's variant. Link, button, and menu trigger paint the shared focus outline. | Released docs, source, and theming tests |+| FR10 | In the open menu, a press (finger, pen or mouse) that lands on an enabled item and slides across the items acts on the one it lifts on, once, through that item's own activation, with the highlight following the pointer; a lift outside acts on nothing, and a mouse released outside closes the menu. The menu adopts `module:DropdownMenu/useMenuPress` for this (`family:action-groups` FR1–FR5; `module:DropdownMenu/useMenuPress` DEC-7). A press on caller content that is not an item, and a Ctrl-, Cmd- or Shift-click or a middle click on a link item, stays that content's or the browser's own, and keyboard and assistive-technology behavior is unchanged. | Current source and tests |+| FR11 | Inside the AppShell mobile drawer, activating a crumb's link or button calls `onClick` and then closes the drawer, as `component:TopNavItem` FR5 and `component:SideNavItem` FR10 do; a crumb marked `isCurrent`, which does not navigate, leaves it open. | Consistency with `component:TopNavItem` FR5 and `component:SideNavItem` FR10; defect fix |### Allowed variation@@ -228,14 +229,15 @@ Consumer migration instructions belong in consumer docs and release notes.## Verification map-| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |-| ---------------- | -------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------- | --------------------------------------------- |-| FR1–FR3, AR1–AR3 | `Breadcrumbs.test.tsx` branch, test-id, and current-state suites | Link, action, explicit current, omitted last crumb | A branch changes semantic element, loses current state, or moves the public root contract. | `audit:BreadcrumbItem/api-behavior` |-| FR4, AR6 | `Breadcrumbs.test.tsx` separator, mirror-style class, and custom-separator tests | Built-in slash, custom separator | A contextual separator fails to mirror exactly once or becomes exposed to AT. | `audit:BreadcrumbItem/rtl` |-| FR5–FR8, AR3–AR5 | `Breadcrumbs.test.tsx` menu suites | String and ReactNode labels, click/keyboard open, rows, selection, Escape, outside dismiss | A menu loses its name/state, focus enters the wrong owner, or dismissal steals valid outside focus. | `audit:BreadcrumbItem/accessibility-behavior` |-| FR9 | `Breadcrumbs.test.tsx` theme-target tests; `themingTargets.test.ts` | Item, menu trigger, menu surface; default/supporting | A target disappears, moves to non-painting plumbing, or loses variant reflection. | `audit:BreadcrumbItem/theming` |-| AR1–AR2 | `Breadcrumbs.a11y.test.tsx` and `Breadcrumbs.a11y.chromium.spec.ts` | Trail, explicit current, auto current, opt-out | A role, name, state, or focus expectation regresses. | `audit:BreadcrumbItem/accessibility-behavior` |-| FR10 | `Breadcrumbs.test.tsx` menu-crumb suite (touch and mouse slides) | Touch and mouse slides across items | Removing the adoption fails the slide tests. | — |+| Contract | Verification | Representative states | Mutation or failure expectation | Audit section |+| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------- | --------------------------------------------- |+| FR1–FR3, AR1–AR3 | `Breadcrumbs.test.tsx` branch, test-id, and current-state suites | Link, action, explicit current, omitted last crumb | A branch changes semantic element, loses current state, or moves the public root contract. | `audit:BreadcrumbItem/api-behavior` |+| FR4, AR6 | `Breadcrumbs.test.tsx` separator, mirror-style class, and custom-separator tests | Built-in slash, custom separator | A contextual separator fails to mirror exactly once or becomes exposed to AT. | `audit:BreadcrumbItem/rtl` |+| FR5–FR8, AR3–AR5 | `Breadcrumbs.test.tsx` menu suites | String and ReactNode labels, click/keyboard open, rows, selection, Escape, outside dismiss | A menu loses its name/state, focus enters the wrong owner, or dismissal steals valid outside focus. | `audit:BreadcrumbItem/accessibility-behavior` |+| FR9 | `Breadcrumbs.test.tsx` theme-target tests; `themingTargets.test.ts` | Item, menu trigger, menu surface; default/supporting | A target disappears, moves to non-painting plumbing, or loses variant reflection. | `audit:BreadcrumbItem/theming` |+| AR1–AR2 | `Breadcrumbs.a11y.test.tsx` and `Breadcrumbs.a11y.chromium.spec.ts` | Trail, explicit current, auto current, opt-out | A role, name, state, or focus expectation regresses. | `audit:BreadcrumbItem/accessibility-behavior` |+| FR10 | `Breadcrumbs.test.tsx` menu-crumb suite (touch and mouse slides) | Touch and mouse slides across items | Removing the adoption fails the slide tests. | — |+| FR11 | `Breadcrumbs.test.tsx` mobile-drawer suite: a crumb link closes the drawer, including a last crumb that is current only by position; a crumb marked `isCurrent` leaves it open. | Link and button crumbs in the TopNav and SideNav drawers; current crumb; a crumb outside the drawer | A crumb press in the drawer that leaves it open, or a current-crumb press or out-of-drawer press that closes it, fails. | — |Read from source, with no focused test yet:diff --git a/packages/core/src/AppShell/useCloseMobileDrawerOnActivate.ts b/packages/core/src/AppShell/useCloseMobileDrawerOnActivate.tsnew file mode 100644index 0000000000..e4b5fbeff6--- /dev/null+++ b/packages/core/src/AppShell/useCloseMobileDrawerOnActivate.ts@@ -0,0 +1,52 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++'use client';++/**+ * @file useCloseMobileDrawerOnActivate.ts+ * @input AppShellMobileContext, TopNav and SideNav render modes+ * @output useCloseMobileDrawerOnActivate, useIsInAppShellMobileDrawer+ * @position Internal AppShell helper for rows in the mobile drawer. Not+ * exported from the package.+ */++import {useAppShellMobile} from './AppShellMobileContext';+import {useSideNavRenderMode} from '../SideNav/SideNavRenderContext';+import {useTopNavRenderMode} from '../TopNav/TopNavRenderContext';++/**+ * Returns a function that wraps a row's click handler. While+ * `isInMobileDrawer` is true, the wrapped handler calls the row's handler and+ * then closes the AppShell mobile drawer. Otherwise the row's handler comes+ * back unchanged. Every drawer row closes the drawer through this one rule.+ */+export function useCloseMobileDrawerOnActivate(+ isInMobileDrawer: boolean,+): <Args extends unknown[]>(+ onClick: ((...args: Args) => void) | undefined,+) => ((...args: Args) => void) | undefined {+ const {closeMobileNav} = useAppShellMobile();+ return onClick => {+ if (!isInMobileDrawer) {+ return onClick;+ }+ return (...args) => {+ onClick?.(...args);+ closeMobileNav();+ };+ };+}++/**+ * Whether the caller renders inside the AppShell mobile drawer: in TopNav+ * content or SideNav content shown there.+ */+export function useIsInAppShellMobileDrawer(): boolean {+ const topNavRenderMode = useTopNavRenderMode();+ const sideNavRenderMode = useSideNavRenderMode();+ return (+ topNavRenderMode === 'drawer' ||+ sideNavRenderMode === 'drawer' ||+ sideNavRenderMode === 'drawer-content'+ );+}diff --git a/packages/core/src/Breadcrumbs/BreadcrumbItem.tsx b/packages/core/src/Breadcrumbs/BreadcrumbItem.tsxindex 0f280210fb..ebd6dac4e0 100644--- a/packages/core/src/Breadcrumbs/BreadcrumbItem.tsx+++ b/packages/core/src/Breadcrumbs/BreadcrumbItem.tsx@@ -7,7 +7,8 @@* @input Uses React use/useRef/useEffect, stylex, theme tokens, BreadcrumbContext,* rtlStyles for the contextual default slash, and (for the menu trigger)* usePopover + the shared DropdownMenu item API (renderDropdownItems,- * DropdownMenuContext, MENU_ITEM_SELECTOR, useListFocus)+ * DropdownMenuContext, MENU_ITEM_SELECTOR, useListFocus), and+ * the AppShell mobile drawer helpers* @output Exports BreadcrumbItem component and BreadcrumbItemProps* @position Individual breadcrumb item; used inside Breadcrumbs*@@ -72,6 +73,10 @@ import {import type {DropdownMenuOption} from '../DropdownMenu/DropdownMenu';import {useMergedRefs} from '../hooks/useMergedRefs';+import {+ useCloseMobileDrawerOnActivate,+ useIsInAppShellMobileDrawer,+} from '../AppShell/useCloseMobileDrawerOnActivate';import {useInteractionModalityTracking} from '../utils/interactionModality';import {isRenderable} from '../utils/isRenderable';// =============================================================================@@ -306,6 +311,11 @@ export function BreadcrumbItem({// aria-current on this instead of guessing the <li>'s last child.const contentRef = useRef<HTMLElement>(null);+ const closeDrawerAfter = useCloseMobileDrawerOnActivate(+ useIsInAppShellMobileDrawer(),+ );+ const handleActivate = closeDrawerAfter(onClick);+const isCurrent = isCurrentProp === true;const isAutoCandidate = isCurrentProp == null;const hasMenu = menu != null;@@ -456,7 +466,7 @@ export function BreadcrumbItem({<LinkComponentref={contentRef}href={href}- onClick={onClick}+ onClick={handleActivate}{...focusOutlineProps.focusVisible(itemStyles.link,isSupporting ? itemStyles.supportingLink : itemStyles.defaultLink,@@ -467,7 +477,7 @@ export function BreadcrumbItem({<buttonref={contentRef as React.RefObject<HTMLButtonElement | null>}type="button"- onClick={onClick}+ onClick={handleActivate}{...focusOutlineProps.focusVisible(itemStyles.link,itemStyles.buttonReset,diff --git a/packages/core/src/Breadcrumbs/Breadcrumbs.test.tsx b/packages/core/src/Breadcrumbs/Breadcrumbs.test.tsxindex 2e9cc5d434..7fd5ad9c97 100644--- a/packages/core/src/Breadcrumbs/Breadcrumbs.test.tsx+++ b/packages/core/src/Breadcrumbs/Breadcrumbs.test.tsx@@ -17,6 +17,15 @@ import {} from './index';import type {BreadcrumbMenuDividerProps, BreadcrumbMenuOption} from './index';import {LinkProvider} from '../Link/LinkProvider';+import {AppShellMobileContext} from '../AppShell/AppShellMobileContext';+import {+ SideNavRenderContext,+ type SideNavRenderMode,+} from '../SideNav/SideNavRenderContext';+import {+ TopNavRenderContext,+ type TopNavRenderMode,+} from '../TopNav/TopNavRenderContext';function CustomLink({children,@@ -371,6 +380,122 @@ describe('BreadcrumbItem', () => {});});+describe('BreadcrumbItem in the AppShell mobile drawer', () => {+ function renderInDrawer(+ crumbs: React.ReactNode,+ {+ topNavMode = 'drawer',+ sideNavMode = 'default',+ }: {+ topNavMode?: TopNavRenderMode;+ sideNavMode?: SideNavRenderMode;+ } = {},+ ) {+ const calls: string[] = [];+ const closeMobileNav = vi.fn(() => calls.push('close'));+ render(+ <AppShellMobileContext+ value={{+ isMobile: true,+ isMobileNavOpen: true,+ toggleMobileNav: () => {},+ openMobileNav: () => {},+ closeMobileNav,+ isMobileNavEnabled: true,+ hasAutoToggle: true,+ }}>+ <SideNavRenderContext value={sideNavMode}>+ <TopNavRenderContext value={topNavMode}>+ <Breadcrumbs>{crumbs}</Breadcrumbs>+ </TopNavRenderContext>+ </SideNavRenderContext>+ </AppShellMobileContext>,+ );+ return {calls, closeMobileNav};+ }++ it('a crumb link calls onClick and then closes the drawer', () => {+ const {calls} = renderInDrawer(+ <>+ <BreadcrumbItem+ href="#/projects"+ onClick={event => {+ event.preventDefault();+ calls.push('onClick');+ }}>+ Projects+ </BreadcrumbItem>+ <BreadcrumbItem isCurrent>Detail</BreadcrumbItem>+ </>,+ );+ fireEvent.click(screen.getByRole('link', {name: 'Projects'}));+ expect(calls).toEqual(['onClick', 'close']);+ });++ it('a crumb button calls onClick and then closes the drawer', () => {+ const {calls} = renderInDrawer(+ <>+ <BreadcrumbItem onClick={() => calls.push('onClick')}>+ Projects+ </BreadcrumbItem>+ <BreadcrumbItem isCurrent>Detail</BreadcrumbItem>+ </>,+ );+ fireEvent.click(screen.getByRole('button', {name: 'Projects'}));+ expect(calls).toEqual(['onClick', 'close']);+ });++ it('a crumb marked isCurrent leaves the drawer open', () => {+ const {closeMobileNav} = renderInDrawer(+ <>+ <BreadcrumbItem href="#/projects">Projects</BreadcrumbItem>+ <BreadcrumbItem isCurrent>Detail</BreadcrumbItem>+ </>,+ );+ fireEvent.click(screen.getByText('Detail'));+ expect(closeMobileNav).not.toHaveBeenCalled();+ });++ it('a last crumb that is current only by position is still a link and closes the drawer', () => {+ const {closeMobileNav} = renderInDrawer(+ <>+ <BreadcrumbItem href="#/projects">Projects</BreadcrumbItem>+ <BreadcrumbItem+ href="#/projects/detail"+ onClick={e => e.preventDefault()}>+ Detail+ </BreadcrumbItem>+ </>,+ );+ const last = screen.getByRole('link', {name: 'Detail'});+ expect(last).toHaveAttribute('aria-current', 'page');+ fireEvent.click(last);+ expect(closeMobileNav).toHaveBeenCalledTimes(1);+ });++ it('a crumb link in the side navigation drawer closes it too', () => {+ const {closeMobileNav} = renderInDrawer(+ <BreadcrumbItem href="#/projects" onClick={e => e.preventDefault()}>+ Projects+ </BreadcrumbItem>,+ {topNavMode: 'default', sideNavMode: 'drawer-content'},+ );+ fireEvent.click(screen.getByRole('link', {name: 'Projects'}));+ expect(closeMobileNav).toHaveBeenCalledTimes(1);+ });++ it('a crumb outside the drawer does not touch it', () => {+ const {closeMobileNav} = renderInDrawer(+ <BreadcrumbItem href="#/projects" onClick={e => e.preventDefault()}>+ Projects+ </BreadcrumbItem>,+ {topNavMode: 'mobile-bar'},+ );+ fireEvent.click(screen.getByRole('link', {name: 'Projects'}));+ expect(closeMobileNav).not.toHaveBeenCalled();+ });+});+describe('BreadcrumbItem menu', () => {// Mock the native Popover API — jsdom doesn't implement showPopover/// hidePopover or the :popover-open match. Mirrors DropdownMenu's test setup.diff --git a/packages/core/src/SideNav/SideNavItem.tsx b/packages/core/src/SideNav/SideNavItem.tsxindex fc75204ee9..caec17ef76 100644--- a/packages/core/src/SideNav/SideNavItem.tsx+++ b/packages/core/src/SideNav/SideNavItem.tsx@@ -72,7 +72,7 @@ import {SideNavCollapseContext,} from './SideNavCollapseContext';import {useSideNavRenderMode} from './SideNavRenderContext';-import {useAppShellMobile} from '../AppShell/AppShellMobileContext';+import {useCloseMobileDrawerOnActivate} from '../AppShell/useCloseMobileDrawerOnActivate';import {themeProps} from '../utils/themeProps';import {disabledInteractionProps} from '../utils/disabledInteraction';@@ -535,8 +535,8 @@ export function SideNavItem({const t = useComponentTranslator();const {isCollapsed} = useSideNavCollapse();const renderMode = useSideNavRenderMode();- const {closeMobileNav} = useAppShellMobile();const isInDrawer = renderMode === 'drawer' || renderMode === 'drawer-content';+ const closeDrawerAfter = useCloseMobileDrawerOnActivate(isInDrawer);const id = useId();const hasChildren = !!children;const depth = use(SideNavItemDepthContext);@@ -625,11 +625,7 @@ export function SideNavItem({toggleItemCollapse();return;}- onClick?.(e);- // Close the mobile nav when a nav item is activated inside the drawer- if (isInDrawer) {- closeMobileNav();- }+ closeDrawerAfter(onClick)?.(e);};const handleToggleClick = (e: React.MouseEvent) => {diff --git a/packages/core/src/TopNav/TopNavItem.tsx b/packages/core/src/TopNav/TopNavItem.tsxindex d69607f1cb..1953305327 100644--- a/packages/core/src/TopNav/TopNavItem.tsx+++ b/packages/core/src/TopNav/TopNavItem.tsx@@ -33,7 +33,7 @@ import type {LinkComponentType} from '../Link/types';import {useTopNavRenderMode} from './TopNavRenderContext';import {navItemStyles, type NavItemSize} from '../NavItem/navItemStyles.stylex';import {mergeProps} from '../utils';-import {useAppShellMobile} from '../AppShell/AppShellMobileContext';+import {useCloseMobileDrawerOnActivate} from '../AppShell/useCloseMobileDrawerOnActivate';import {themeProps} from '../utils/themeProps';import {focusOutlineProps} from '../utils/focusOutline.stylex';import {interactionOverlayStyles} from '../utils/interactionOverlay.stylex';@@ -199,7 +199,9 @@ export function TopNavItem({const pressable = usePressFeedback();const LinkComponent = useLinkComponent(as);const renderMode = useTopNavRenderMode();- const {closeMobileNav} = useAppShellMobile();+ const closeDrawerAfter = useCloseMobileDrawerOnActivate(+ renderMode === 'drawer',+ );// A disabled item renders as a plain <a> with no href: an href-less anchor// is not focusable and exposes no link affordance, so programmatic focus +@@ -214,14 +216,13 @@ export function TopNavItem({// Drawer mode — render as a SideNavItem-style vertical list element// =========================================================================if (renderMode === 'drawer') {+ const activate = closeDrawerAfter(onClick);const handleDrawerClick = (e: React.MouseEvent<HTMLAnchorElement>) => {if (isDisabled) {preventDefaultClick(e);return;}- // Forward the original onClick if present- onClick?.(e);- closeMobileNav();+ activate?.(e);};return (diff --git a/packages/core/src/TopNav/TopNavMegaMenuItem.tsx b/packages/core/src/TopNav/TopNavMegaMenuItem.tsxindex 91ff5a8c44..0930415dc6 100644--- a/packages/core/src/TopNav/TopNavMegaMenuItem.tsx+++ b/packages/core/src/TopNav/TopNavMegaMenuItem.tsx@@ -32,7 +32,7 @@ import type {LinkComponentType} from '../Link/types';import {useTopNavRenderMode} from './TopNavRenderContext';import {mergeProps} from '../utils';import type {BaseProps} from '../BaseProps';-import {useAppShellMobile} from '../AppShell/AppShellMobileContext';+import {useCloseMobileDrawerOnActivate} from '../AppShell/useCloseMobileDrawerOnActivate';import {themeProps} from '../utils/themeProps';import {focusOutlineProps} from '../utils/focusOutline.stylex';import {interactionOverlayStyles} from '../utils/interactionOverlay.stylex';@@ -200,7 +200,9 @@ export function TopNavMegaMenuItem({const pressable = usePressFeedback();const renderMode = useTopNavRenderMode();const LinkComponent = useLinkComponent(as);- const {closeMobileNav} = useAppShellMobile();+ const closeDrawerAfter = useCloseMobileDrawerOnActivate(+ renderMode === 'drawer',+ );// =========================================================================// Drawer mode — matches SideNavItem / TopNavMenu drawer appearance@@ -208,10 +210,9 @@ export function TopNavMegaMenuItem({if (renderMode === 'drawer') {const Element = href ? LinkComponent : 'button';const elementProps = Element === 'button' ? {type: 'button' as const} : {};- const handleDrawerClick = () => {+ const handleDrawerClick = closeDrawerAfter(() => {onClick?.();- closeMobileNav();- };+ });return (<Element{...restProps}diff --git a/packages/core/src/TopNav/TopNavMenu.tsx b/packages/core/src/TopNav/TopNavMenu.tsxindex 1ac19ebb85..7a1e36235e 100644--- a/packages/core/src/TopNav/TopNavMenu.tsx+++ b/packages/core/src/TopNav/TopNavMenu.tsx@@ -39,7 +39,7 @@ import type {BaseProps} from '../BaseProps';import {navItemStyles} from '../NavItem/navItemStyles.stylex';import {useTopNavSlot} from './TopNavContext';import {useTopNavRenderMode} from './TopNavRenderContext';-import {useAppShellMobile} from '../AppShell/AppShellMobileContext';+import {useCloseMobileDrawerOnActivate} from '../AppShell/useCloseMobileDrawerOnActivate';import {useLinkComponent} from '../Link/useLinkComponent';import {isSafeUrl} from '../utils/safeUrl';import {themeProps} from '../utils/themeProps';@@ -363,7 +363,9 @@ export function TopNavMenu({}: TopNavMenuProps) {const pressable = usePressFeedback();const renderMode = useTopNavRenderMode();- const {closeMobileNav} = useAppShellMobile();+ const closeDrawerAfter = useCloseMobileDrawerOnActivate(+ renderMode === 'drawer',+ );const LinkComponent = useLinkComponent();const [drawerExpanded, setDrawerExpanded] = useState(false);const menuId = useId();@@ -524,10 +526,9 @@ export function TopNavMenu({key={getMenuItemKey(item)}href={item.href}{...pressable}- onClick={(_e: React.MouseEvent) => {+ onClick={closeDrawerAfter((_e: React.MouseEvent) => {item.onClick?.();- closeMobileNav();- }}+ })}{...focusOutlineProps.focusVisible(navItemStyles.item,interactionOverlayStyles.backgroundColor,