An open source design system that's fully customizable and agent ready
TypeScript
fix(core): a Token with an href now calls a caller's onClick (#9205)
Parinith26ce561a4b59
diff --git a/.changeset/token-link-onclick.md b/.changeset/token-link-onclick.mdnew file mode 100644index 0000000000..57c5fc6d91--- /dev/null+++ b/.changeset/token-link-onclick.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] A `Token` with an `href` now calls a caller's `onClick` (`architecture:public-component-api` INV5, `Token` FR3). Before, `onClick` was dropped whenever `href` made the token a link, so the handler never ran on a click. This covers the link token and the link token with a remove button, where a plain click on the token surface runs the handler once, a Cmd/Ctrl-click or middle click on the surface opens the link in a new tab without running it, and a click on the Remove button does not run it. Navigation is unchanged, and calling `preventDefault` in the handler still stops it.++@parinith-webdiff --git a/packages/core/src/Token/Token.test.tsx b/packages/core/src/Token/Token.test.tsxindex 81422e4dca..630ec93132 100644--- a/packages/core/src/Token/Token.test.tsx+++ b/packages/core/src/Token/Token.test.tsx@@ -456,6 +456,78 @@ describe('Token accessibility', () => {});});+describe('Token link onClick', () => {+ // jsdom does not implement navigation, so a native listener stops it. React+ // handlers still run for the click.+ function stopNavigation(link: HTMLElement) {+ link.addEventListener('click', e => e.preventDefault());+ }++ it('calls a caller onClick once when a link token is clicked', () => {+ const handleClick = vi.fn();+ render(<Token label="Tag" href="/test" onClick={handleClick} />);+ const link = screen.getByRole('link', {name: 'Tag'});+ stopNavigation(link);++ fireEvent.click(link);+ expect(handleClick).toHaveBeenCalledTimes(1);+ expect(link).toHaveAttribute('href', '/test');+ });++ it('calls a caller onClick once when the link of a removable token is clicked', () => {+ const handleClick = vi.fn();+ render(+ <Token+ label="Tag"+ href="/test"+ onClick={handleClick}+ onRemove={() => {}}+ />,+ );+ const link = screen.getByRole('link', {name: 'Tag'});+ stopNavigation(link);++ fireEvent.click(link);+ expect(handleClick).toHaveBeenCalledTimes(1);+ });++ it('calls a caller onClick once when the surface of a removable token is clicked', () => {+ const handleClick = vi.fn();+ render(+ <Token+ label="Tag"+ href="/test"+ onClick={handleClick}+ onRemove={() => {}}+ data-testid="token"+ />,+ );+ stopNavigation(screen.getByRole('link', {name: 'Tag'}));++ // The surface click is delegated to the link; it must not run twice.+ fireEvent.click(screen.getByTestId('token'));+ expect(handleClick).toHaveBeenCalledTimes(1);+ });++ it('does not call a caller onClick when the remove button is clicked', () => {+ const handleClick = vi.fn();+ const handleRemove = vi.fn();+ render(+ <Token+ label="Tag"+ href="/test"+ onClick={handleClick}+ onRemove={handleRemove}+ />,+ );+ stopNavigation(screen.getByRole('link', {name: 'Tag'}));++ fireEvent.click(screen.getByRole('button', {name: 'Remove Tag'}));+ expect(handleRemove).toHaveBeenCalledTimes(1);+ expect(handleClick).not.toHaveBeenCalled();+ });+});+describe('Token link with remove button', () => {it('does not nest the remove button inside the link', () => {render(<Token label="Tag" href="/test" onRemove={() => {}} />);diff --git a/packages/core/src/Token/Token.tsx b/packages/core/src/Token/Token.tsxindex d89c1e3188..98036ce398 100644--- a/packages/core/src/Token/Token.tsx+++ b/packages/core/src/Token/Token.tsx@@ -404,6 +404,7 @@ export function Token({<LinkComponentref={ref as React.Ref<HTMLAnchorElement>}href={href as string}+ onClick={onClick}{...pressable}{...mergeProps(themeProps('token', {@@ -444,6 +445,7 @@ export function Token({ref={ref}href={href as string}isDisabled={isDisabled}+ onLinkClick={onClick}LinkComponent={LinkComponent}icon={icon}endContent={endContent}diff --git a/packages/core/src/Token/TokenLink.tsx b/packages/core/src/Token/TokenLink.tsxindex 9a895ed568..4c536292fe 100644--- a/packages/core/src/Token/TokenLink.tsx+++ b/packages/core/src/Token/TokenLink.tsx@@ -25,7 +25,13 @@* - /packages/core/src/Token/Token.test.tsx (tests for delegated behavior)*/-import type {ReactNode, CSSProperties, Ref, HTMLAttributes} from 'react';+import type {+ ReactNode,+ CSSProperties,+ Ref,+ HTMLAttributes,+ MouseEventHandler,+} from 'react';import {useRef} from 'react';import {useClickableContainer} from '../hooks/useClickableContainer';import type {LinkComponentType} from '../Link/types';@@ -40,6 +46,13 @@ export interface TokenLinkProps extends HTMLAttributes<HTMLElement> {href: string;/** Whether the token is disabled. */isDisabled: boolean;+ /**+ * Caller `onClick`, attached to the inner link. A plain click on the+ * container surface is delegated to the link, so this runs once per+ * plain-click activation of the link or the surface. A Cmd/Ctrl-click or+ * middle click on the surface opens the link in a new tab and skips it.+ */+ onLinkClick?: MouseEventHandler<HTMLElement>;/** Resolved link component (native `<a>` or a router link). */LinkComponent: LinkComponentType;/** `stylex.props()` output applied to the invisible inner link. */@@ -68,6 +81,7 @@ export function TokenLink({ref,href,isDisabled,+ onLinkClick,LinkComponent,linkStyleProps,labelContent,@@ -117,6 +131,7 @@ export function TokenLink({<LinkComponentref={linkRef as Ref<HTMLAnchorElement>}href={href}+ onClick={onLinkClick}{...linkStyleProps}>{labelContent}</LinkComponent>