Skip to content

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

TypeScript

fix(core): a disabled TopNavItem no longer runs a caller's handlers (#10039)

Parinithdedecd0345c5
diff --git a/.changeset/topnavitem-disabled-caller-handlers.md b/.changeset/topnavitem-disabled-caller-handlers.md
new file mode 100644
index 0000000000..4c7de58d43
--- /dev/null
+++ b/.changeset/topnavitem-disabled-caller-handlers.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A disabled `TopNavItem` no longer runs a caller's `onPointerDown`, `onMouseDown`, `onKeyDown` or any other `on*` prop except `onFocus` and `onBlur`. Before, a key pressed while script had focused a disabled item, or an event dispatched to it, still reached them, in both the default and the drawer form; only `onClick` was withheld. Now a disabled `TopNavItem` runs no caller handler other than `onFocus` and `onBlur`, as a disabled `Slider`, `TextInput` and `TextArea` do (`spec:AST-085` FR2). An enabled `TopNavItem` is unchanged. No API changes.
+
+@parinith-web
diff --git a/packages/core/src/TopNav/TopNav.test.tsx b/packages/core/src/TopNav/TopNav.test.tsx
index a3a5aed53f..7ad35e89c2 100644
--- a/packages/core/src/TopNav/TopNav.test.tsx
+++ b/packages/core/src/TopNav/TopNav.test.tsx
@@ -655,6 +655,60 @@ describe('TopNavItem', () => {
expect(handleClick).not.toHaveBeenCalled();
});
+ it.each([
+ ['default', null],
+ ['drawer', 'drawer' as const],
+ ])(
+ 'does not run a disabled item’s caller handlers (%s mode)',
+ (_name, mode) => {
+ const onPointerDown = vi.fn();
+ const onMouseDown = vi.fn();
+ const onKeyDown = vi.fn();
+ const item = (
+ <TopNavItem
+ label="Home"
+ href="/home"
+ isDisabled
+ onPointerDown={onPointerDown}
+ onMouseDown={onMouseDown}
+ onKeyDown={onKeyDown}
+ />
+ );
+ render(
+ mode ? (
+ <TopNavRenderContext value={mode}>{item}</TopNavRenderContext>
+ ) : (
+ item
+ ),
+ );
+ const anchor = screen.getByText('Home').closest('a') as HTMLElement;
+ fireEvent.pointerDown(anchor);
+ fireEvent.mouseDown(anchor);
+ fireEvent.keyDown(anchor, {key: 'x'});
+ expect(onPointerDown).not.toHaveBeenCalled();
+ expect(onMouseDown).not.toHaveBeenCalled();
+ expect(onKeyDown).not.toHaveBeenCalled();
+ },
+ );
+
+ it('still runs an enabled item’s caller handlers', () => {
+ const onPointerDown = vi.fn();
+ const onKeyDown = vi.fn();
+ render(
+ <TopNavItem
+ label="Home"
+ href="/home"
+ onPointerDown={onPointerDown}
+ onKeyDown={onKeyDown}
+ />,
+ );
+ const anchor = screen.getByText('Home').closest('a') as HTMLElement;
+ fireEvent.pointerDown(anchor);
+ fireEvent.keyDown(anchor, {key: 'x'});
+ expect(onPointerDown).toHaveBeenCalledTimes(1);
+ expect(onKeyDown).toHaveBeenCalledTimes(1);
+ });
+
it('keeps href and click behavior for enabled items (default mode)', async () => {
const user = userEvent.setup();
const handleClick = vi.fn();
diff --git a/packages/core/src/TopNav/TopNavItem.tsx b/packages/core/src/TopNav/TopNavItem.tsx
index 1953305327..d2241a01c0 100644
--- a/packages/core/src/TopNav/TopNavItem.tsx
+++ b/packages/core/src/TopNav/TopNavItem.tsx
@@ -38,6 +38,7 @@ import {themeProps} from '../utils/themeProps';
import {focusOutlineProps} from '../utils/focusOutline.stylex';
import {interactionOverlayStyles} from '../utils/interactionOverlay.stylex';
import {usePressFeedback} from '../hooks/usePressFeedback';
+import {withholdCallerHandlers} from '../utils/disabledInteraction';
/**
* NavItem styles with hover/selected states
@@ -212,6 +213,10 @@ export function TopNavItem({
// disabled paths of Link and SideNavItem.)
const Root = isDisabled ? 'a' : LinkComponent;
+ // A disabled item runs no caller handler except `onFocus` and `onBlur`, as a
+ // natively disabled control runs none.
+ const callerProps = withholdCallerHandlers(props, isDisabled);
+
// =========================================================================
// Drawer mode — render as a SideNavItem-style vertical list element
// =========================================================================
@@ -251,7 +256,7 @@ export function TopNavItem({
className,
style,
)}
- {...props}
+ {...callerProps}
onClick={handleDrawerClick}>
{icon}
{!isIconOnly && (children ?? label)}
@@ -289,7 +294,7 @@ export function TopNavItem({
className,
style,
)}
- {...props}>
+ {...callerProps}>
{icon}
{!isIconOnly && (children ?? label)}
</Root>