Skip to content

Commit 9da6410

Browse files
EvandroLGclaude
andauthored
fix(a11y): add keyboard activation and button role to CardHorizontal (#1269)
* fix(a11y): CardHorizontal keyboard operable A clickable CardHorizontal was focusable but did nothing on Enter or Space. It now activates on both keys, the same as a click, and is exposed as a button when it has no inner action button. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(CardHorizontal): only handle keys and button role for actionable cards Display-only cards (no onClick, onButtonClick or infoUrl) keep the default Enter and Space behavior and are no longer announced as buttons. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(CardHorizontal): activate once while Enter or Space is held A held key repeats keydown, which called the card's onClick on every repeat. Only the first press activates the card now, and a held Space still doesn't scroll the page. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(CardHorizontal): use one flag for the button role and key handling A card with an inner info button had no button role but still handled Enter and Space on the wrapper. The inner button is now the only keyboard action on those cards. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent b050934 commit 9da6410

5 files changed

Lines changed: 261 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clickhouse/click-ui': patch
3+
---
4+
5+
Activate a clickable CardHorizontal with Enter and Space, and expose it as a button when it has no inner action button. Display-only cards keep the default key behavior.

‎eslint-suppressions.json‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
"count": 1
88
}
99
},
10-
"src/components/CardHorizontal/CardHorizontal.tsx": {
11-
"jsx-a11y/click-events-have-key-events": {
12-
"count": 1
13-
},
14-
"jsx-a11y/no-static-element-interactions": {
15-
"count": 1
16-
}
17-
},
1810
"src/components/CardPrimary/CardPrimary.tsx": {
1911
"jsx-a11y/no-noninteractive-tabindex": {
2012
"count": 1

‎src/components/CardHorizontal/CardHorizontal.stories.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Meta, StoryObj } from '@storybook/react-vite';
22
import { CSSProperties, ReactNode } from 'react';
3+
import { expect, fn, userEvent, within } from 'storybook/test';
34

45
import { ICON_NAMES } from '../Icon/IconCommon';
56

@@ -129,3 +130,21 @@ export const WithButton: Story = {
129130
infoUrl: 'https://clickhouse.com',
130131
},
131132
};
133+
134+
export const Keyboard: Story = {
135+
args: {
136+
...baseArgs,
137+
isSelectable: true,
138+
onClick: fn(),
139+
},
140+
play: async ({ canvasElement, args }) => {
141+
const card = within(canvasElement).getByRole('button', { name: /Card title/ });
142+
143+
await userEvent.tab();
144+
await expect(card).toHaveFocus();
145+
146+
await userEvent.keyboard('{Enter}');
147+
await userEvent.keyboard(' ');
148+
await expect(args.onClick).toHaveBeenCalledTimes(2);
149+
},
150+
};

‎src/components/CardHorizontal/CardHorizontal.test.tsx‎

Lines changed: 209 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { screen } from '@testing-library/react';
1+
import { fireEvent, screen } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import { CardHorizontal, CardHorizontalProps } from '@/components/CardHorizontal';
44
import { renderCUI } from '@/utils/test-utils';
@@ -290,4 +290,212 @@ describe('CardHorizontal Component', () => {
290290
expect(warnSpy).toHaveBeenCalledTimes(1);
291291
expect(warnSpy.mock.calls[0][0]).toContain('javascript:alert(1)');
292292
});
293+
294+
describe('keyboard', () => {
295+
it('should call onClick when Enter is pressed on the focused card', async () => {
296+
const onClick = vitest.fn();
297+
const { container } = renderCard({ title: 'Test Card', onClick });
298+
299+
(container.firstChild as HTMLElement).focus();
300+
await userEvent.keyboard('{Enter}');
301+
302+
expect(onClick).toHaveBeenCalledTimes(1);
303+
});
304+
305+
it('should call onClick when Space is pressed on the focused card', async () => {
306+
const onClick = vitest.fn();
307+
const { container } = renderCard({ title: 'Test Card', onClick });
308+
309+
(container.firstChild as HTMLElement).focus();
310+
await userEvent.keyboard(' ');
311+
312+
expect(onClick).toHaveBeenCalledTimes(1);
313+
});
314+
315+
it('should prevent the default Space action so the page does not scroll', () => {
316+
const { container } = renderCard({ title: 'Test Card', onClick: vitest.fn() });
317+
318+
const notPrevented = fireEvent.keyDown(container.firstChild as HTMLElement, {
319+
key: ' ',
320+
});
321+
322+
expect(notPrevented).toBe(false);
323+
});
324+
325+
it.each([
326+
['Enter', 'Enter'],
327+
['Space', ' '],
328+
])('should activate only once while %s is held down', (_name, key) => {
329+
const onClick = vitest.fn();
330+
const { container } = renderCard({ title: 'Test Card', onClick });
331+
332+
const wrapper = container.firstChild as HTMLElement;
333+
fireEvent.keyDown(wrapper, { key });
334+
fireEvent.keyDown(wrapper, { key, repeat: true });
335+
fireEvent.keyDown(wrapper, { key, repeat: true });
336+
337+
expect(onClick).toHaveBeenCalledTimes(1);
338+
});
339+
340+
it('should keep preventing the default Space action while Space is held down', () => {
341+
const { container } = renderCard({ title: 'Test Card', onClick: vitest.fn() });
342+
343+
const notPrevented = fireEvent.keyDown(container.firstChild as HTMLElement, {
344+
key: ' ',
345+
repeat: true,
346+
});
347+
348+
expect(notPrevented).toBe(false);
349+
});
350+
351+
it('should keep the default Space action on a display-only card', () => {
352+
const { container } = renderCard({ title: 'Summary', isSelectable: false });
353+
354+
const notPrevented = fireEvent.keyDown(container.firstChild as HTMLElement, {
355+
key: ' ',
356+
});
357+
358+
expect(notPrevented).toBe(true);
359+
});
360+
361+
it('should leave Enter to the inner button when the card has one', () => {
362+
const onButtonClick = vitest.fn();
363+
const { container } = renderCard({
364+
title: 'Test Card',
365+
infoText: 'Continue',
366+
onButtonClick,
367+
});
368+
369+
fireEvent.keyDown(container.firstChild as HTMLElement, { key: 'Enter' });
370+
371+
expect(onButtonClick).not.toHaveBeenCalled();
372+
});
373+
374+
it('should keep the default Space action when the card has an inner button', () => {
375+
const { container } = renderCard({
376+
title: 'Test Card',
377+
infoText: 'Continue',
378+
onButtonClick: vitest.fn(),
379+
});
380+
381+
const notPrevented = fireEvent.keyDown(container.firstChild as HTMLElement, {
382+
key: ' ',
383+
});
384+
385+
expect(notPrevented).toBe(true);
386+
});
387+
388+
it('should call onButtonClick when Enter is pressed on the focused card', async () => {
389+
const onButtonClick = vitest.fn();
390+
const { container } = renderCard({ title: 'Test Card', onButtonClick });
391+
392+
(container.firstChild as HTMLElement).focus();
393+
await userEvent.keyboard('{Enter}');
394+
395+
expect(onButtonClick).toHaveBeenCalledTimes(1);
396+
});
397+
398+
it('should open infoUrl when Enter is pressed on the focused card', async () => {
399+
const windowOpenSpy = vitest.spyOn(window, 'open').mockImplementation(() => null);
400+
const { container } = renderCard({
401+
title: 'Test Card',
402+
infoUrl: 'https://example.com',
403+
});
404+
405+
(container.firstChild as HTMLElement).focus();
406+
await userEvent.keyboard('{Enter}');
407+
408+
expect(windowOpenSpy).toHaveBeenCalledTimes(1);
409+
expect(windowOpenSpy).toHaveBeenCalledWith(
410+
'https://example.com',
411+
'_blank',
412+
'noopener'
413+
);
414+
});
415+
416+
it('should not activate a disabled card', () => {
417+
const onButtonClick = vitest.fn();
418+
const windowOpenSpy = vitest.spyOn(window, 'open').mockImplementation(() => null);
419+
const { container } = renderCard({
420+
title: 'Test Card',
421+
disabled: true,
422+
infoUrl: 'https://example.com',
423+
onButtonClick,
424+
});
425+
426+
const wrapper = container.firstChild as HTMLElement;
427+
fireEvent.keyDown(wrapper, { key: 'Enter' });
428+
fireEvent.keyDown(wrapper, { key: ' ' });
429+
430+
expect(onButtonClick).not.toHaveBeenCalled();
431+
expect(windowOpenSpy).not.toHaveBeenCalled();
432+
});
433+
434+
it('should activate only once when Enter is pressed on the inner button', async () => {
435+
const onButtonClick = vitest.fn();
436+
const windowOpenSpy = vitest.spyOn(window, 'open').mockImplementation(() => null);
437+
const { getByRole } = renderCard({
438+
title: 'Test Card',
439+
infoText: 'Click me',
440+
infoUrl: 'https://example.com',
441+
onButtonClick,
442+
});
443+
444+
getByRole('button').focus();
445+
await userEvent.keyboard('{Enter}');
446+
447+
expect(onButtonClick).toHaveBeenCalledTimes(1);
448+
expect(windowOpenSpy).toHaveBeenCalledTimes(1);
449+
});
450+
451+
it("should call the consumer's onKeyDown", async () => {
452+
const onKeyDown = vitest.fn();
453+
const { container } = renderCard({ title: 'Test Card', onKeyDown });
454+
455+
(container.firstChild as HTMLElement).focus();
456+
await userEvent.keyboard('{Enter}');
457+
458+
expect(onKeyDown).toHaveBeenCalledTimes(1);
459+
});
460+
461+
it("should not activate when the consumer's onKeyDown prevents the default", async () => {
462+
const onClick = vitest.fn();
463+
const { container } = renderCard({
464+
title: 'Test Card',
465+
onClick,
466+
onKeyDown: e => e.preventDefault(),
467+
});
468+
469+
(container.firstChild as HTMLElement).focus();
470+
await userEvent.keyboard('{Enter}');
471+
472+
expect(onClick).not.toHaveBeenCalled();
473+
});
474+
});
475+
476+
describe('role', () => {
477+
it('should expose an actionable card as a button', () => {
478+
renderCard({ title: 'Test Card', onClick: vitest.fn() });
479+
480+
expect(screen.getByRole('button')).toHaveTextContent('Test Card');
481+
});
482+
483+
it('should not expose the card as a button when it renders an inner button', () => {
484+
renderCard({
485+
title: 'Test Card',
486+
infoText: 'Click me',
487+
infoUrl: 'https://example.com',
488+
});
489+
490+
const buttons = screen.getAllByRole('button');
491+
expect(buttons).toHaveLength(1);
492+
expect(buttons[0]).toHaveTextContent('Click me');
493+
});
494+
495+
it('should not expose a display-only card as a button', () => {
496+
renderCard({ title: 'Summary' });
497+
498+
expect(screen.queryByRole('button')).not.toBeInTheDocument();
499+
});
500+
});
293501
});

‎src/components/CardHorizontal/CardHorizontal.tsx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export const CardHorizontal = ({
8080
badgeIcon,
8181
badgeIconDir,
8282
onButtonClick,
83+
onKeyDown,
8384
className,
8485
...props
8586
}: CardHorizontalProps) => {
@@ -100,11 +101,38 @@ export const CardHorizontal = ({
100101
e.stopPropagation();
101102
handleClick(e);
102103
};
104+
const isActionable =
105+
typeof props.onClick === 'function' ||
106+
typeof onButtonClick === 'function' ||
107+
Boolean(infoUrl);
108+
// With an inner info button, that button is the keyboard action, so the card leaves Enter and Space alone.
109+
const isButton = isActionable && !infoText;
110+
const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => {
111+
if (typeof onKeyDown === 'function') {
112+
onKeyDown(e);
113+
}
114+
// Keys from nested controls bubble up here and must not activate the card.
115+
const shouldSkipActivation =
116+
!isButton || e.defaultPrevented || disabled || e.target !== e.currentTarget;
117+
if (shouldSkipActivation) {
118+
return;
119+
}
120+
if (e.key === 'Enter' || e.key === ' ') {
121+
e.preventDefault();
122+
// A held key repeats keydown, so only the first press activates the card.
123+
if (!e.repeat) {
124+
// Runs whichever onClick is attached.
125+
e.currentTarget.click();
126+
}
127+
}
128+
};
103129
return (
104130
<div
105131
tabIndex={disabled ? -1 : 0}
106132
aria-disabled={disabled}
133+
role={isButton ? 'button' : undefined}
107134
onClick={handleClick}
135+
onKeyDown={handleKeyDown}
108136
{...props}
109137
className={cn(
110138
wrapperVariants({

0 commit comments

Comments
 (0)