Skip to content

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

TypeScript

fix(core): keep disabled ClickableCard out of the router link (#8659)

Divyanshu Dhruv726703b55d86
diff --git a/.changeset/clickablecard-disabled-router-link.md b/.changeset/clickablecard-disabled-router-link.md
new file mode 100644
index 0000000000..8fd253d708
--- /dev/null
+++ b/.changeset/clickablecard-disabled-router-link.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] ClickableCard: a disabled link card no longer passes an undefined `href` to the router link component, and a focused link card keeps focus when its disabled state changes.
+
+@divyanshudhruv
diff --git a/packages/core/src/ClickableCard/ClickableCard.test.tsx b/packages/core/src/ClickableCard/ClickableCard.test.tsx
index 031be76ade..3fb7163605 100644
--- a/packages/core/src/ClickableCard/ClickableCard.test.tsx
+++ b/packages/core/src/ClickableCard/ClickableCard.test.tsx
@@ -1,9 +1,11 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
+import {Activity, type ReactNode} from 'react';
import {describe, it, expect, vi} from 'vitest';
import {render, screen, fireEvent} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {ClickableCard} from './ClickableCard';
+import {LinkProvider} from '../Link';
import {
afterPaintsPress,
hasReleaseFade,
@@ -171,6 +173,100 @@ describe('ClickableCard', () => {
expect(handleClick).not.toHaveBeenCalled();
});
+ it('does not render a disabled link through the router link component', () => {
+ function RouterLink({
+ href,
+ children,
+ }: {
+ href?: string;
+ children?: ReactNode;
+ }) {
+ if (href == null) {
+ throw new Error('Router link requires an href');
+ }
+ return <a href={href}>{children}</a>;
+ }
+
+ render(
+ <LinkProvider component={RouterLink}>
+ <ClickableCard label="Disabled link" href="/settings" isDisabled>
+ <span>Content</span>
+ </ClickableCard>
+ </LinkProvider>,
+ );
+
+ const link = screen.getByRole('link', {name: 'Disabled link'});
+ expect(link).toHaveAttribute('aria-disabled', 'true');
+ expect(link).not.toHaveAttribute('href');
+ });
+
+ it('keeps focus when a focused link card toggles its disabled state', async () => {
+ const user = userEvent.setup();
+ const {rerender} = render(
+ <ClickableCard label="Focus card" href="/settings">
+ <span>Content</span>
+ </ClickableCard>,
+ );
+
+ await user.tab();
+ expect(screen.getByRole('link', {name: 'Focus card'})).toHaveFocus();
+
+ rerender(
+ <ClickableCard label="Focus card" href="/settings" isDisabled>
+ <span>Content</span>
+ </ClickableCard>,
+ );
+ expect(screen.getByRole('link', {name: 'Focus card'})).toHaveFocus();
+
+ rerender(
+ <ClickableCard label="Focus card" href="/settings">
+ <span>Content</span>
+ </ClickableCard>,
+ );
+ expect(screen.getByRole('link', {name: 'Focus card'})).toHaveFocus();
+ });
+
+ it('leaves focus where the person put it when a hidden card is revealed', async () => {
+ const user = userEvent.setup();
+ function Harness({isHidden}: {isHidden: boolean}) {
+ return (
+ <>
+ <Activity mode={isHidden ? 'hidden' : 'visible'}>
+ <ClickableCard label="Hidden card" href="/settings">
+ <span>Content</span>
+ </ClickableCard>
+ </Activity>
+ <button type="button">Elsewhere</button>
+ </>
+ );
+ }
+ const {rerender} = render(<Harness isHidden={false} />);
+ await user.tab();
+ expect(screen.getByRole('link', {name: 'Hidden card'})).toHaveFocus();
+ rerender(<Harness isHidden />);
+ screen.getByRole('button', {name: 'Elsewhere'}).focus();
+ rerender(<Harness isHidden={false} />);
+ expect(screen.getByRole('button', {name: 'Elsewhere'})).toHaveFocus();
+ });
+
+ it('takes focus back without scrolling the page', async () => {
+ const user = userEvent.setup();
+ const {rerender} = render(
+ <ClickableCard label="Scroll card" href="/settings">
+ <span>Content</span>
+ </ClickableCard>,
+ );
+ await user.tab();
+ const focus = vi.spyOn(HTMLElement.prototype, 'focus');
+ rerender(
+ <ClickableCard label="Scroll card" href="/settings" isDisabled>
+ <span>Content</span>
+ </ClickableCard>,
+ );
+ expect(focus).toHaveBeenCalledWith({preventScroll: true});
+ focus.mockRestore();
+ });
+
it('runs onClick once from the keyboard via the hidden button', async () => {
const user = userEvent.setup();
const handleClick = vi.fn();
diff --git a/packages/core/src/ClickableCard/ClickableCard.tsx b/packages/core/src/ClickableCard/ClickableCard.tsx
index c9fc344bef..34ae48b55b 100644
--- a/packages/core/src/ClickableCard/ClickableCard.tsx
+++ b/packages/core/src/ClickableCard/ClickableCard.tsx
@@ -29,7 +29,13 @@
* For toggle selection, use SelectableCard.
*/
-import {type ReactNode, type MouseEvent, useRef, type Ref} from 'react';
+import {
+ type ReactNode,
+ type MouseEvent,
+ useCallback,
+ useRef,
+ type Ref,
+} from 'react';
import * as stylex from '@stylexjs/stylex';
import type {StyleXStyles} from '@stylexjs/stylex';
import {
@@ -327,6 +333,41 @@ export function ClickableCard({
const pressable = usePressFeedback();
const containerRef = useRef<HTMLDivElement | null>(null);
const interactiveRef = useRef<HTMLElement | null>(null);
+ const restoreFocusRef = useRef(false);
+ // The isDisabled value of the link that just detached: a link card that
+ // flips it replaces its hidden link (router component and plain anchor),
+ // and only that replacement takes focus back.
+ const detachedDisabledRef = useRef<boolean | null>(null);
+
+ // A focused link remounts when isDisabled changes: the replacement takes
+ // focus back, without scrolling, if focus fell to the page. Any other
+ // detach and attach (a hidden Activity or Suspense boundary revealed
+ // again) leaves focus where the person put it.
+ const setInteractiveRef = useCallback(
+ (node: HTMLElement | null) => {
+ if (node == null) {
+ const previous = interactiveRef.current;
+ restoreFocusRef.current =
+ previous != null && previous === previous.ownerDocument.activeElement;
+ detachedDisabledRef.current = isDisabled;
+ interactiveRef.current = null;
+ return;
+ }
+ interactiveRef.current = node;
+ const isFlip =
+ detachedDisabledRef.current != null &&
+ detachedDisabledRef.current !== isDisabled;
+ const active = node.ownerDocument.activeElement;
+ const hasFallen = active == null || active === node.ownerDocument.body;
+ if (restoreFocusRef.current && isFlip && hasFallen) {
+ node.focus({preventScroll: true});
+ }
+ restoreFocusRef.current = false;
+ detachedDisabledRef.current = null;
+ },
+ [isDisabled],
+ );
+
const LinkComponent = useLinkComponent();
const {onClick, onMouseUp} = useClickableContainer({
@@ -347,6 +388,9 @@ export function ClickableCard({
const isLink = href != null;
+ // Disabled links have no href, so bypass the router and render an anchor.
+ const LinkRoot = isDisabled ? 'a' : LinkComponent;
+
// Only the `default` variant has a visible border. Card draws a transparent
// 1px border on every other variant purely to avoid layout jitter; we drop
// it here so the hover overlay covers the full box with no untinted ring.
@@ -385,8 +429,8 @@ export function ClickableCard({
onMouseUp={!isDisabled ? handleMouseUp : undefined}
{...props}>
{isLink ? (
- <LinkComponent
- ref={interactiveRef as React.Ref<HTMLAnchorElement>}
+ <LinkRoot
+ ref={setInteractiveRef}
href={isDisabled ? undefined : href}
target={isDisabled ? undefined : target}
role={isDisabled ? 'link' : undefined}
@@ -399,7 +443,7 @@ export function ClickableCard({
/>
) : (
<button
- ref={interactiveRef as React.Ref<HTMLButtonElement>}
+ ref={setInteractiveRef}
type="button"
aria-label={label}
aria-disabled={isDisabled || undefined}