Skip to content

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

TypeScript

fix(core): a disabled NavHeadingMenuItem link no longer navigates (#8667)

Parinith4c9a9d331502
diff --git a/.changeset/nav-heading-menu-item-disabled-link.md b/.changeset/nav-heading-menu-item-disabled-link.md
new file mode 100644
index 0000000000..5a99a918a3
--- /dev/null
+++ b/.changeset/nav-heading-menu-item-disabled-link.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A disabled `NavHeadingMenuItem` with `href` no longer navigates (`spec:AST-085` FR2). Before, it kept its link destination and only skipped `onClick`, so a click, a middle-click, or assistive-technology activation still followed the link. It now renders a plain `<a>` with no `href`, still reports `aria-disabled`, and does not use a custom `LinkProvider` component. The disabled link row now carries `tabIndex={-1}`: it stays focusable but is no longer a Tab stop (before, it was a page Tab stop only because it kept its `href`; `spec:AST-085` FR1). A link row that becomes disabled or enabled while focused keeps focus (`spec:AST-085` FR4). Apps need no code change; an enabled item is unchanged.
+
+@parinith-web
diff --git a/packages/core/src/NavMenu/NavHeadingMenu.test.tsx b/packages/core/src/NavMenu/NavHeadingMenu.test.tsx
index 4c9eac2238..1e3762ec73 100644
--- a/packages/core/src/NavMenu/NavHeadingMenu.test.tsx
+++ b/packages/core/src/NavMenu/NavHeadingMenu.test.tsx
@@ -1,9 +1,10 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
import {describe, it, expect, vi} from 'vitest';
-import type {KeyboardEvent} from 'react';
+import {Activity, type KeyboardEvent} from 'react';
import {render, screen, fireEvent} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
+import {LinkProvider} from '../Link';
import {NavHeadingMenu} from './NavHeadingMenu';
import {NavHeadingMenuItem} from './NavHeadingMenuItem';
import {NavHeadingCloseContext} from './NavMenuContext';
@@ -128,6 +129,203 @@ describe('NavHeadingMenuItem', () => {
);
});
+ it('exposes no destination on a disabled link item', () => {
+ render(
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" isDisabled />
+ </NavHeadingMenu>,
+ );
+ const item = screen.getByRole('menuitem');
+ // No href means no navigation path at all: not a pointer click, a
+ // middle-click, an assistive-technology activation, or Enter.
+ expect(item).not.toHaveAttribute('href');
+ // The row keeps its shape and still reports the disabled state.
+ expect(item.tagName).toBe('A');
+ expect(item).toHaveAttribute('aria-disabled', 'true');
+ });
+
+ it('does not reach a custom link component when a link item is disabled', () => {
+ function CustomLink({
+ children,
+ ref,
+ ...props
+ }: React.ComponentPropsWithRef<'a'>) {
+ return (
+ <a ref={ref} data-custom-link {...props}>
+ {children}
+ </a>
+ );
+ }
+ render(
+ <LinkProvider component={CustomLink}>
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" isDisabled />
+ </NavHeadingMenu>
+ </LinkProvider>,
+ );
+ const item = screen.getByRole('menuitem');
+ expect(item).not.toHaveAttribute('data-custom-link');
+ expect(item).not.toHaveAttribute('href');
+ });
+
+ it('keeps an enabled link item on the custom link component', () => {
+ function CustomLink({
+ children,
+ ref,
+ ...props
+ }: React.ComponentPropsWithRef<'a'>) {
+ return (
+ <a ref={ref} data-custom-link {...props}>
+ {children}
+ </a>
+ );
+ }
+ render(
+ <LinkProvider component={CustomLink}>
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" />
+ </NavHeadingMenu>
+ </LinkProvider>,
+ );
+ const item = screen.getByRole('menuitem');
+ expect(item).toHaveAttribute('data-custom-link');
+ expect(item).toHaveAttribute('href', '/docs');
+ });
+
+ it('keeps a disabled link item focusable but out of the Tab order', () => {
+ render(
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" isDisabled />
+ </NavHeadingMenu>,
+ );
+ const item = screen.getByRole('menuitem');
+ // No href and no tabindex would make the anchor unfocusable.
+ expect(item).toHaveAttribute('tabindex', '-1');
+ item.focus();
+ expect(item).toHaveFocus();
+ });
+
+ it('keeps focus on a link item that becomes disabled while focused', () => {
+ const {rerender} = render(
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" />
+ </NavHeadingMenu>,
+ );
+ screen.getByRole('menuitem').focus();
+ expect(screen.getByRole('menuitem')).toHaveFocus();
+
+ rerender(
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" isDisabled />
+ </NavHeadingMenu>,
+ );
+ const disabled = screen.getByRole('menuitem');
+ expect(disabled).not.toHaveAttribute('href');
+ expect(disabled).toHaveFocus();
+
+ rerender(
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" />
+ </NavHeadingMenu>,
+ );
+ expect(screen.getByRole('menuitem')).toHaveFocus();
+ });
+
+ it('does not steal focus when an unfocused link item toggles disabled', () => {
+ const {rerender} = render(
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" />
+ </NavHeadingMenu>,
+ );
+ rerender(
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" isDisabled />
+ </NavHeadingMenu>,
+ );
+ expect(screen.getByRole('menuitem')).not.toHaveFocus();
+ });
+
+ it('does not pull focus back when an Activity boundary reveals a row', () => {
+ const tree = (mode: 'visible' | 'hidden') => (
+ <>
+ <button type="button">Elsewhere</button>
+ <Activity mode={mode}>
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" />
+ </NavHeadingMenu>
+ </Activity>
+ </>
+ );
+ const {rerender} = render(tree('visible'));
+ screen.getByRole('menuitem').focus();
+ expect(screen.getByRole('menuitem')).toHaveFocus();
+
+ // Hide the row while it holds focus, then move focus elsewhere.
+ rerender(tree('hidden'));
+ const other = screen.getByRole('button', {name: 'Elsewhere'});
+ other.focus();
+ expect(other).toHaveFocus();
+
+ // Revealing the same node must leave focus where the person put it.
+ rerender(tree('visible'));
+ expect(other).toHaveFocus();
+ });
+
+ it('leaves focus on the page when an Activity boundary reveals a row', () => {
+ const tree = (mode: 'visible' | 'hidden') => (
+ <Activity mode={mode}>
+ <NavHeadingMenu>
+ <NavHeadingMenuItem label="Docs" href="/docs" />
+ </NavHeadingMenu>
+ </Activity>
+ );
+ const {rerender} = render(tree('visible'));
+ screen.getByRole('menuitem').focus();
+ expect(screen.getByRole('menuitem')).toHaveFocus();
+
+ // Hide while focused, then drop focus to the page.
+ rerender(tree('hidden'));
+ (document.activeElement as HTMLElement | null)?.blur();
+
+ // The same node re-attaches on reveal; it was not replaced, so it must not
+ // pull focus back from the page.
+ rerender(tree('visible'));
+ expect(document.activeElement).toBe(document.body);
+ });
+
+ it('does not take focus on reveal when isDisabled flipped while hidden', () => {
+ const tree = (mode: 'visible' | 'hidden', isDisabled: boolean) => (
+ <>
+ <button type="button">Elsewhere</button>
+ <Activity mode={mode}>
+ <NavHeadingMenu>
+ <NavHeadingMenuItem
+ label="Billing"
+ href="/billing"
+ isDisabled={isDisabled}
+ />
+ </NavHeadingMenu>
+ </Activity>
+ </>
+ );
+ const {rerender} = render(tree('visible', false));
+ screen.getByRole('menuitem').focus();
+ expect(screen.getByRole('menuitem')).toHaveFocus();
+
+ // Hide while focused, swap the row's element type while hidden, then move
+ // focus to another control.
+ rerender(tree('hidden', false));
+ rerender(tree('hidden', true));
+ const other = screen.getByRole('button', {name: 'Elsewhere'});
+ other.focus();
+ expect(other).toHaveFocus();
+
+ // The swapped-in node attaches only now and must not take focus.
+ rerender(tree('visible', true));
+ expect(screen.getByRole('menuitem')).not.toHaveAttribute('href');
+ expect(other).toHaveFocus();
+ });
+
it('renders description text', () => {
render(
<NavHeadingMenu>
diff --git a/packages/core/src/NavMenu/NavHeadingMenuItem.tsx b/packages/core/src/NavMenu/NavHeadingMenuItem.tsx
index d3e253bbe8..9ac1a0fb7a 100644
--- a/packages/core/src/NavMenu/NavHeadingMenuItem.tsx
+++ b/packages/core/src/NavMenu/NavHeadingMenuItem.tsx
@@ -12,7 +12,7 @@
* - /packages/core/src/NavMenu/index.ts
*/
-import React, {useCallback, type ReactNode} from 'react';
+import React, {useCallback, useRef, type ReactNode} from 'react';
import * as stylex from '@stylexjs/stylex';
import {renderIconSlot, type IconType} from '../Icon';
import {Text} from '../Text';
@@ -23,7 +23,7 @@ import {
typeScaleVars,
radiusVars,
} from '../theme/tokens.stylex';
-import {mergeProps} from '../utils';
+import {mergeProps, mergeRefs} from '../utils';
import type {BaseProps} from '../BaseProps';
import {useNavHeadingMenuContext} from './NavMenuContext';
import {useLinkComponent} from '../Link/useLinkComponent';
@@ -144,16 +144,64 @@ export function NavHeadingMenuItem({
}, [isDisabled, onClick, ctx]);
const LinkComponent = useLinkComponent();
- const Element = href ? LinkComponent : 'div';
+ // A disabled link item must not navigate (spec:AST-085 FR2). Cancelling the
+ // click would leave the destination live for middle-click, assistive
+ // technology activation, and "open in new tab", so the item drops its href
+ // instead. The router LinkComponent is skipped too: some routers reject a
+ // missing destination. A plain anchor keeps the row's element shape.
+ // (Mirrors the disabled paths of Link, Tab, and TopNavItem.)
+ const Element = href ? (isDisabled ? 'a' : LinkComponent) : 'div';
+
+ // The element type above changes when `isDisabled` flips, so React replaces
+ // the row. Carry focus over to the replacement when the outgoing node held it
+ // (spec:AST-085 FR4). The ref cleanup runs before the old node is removed, so
+ // `activeElement` is still readable there. Only a different node replacing a
+ // removed one takes focus, and only if focus has fallen to the page: React
+ // also detaches and re-attaches the same node (for example when an <Activity>
+ // boundary hides and reveals the row), and neither case may pull focus back
+ // after the person has moved elsewhere.
+ const focusedNodeRef = useRef<HTMLElement | null>(null);
+ const itemRef = useCallback(
+ (node: HTMLElement | null) => {
+ if (node === null) {
+ return;
+ }
+ const outgoing = focusedNodeRef.current;
+ focusedNodeRef.current = null;
+ // Focus only follows the swap when it has fallen to the page. If the
+ // person moved focus elsewhere in the meantime (for example while an
+ // <Activity> boundary kept the row hidden), leave it alone.
+ const focusFell =
+ document.activeElement === null ||
+ document.activeElement === document.body;
+ if (
+ outgoing !== null &&
+ outgoing !== node &&
+ !outgoing.isConnected &&
+ focusFell
+ ) {
+ node.focus();
+ }
+ const release = mergeRefs<HTMLElement>(ref)(node);
+ return () => {
+ focusedNodeRef.current = document.activeElement === node ? node : null;
+ release?.();
+ };
+ },
+ [ref],
+ );
return (
<Element
{...restProps}
- ref={ref}
+ ref={itemRef}
role="menuitem"
- tabIndex={isDisabled ? undefined : -1}
+ // A disabled link row has no href, so without tabIndex it could not take
+ // focus at all. -1 keeps it focusable but out of the Tab order, as an
+ // enabled row is (spec:AST-085 FR1).
+ tabIndex={isDisabled && !href ? undefined : -1}
aria-disabled={isDisabled || undefined}
- href={href}
+ href={isDisabled ? undefined : href}
onClick={handleClick}
data-testid={testId}
{...mergeProps(