Skip to content

Commit 44077a8

Browse files
authored
docs(checkbox): updated checkbox documentation for accessbility (#5381)
* docs(checkbox): updated checkbox documentation for accessbility * docs(checkbox): added feedback from reviews * docs(checkbox): added readonly
1 parent 784689d commit 44077a8

File tree

1 file changed

+111
-23
lines changed

1 file changed

+111
-23
lines changed

packages/checkbox/README.md

Lines changed: 111 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Description
1+
## Overview
22

33
`<sp-checkbox>` allow users to select multiple items from a list of independent
44
options, or to mark an individual option as selected.
@@ -12,30 +12,36 @@ instead of selecting.
1212
[![How big is this package in your project?](https://img.shields.io/bundlephobia/minzip/@spectrum-web-components/checkbox?style=for-the-badge)](https://bundlephobia.com/result?p=@spectrum-web-components/checkbox)
1313
[![Try it on webcomponents.dev](https://img.shields.io/badge/Try%20it%20on-webcomponents.dev-green?style=for-the-badge)](https://webcomponents.dev/edit/collection/fO75441E1Q5ZlI0e9pgq/jeIGAXHMUrTp6hGMquoD/src/index.ts)
1414

15-
```
15+
```bash
1616
yarn add @spectrum-web-components/checkbox
1717
```
1818

1919
Import the side effectful registration of `<sp-checkbox>` via:
2020

21-
```
21+
```ts
2222
import '@spectrum-web-components/checkbox/sp-checkbox.js';
2323
```
2424

2525
When looking to leverage the `Checkbox` base class as a type and/or for extension purposes, do so via:
2626

27-
```
27+
```ts
2828
import { Checkbox } from '@spectrum-web-components/checkbox';
2929
```
3030

31-
## Sizes
31+
### Anatomy
32+
33+
A checkbox consists of a box that can be checked or unchecked, and a label that describes its purpose. The checkbox can also be in an indeterminate state, which is visually distinct from both checked and unchecked states.
34+
35+
### Options
36+
37+
#### Sizes
3238

3339
<sp-tabs selected="m" auto label="Size Attribute Options">
3440
<sp-tab value="s">Small</sp-tab>
3541
<sp-tab-panel value="s">
3642

37-
```html demo
38-
<sp-field-group>
43+
```html
44+
<sp-field-group horizontal label="Small Checkboxes">
3945
<sp-checkbox size="s">Small</sp-checkbox>
4046
<sp-checkbox size="s" checked>Small Checked</sp-checkbox>
4147
<sp-checkbox size="s" indeterminate>Small Indeterminate</sp-checkbox>
@@ -46,8 +52,8 @@ import { Checkbox } from '@spectrum-web-components/checkbox';
4652
<sp-tab value="m">Medium</sp-tab>
4753
<sp-tab-panel value="m">
4854

49-
```html demo
50-
<sp-field-group>
55+
```html
56+
<sp-field-group horizontal label="Medium Checkboxes">
5157
<sp-checkbox size="m">Medium</sp-checkbox>
5258
<sp-checkbox size="m" checked>Medium Checked</sp-checkbox>
5359
<sp-checkbox size="m" indeterminate>Medium Indeterminate</sp-checkbox>
@@ -59,7 +65,7 @@ import { Checkbox } from '@spectrum-web-components/checkbox';
5965
<sp-tab-panel value="l">
6066

6167
```html demo
62-
<sp-field-group>
68+
<sp-field-group horizontal label="Large Checkboxes">
6369
<sp-checkbox size="l">Large</sp-checkbox>
6470
<sp-checkbox size="l" checked>Large Checked</sp-checkbox>
6571
<sp-checkbox size="l" indeterminate>Large Indeterminate</sp-checkbox>
@@ -71,7 +77,7 @@ import { Checkbox } from '@spectrum-web-components/checkbox';
7177
<sp-tab-panel value="xl">
7278

7379
```html demo
74-
<sp-field-group>
80+
<sp-field-group horizontal label="Extra Large Checkboxes">
7581
<sp-checkbox size="xl">Extra Large</sp-checkbox>
7682
<sp-checkbox size="xl" checked>Extra Large Checked</sp-checkbox>
7783
<sp-checkbox size="xl" indeterminate>Extra Large Indeterminate</sp-checkbox>
@@ -81,15 +87,17 @@ import { Checkbox } from '@spectrum-web-components/checkbox';
8187
</sp-tab-panel>
8288
</sp-tabs>
8389

84-
## Variants
90+
#### Variants
8591

86-
### Standard checkboxes
92+
<sp-tabs selected="standard" auto label="Checkbox Variants">
93+
<sp-tab value="standard">Standard Checkboxes</sp-tab>
94+
<sp-tab-panel value="standard">
8795

8896
Standard checkboxes are the default style for checkboxes. They are optimal for
8997
application panels where all visual elements are monochrome in order to direct
9098
focus to the content.
9199

92-
```html-live
100+
```html
93101
<div style="display: flex; justify-content: space-between;">
94102
<div style="display: flex; flex-direction: column;">
95103
<h4 class="spectrum-Heading--subtitle1">Default</h4>
@@ -114,15 +122,17 @@ focus to the content.
114122
</div>
115123
```
116124

117-
### Emphasized checkboxes
125+
</sp-tab-panel>
126+
<sp-tab value="emphasized">Emphasized Checkboxes</sp-tab>
127+
<sp-tab-panel value="emphasized">
118128

119129
Emphasized checkboxes are a secondary style for checkboxes. The blue color
120130
provides a visual prominence that is optimal for forms, settings, lists or grids
121131
of assets, etc. where the checkboxes need to be noticed.
122132

123-
```html-live
133+
```html
124134
<div style="display: flex; justify-content: space-between;">
125-
<div style="display: flex; flex-direction: column; justify-content: space-between;">
135+
<div style="display: flex; flex-direction: column;">
126136
<h4 class="spectrum-Heading--subtitle1">Default</h4>
127137
<sp-checkbox emphasized>Web component</sp-checkbox>
128138
<sp-checkbox emphasized checked>Web component</sp-checkbox>
@@ -133,32 +143,110 @@ of assets, etc. where the checkboxes need to be noticed.
133143
<h4 class="spectrum-Heading--subtitle1">Invalid</h4>
134144
<sp-checkbox emphasized invalid>Web component</sp-checkbox>
135145
<sp-checkbox emphasized checked invalid>Web component</sp-checkbox>
136-
<sp-checkbox emphasized indeterminate invalid>Web component</sp-checkbox>
146+
<sp-checkbox emphasized indeterminate invalid>
147+
Web component
148+
</sp-checkbox>
137149
</div>
138150

139151
<div style="display: flex; flex-direction: column;">
140152
<h4 class="spectrum-Heading--subtitle1">Disabled</h4>
141153
<sp-checkbox emphasized disabled>Web component</sp-checkbox>
142154
<sp-checkbox emphasized checked disabled>Web component</sp-checkbox>
143-
<sp-checkbox emphasized indeterminate disabled>Web component</sp-checkbox>
155+
<sp-checkbox emphasized indeterminate disabled>
156+
Web component
157+
</sp-checkbox>
144158
</div>
145159
</div>
146160
```
147161

148-
### Handling events
162+
</sp-tab-panel>
163+
</sp-tabs>
164+
165+
### States
166+
167+
#### Invalid
168+
169+
The `invalid` attribute indicates that the checkbox's value is invalid. When set, appropriate ARIA attributes will be automatically applied.
170+
171+
```html
172+
<sp-checkbox invalid>Invalid</sp-checkbox>
173+
```
174+
175+
#### Disabled
176+
177+
The `disabled` attribute prevents the checkbox from receiving focus or events. The checkbox will appear faded.
178+
179+
```html
180+
<sp-checkbox disabled>Disabled</sp-checkbox>
181+
```
182+
183+
#### Indeterminate
184+
185+
The `indeterminate` attribute sets the checkbox to an indeterminate state, visually distinct from both checked and unchecked states.
186+
187+
```html
188+
<sp-checkbox indeterminate>Indeterminate</sp-checkbox>
189+
```
190+
191+
#### Read-only
192+
193+
Checkboxes have a `readonly` attribute for when they’re in the disabled state but still need their labels to be shown. This allows for content to be copied, but not interacted with or changed.
194+
195+
```html
196+
<sp-checkbox readonly>Read-only</sp-checkbox>
197+
```
198+
199+
### Behaviors
200+
201+
#### Handling events
149202

150203
Event handlers for clicks and other user actions can be registered on an `<sp-checkbox>` as they would a standard `<input type="checkbox">` element.
151204

152205
```html
153206
<sp-checkbox
154207
id="checkbox-example"
155-
onclick="spAlert(this, '<sp-radio> clicked!')"
208+
onclick="spAlert(this, '<sp-checkbox> clicked!')"
156209
>
157-
Web component
210+
Check this box to see an onclick alert.
158211
</sp-checkbox>
159212
```
160213

161-
## Accessibility
214+
### Accessibility
162215

163216
Checkboxes are accessible by default, rendered in HTML using the `<input type="checkbox">` element. When the checkbox is set as `indeterminate` or
164217
`invalid`, the appropriate ARIA state attribute will automatically be applied.
218+
219+
#### Include a label
220+
221+
Every checkbox must have a label that clearly describes its purpose. The label can be provided as content within the `<sp-checkbox>` element.
222+
223+
```html
224+
<sp-checkbox>Send me text messages.</sp-checkbox>
225+
```
226+
227+
#### Label groups of related checkboxes
228+
229+
Sets of checkboxes should always have a clear label that describes what the list of options represents and guides users what to do. This is important for accessibility, since a screen reader will read the label before each option. (See [field group's label documentation](/field-group/#label) for more information.)
230+
231+
```html
232+
<sp-field-group label="Select your toppings">
233+
<sp-checkbox>Ketchup</sp-checkbox>
234+
<sp-checkbox>Mustard</sp-checkbox>
235+
<sp-checkbox>Pickles</sp-checkbox>
236+
</sp-field-group>
237+
```
238+
239+
#### Keyboard Navigation
240+
241+
Checkboxes can be toggled using the <kbd>Space</kbd> key when focused. They follow the standard tab order of the page.
242+
243+
#### Screen readers
244+
245+
Screen readers interpret checkboxes by announcing their role, label, current state, and role to the user. This allows users relying on assistive technology to understand and interact with the checkbox effectively.
246+
247+
When focused, a screen reader will announce:
248+
249+
- The label (text provided inside the or associated with it)
250+
- The state: "checked", "not checked", or "partially checked" (when indeterminate is set)
251+
- The role: "checkbox"
252+
- If the checkbox is marked as invalid, it may also announce "invalid entry" depending on the screen reader.

0 commit comments

Comments
 (0)