Skip to content

Commit 2382438

Browse files
add example to better explain selectable a11y props
1 parent 1cad9b5 commit 2382438

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

packages/react-core/src/components/Card/examples/Card.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ import pfLogoSmall from './pf-logo-small.svg';
7171
```ts file='./CardSelectable.tsx'
7272
```
7373

74+
### Selectable accessibility highlight
75+
76+
This example demonstrates how the `hasSelectableInput` and `onSelectableInputChange` props enable improved accessibility for selectable cards.
77+
78+
The first card sets `hasSelectableInput` to true, which renders a checkbox input that is only visible to, and navigable by, screen readers. This input communicates to assistive technology users that a card is selectable , and the current selection state if so.
79+
80+
By default this input will have an aria-label that corresponds to the title given to the card if using the card title component. If you don't use the card title component in your selectable card you must pass a custom aria-label for this input using the `selectableInputAriaLabel` prop.
81+
82+
The first card also (by passing an onchange callback to `onSelectableInputChange`) enables the selection/deselection of the associated card by checking/unchecking the checkbox input.
83+
84+
The second card does not set `hasSelectableInput` to true, so the input is not rendered. It does not communicate to screen reader users that it is selectable or if it is currently selected.
85+
86+
To best understand this example it is encouraged that you navigate both of these cards using a screen reader.
87+
88+
```ts file='./CardSelectableA11yHighlight.tsx'
89+
```
90+
7491
### With heading element
7592

7693
```ts file='./CardWithHeadingElement.tsx'
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React from 'react';
2+
import { Card, CardTitle, CardBody } from '@patternfly/react-core';
3+
4+
export const CardSelectableA11yHighlight: React.FunctionComponent = () => {
5+
const [selected, setSelected] = React.useState<string>('');
6+
7+
const onKeyDown = (event: React.KeyboardEvent) => {
8+
if (event.target !== event.currentTarget) {
9+
return;
10+
}
11+
if ([' ', 'Enter'].includes(event.key)) {
12+
event.preventDefault();
13+
const newSelected = event.currentTarget.id === selected ? null : event.currentTarget.id;
14+
setSelected(newSelected);
15+
}
16+
};
17+
18+
const onClick = (event: React.MouseEvent) => {
19+
const newSelected = event.currentTarget.id === selected ? null : event.currentTarget.id;
20+
setSelected(newSelected);
21+
};
22+
23+
const onChange = (labelledById: string, _event: React.FormEvent<HTMLInputElement>) => {
24+
const newSelected = labelledById === selected ? null : labelledById;
25+
setSelected(newSelected);
26+
};
27+
28+
return (
29+
<React.Fragment>
30+
<Card
31+
id="selectable-first-card"
32+
onKeyDown={onKeyDown}
33+
onClick={onClick}
34+
hasSelectableInput
35+
onSelectableInputChange={onChange}
36+
isSelectableRaised
37+
isSelected={selected === 'selectable-first-card'}
38+
>
39+
<CardTitle>Selectable card with proper accessibility considerations</CardTitle>
40+
<CardBody>
41+
When using a screen reader a checkbox will become navigable that indicates this card is selectable and
42+
communicate if it is currently selected.
43+
</CardBody>
44+
</Card>
45+
<br />
46+
<Card
47+
id="selectable-second-card"
48+
onKeyDown={onKeyDown}
49+
onClick={onClick}
50+
isSelectableRaised
51+
isSelected={selected === 'selectable-second-card'}
52+
>
53+
<CardTitle>Selectable card without proper accessibility considerations</CardTitle>
54+
<CardBody>
55+
When using a screen reader there are no indications that this card is selectable or if it is currently
56+
selected.
57+
</CardBody>
58+
</Card>
59+
</React.Fragment>
60+
);
61+
};

0 commit comments

Comments
 (0)