An open source design system that's fully customizable and agent ready
TypeScript
fix(core): a disabled Tab no longer runs a caller's handlers (#10035)
Parinithaabfc163d84d
diff --git a/.changeset/tab-disabled-caller-handlers.md b/.changeset/tab-disabled-caller-handlers.mdnew file mode 100644index 0000000000..eb7d686c60--- /dev/null+++ b/.changeset/tab-disabled-caller-handlers.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A disabled `Tab` no longer runs a caller's `onPointerDown`, `onMouseDown`, `onKeyDown` or any other `on*` prop except `onFocus` and `onBlur`. Before, a press or key on a disabled tab still reached them, for both the button and the link form; only `onClick` was withheld. Now a disabled `Tab` runs no caller handler other than `onFocus` and `onBlur`, as a disabled `Slider`, `TextInput` and `TextArea` do (`spec:AST-085` FR2). An enabled `Tab` is unchanged. No API changes.++@parinith-webdiff --git a/packages/core/src/TabList/Tab.tsx b/packages/core/src/TabList/Tab.tsxindex 6d7937b772..2aa689cc3b 100644--- a/packages/core/src/TabList/Tab.tsx+++ b/packages/core/src/TabList/Tab.tsx@@ -39,6 +39,7 @@ import {useLinkComponent} from '../Link/useLinkComponent';import type {LinkComponentType} from '../Link/types';import {mergeProps} from '../utils';import {useDevWarning} from '../hooks/useDevWarning';+import {withholdCallerHandlers} from '../utils/disabledInteraction';import {EDGE_COMP_ATTR} from '../Layout/edgeCompensation.stylex';import {themeProps} from '../utils/themeProps';import {focusOutlineProps} from '../utils/focusOutline.stylex';@@ -362,7 +363,10 @@ export function Tab({) : null;const sharedProps = {- ...restProps,+ // A disabled tab keeps its tab stop, so the browser still sends it events.+ // It runs no caller handler other than `onFocus` and `onBlur`, as a natively+ // disabled control runs none. `onClick` is guarded in `handleSelect`.+ ...withholdCallerHandlers(restProps, isDisabled),...pressable,...(isLabelHidden ? {'aria-label': label} : {}),[EDGE_COMP_ATTR]: '',diff --git a/packages/core/src/TabList/TabList.test.tsx b/packages/core/src/TabList/TabList.test.tsxindex d529d596e6..698726bde4 100644--- a/packages/core/src/TabList/TabList.test.tsx+++ b/packages/core/src/TabList/TabList.test.tsx@@ -1708,6 +1708,91 @@ describe('pressed state', () => {expect(onChange).not.toHaveBeenCalled();});+ it('does not run a disabled tab’s caller handlers other than focus and blur', () => {+ const onKeyDown = vi.fn();+ const onPointerDown = vi.fn();+ const onMouseDown = vi.fn();+ const onFocus = vi.fn();+ const onBlur = vi.fn();+ render(+ <TabList value="home" onChange={() => {}}>+ <Tab value="home" label="Home" />+ <Tab+ value="settings"+ label="Settings"+ aria-disabled="true"+ onKeyDown={onKeyDown}+ onPointerDown={onPointerDown}+ onMouseDown={onMouseDown}+ onFocus={onFocus}+ onBlur={onBlur}+ />+ </TabList>,+ );+ const tab = screen.getByRole('button', {name: 'Settings'});+ fireEvent.pointerDown(tab);+ fireEvent.mouseDown(tab);+ fireEvent.keyDown(tab, {key: 'x'});+ expect(onPointerDown).not.toHaveBeenCalled();+ expect(onMouseDown).not.toHaveBeenCalled();+ expect(onKeyDown).not.toHaveBeenCalled();+ fireEvent.focus(tab);+ fireEvent.blur(tab);+ expect(onFocus).toHaveBeenCalledTimes(1);+ expect(onBlur).toHaveBeenCalledTimes(1);+ });++ it('does not run a disabled link tab’s caller handlers', () => {+ const onKeyDown = vi.fn();+ const onPointerDown = vi.fn();+ const onMouseDown = vi.fn();+ const {container} = render(+ <TabList value="home" onChange={() => {}}>+ <Tab value="home" label="Home" />+ <Tab+ value="settings"+ label="Settings"+ href="/settings"+ aria-disabled="true"+ onKeyDown={onKeyDown}+ onPointerDown={onPointerDown}+ onMouseDown={onMouseDown}+ />+ </TabList>,+ );+ const tab = container.querySelector('a[aria-disabled="true"]');+ if (tab == null) {+ throw new Error('the disabled link tab did not render an anchor');+ }+ fireEvent.pointerDown(tab);+ fireEvent.mouseDown(tab);+ fireEvent.keyDown(tab, {key: 'x'});+ expect(onPointerDown).not.toHaveBeenCalled();+ expect(onMouseDown).not.toHaveBeenCalled();+ expect(onKeyDown).not.toHaveBeenCalled();+ });++ it('still runs an enabled tab’s caller handlers', () => {+ const onKeyDown = vi.fn();+ const onPointerDown = vi.fn();+ render(+ <TabList value="home" onChange={() => {}}>+ <Tab value="home" label="Home" />+ <Tab+ value="settings"+ label="Settings"+ onKeyDown={onKeyDown}+ onPointerDown={onPointerDown}+ />+ </TabList>,+ );+ const tab = screen.getByRole('button', {name: 'Settings'});+ fireEvent.pointerDown(tab);+ fireEvent.keyDown(tab, {key: 'x'});+ expect(onPointerDown).toHaveBeenCalledTimes(1);+ expect(onKeyDown).toHaveBeenCalledTimes(1);+ });+it('removes navigation from a disabled link tab', () => {const onChange = vi.fn();const {container} = render(