An open source design system that's fully customizable and agent ready
TypeScript
fix(core): drop live href and onClick from disabled Links (#4009)
Baraa Hamodifd658d7e20a7
diff --git a/.changeset/link-disabled-inert.md b/.changeset/link-disabled-inert.mdnew file mode 100644index 0000000000..17566b39d8--- /dev/null+++ b/.changeset/link-disabled-inert.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Link: disabled links no longer carry a live href/onClick — programmatic focus or AT activation can no longer trigger navigation.+@bhamodidiff --git a/packages/core/src/Link/Link.test.tsx b/packages/core/src/Link/Link.test.tsxindex cd0de3bfe9..fafaf5c561 100644--- a/packages/core/src/Link/Link.test.tsx+++ b/packages/core/src/Link/Link.test.tsx@@ -10,7 +10,7 @@*/import {describe, it, expect, vi} from 'vitest';-import {render, screen} from '@testing-library/react';+import {fireEvent, render, screen} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {Link} from './Link';import {LinkProvider} from './LinkProvider';@@ -153,9 +153,81 @@ describe('Link', () => {Disabled Link</Link>,);- const link = screen.getByRole('link');+ // An href-less anchor has no implicit `link` role, so query by text.+ const link = screen.getByText('Disabled Link').closest('a');+ expect(link).not.toBeNull();expect(link).toHaveAttribute('aria-disabled', 'true');expect(link).toHaveAttribute('tabIndex', '-1');+ // Visual classes are preserved in the disabled state.+ expect(link?.className).toContain('astryx-link');+ });++ it('disabled link has no href attribute', () => {+ render(+ <Link href="/test" isDisabled>+ Disabled Link+ </Link>,+ );+ const link = screen.getByText('Disabled Link').closest('a');+ expect(link).not.toBeNull();+ expect(link).not.toHaveAttribute('href');+ });++ it('clicking a disabled link cancels default navigation', () => {+ render(+ <Link href="/test" isDisabled>+ Disabled Link+ </Link>,+ );+ const link = screen.getByText('Disabled Link').closest('a');+ expect(link).not.toBeNull();+ // fireEvent bypasses pointer-events, simulating programmatic/AT+ // activation. It returns false when preventDefault was called, i.e.+ // any default navigation behavior is cancelled.+ const notCancelled = fireEvent.click(link as HTMLAnchorElement);+ expect(notCancelled).toBe(false);+ });++ it('disabled link with onClick fires neither navigation nor the consumer onClick', () => {+ const handleClick = vi.fn();+ const {container} = render(+ <Link href="/test" isDisabled onClick={handleClick}>+ Disabled Link+ </Link>,+ );+ // With onClick present, a disabled Link renders as a disabled <button>+ // (useInteractiveRole excludes a disabled href). Either way the rendered+ // root must carry no live href and never invoke the consumer handler.+ const el = container.firstElementChild as HTMLElement;+ expect(el).not.toHaveAttribute('href');+ fireEvent.click(el);+ expect(handleClick).not.toHaveBeenCalled();+ });++ it('disabled link omits target and rel', () => {+ render(+ <Link href="https://example.com" isDisabled isExternalLink>+ Disabled External+ </Link>,+ );+ const link = screen.getByText('Disabled External').closest('a');+ expect(link).not.toBeNull();+ expect(link).not.toHaveAttribute('target');+ expect(link).not.toHaveAttribute('rel');+ });++ it('disabled link renders a plain anchor, not the custom LinkComponent', () => {+ render(+ <LinkProvider component={CustomLink}>+ <Link href="/custom" as={CustomLink} isDisabled>+ Disabled Custom+ </Link>+ </LinkProvider>,+ );+ const link = screen.getByText('Disabled Custom').closest('a');+ expect(link).not.toBeNull();+ expect(link).not.toHaveAttribute('data-custom-link');+ expect(link).not.toHaveAttribute('href');});it('renders external link with icon and target="_blank"', () => {diff --git a/packages/core/src/Link/Link.tsx b/packages/core/src/Link/Link.tsxindex de40aab54c..d11c7c7705 100644--- a/packages/core/src/Link/Link.tsx+++ b/packages/core/src/Link/Link.tsx@@ -174,6 +174,10 @@ export interface LinkProps extends BaseProps<hasUnderline?: boolean;/*** Whether the link is disabled.+ * A disabled link renders as a plain anchor without an href (and without+ * target/rel/onClick), so it cannot be focused or activated — no+ * navigation and no onClick, even via programmatic focus or assistive+ * technology activation.* @default false*/isDisabled?: boolean;@@ -261,6 +265,15 @@ export interface LinkProps extends BaseProps<children: ReactNode;}+/**+ * Click handler for disabled links. The disabled anchor renders without an+ * href, so there is no navigation to block in practice; preventDefault is a+ * defensive guard against synthetic/programmatic clicks.+ */+function preventDefaultClick(event: React.MouseEvent<HTMLAnchorElement>): void {+ event.preventDefault();+}+/*** A styled anchor link component.*@@ -368,6 +381,37 @@ export function Link({{sharedContent}</button>);+ } else if (isDisabled) {+ // A disabled link renders as a plain <a> with no href: an href-less+ // anchor is not focusable and exposes no link affordance, so programmatic+ // focus + Enter, AT activation commands, and middle-click cannot navigate+ // or fire the consumer onClick. The router LinkComponent is deliberately+ // skipped — a disabled link performs no navigation, and custom router+ // links may require a live href. target/rel are omitted with the href.+ linkElement = (+ <a+ ref={ref as React.Ref<HTMLAnchorElement>}+ onClick={preventDefaultClick}+ aria-label={label || undefined}+ aria-disabled={true}+ tabIndex={-1}+ {...mergeProps(+ themeProps('link', {color}),+ stylex.props(+ styles.base,+ linkColorStyles[color],+ hasUnderline && styles.hasUnderline,+ isStandalone && styles.standalone,+ styles.disabled,+ xstyle,+ ),+ className,+ style,+ )}+ {...props}>+ {sharedContent}+ </a>+ );} else {linkElement = (<LinkComponent