You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`<sp-checkbox>` allow users to select multiple items from a list of independent
4
4
options, or to mark an individual option as selected.
@@ -12,30 +12,36 @@ instead of selecting.
12
12
[](https://bundlephobia.com/result?p=@spectrum-web-components/checkbox)
13
13
[](https://webcomponents.dev/edit/collection/fO75441E1Q5ZlI0e9pgq/jeIGAXHMUrTp6hGMquoD/src/index.ts)
14
14
15
-
```
15
+
```bash
16
16
yarn add @spectrum-web-components/checkbox
17
17
```
18
18
19
19
Import the side effectful registration of `<sp-checkbox>` via:
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.
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-checkboxreadonly>Read-only</sp-checkbox>
197
+
```
198
+
199
+
### Behaviors
200
+
201
+
#### Handling events
149
202
150
203
Event handlers for clicks and other user actions can be registered on an `<sp-checkbox>` as they would a standard `<input type="checkbox">` element.
151
204
152
205
```html
153
206
<sp-checkbox
154
207
id="checkbox-example"
155
-
onclick="spAlert(this, '<sp-radio> clicked!')"
208
+
onclick="spAlert(this, '<sp-checkbox> clicked!')"
156
209
>
157
-
Web component
210
+
Check this box to see an onclick alert.
158
211
</sp-checkbox>
159
212
```
160
213
161
-
## Accessibility
214
+
###Accessibility
162
215
163
216
Checkboxes are accessible by default, rendered in HTML using the `<input type="checkbox">` element. When the checkbox is set as `indeterminate` or
164
217
`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-grouplabel="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