Choice
- How
- A
<label class=“sdc-choice”>wrapping a native checkbox or radio and a<span>for the words. Several sit in a.sdc-choice-set, which wraps them onto as many rows as they need.Choice.astrowrites one. - When
- For a short, visible set of options a visitor picks from. Checkbox for any number, radio for exactly one. Not for a long list: past about a dozen options a Select is easier to scan.
- Where
- Inside a Fieldset, which gives the group its name. A set of chips with only a heading above it has no accessible name.
Why the input is one pixel wide
Section titled “Why the input is one pixel wide”The native input stays in the DOM. It is not display: none and not
visibility: hidden, because both remove it from the accessibility tree and from
the tab order. It is one pixel, transparent and absolutely positioned, so it
keeps every behaviour the browser gives it for free: space to toggle, arrow keys
to move between radios in a group, and the right announcement.
The label is the visible target and carries the size, --sdc-control-h (44px).
The focus ring is drawn on the label with :has(input:focus-visible), not on the
input, because a ring around a one pixel box is not a visible focus indicator.
An automated target-size scan will flag the input and it is wrong to. The input measures about 1 by 52; the label around it measures at least 54 by 44. WCAG 2.2 target size is about the thing a person activates, and that is the label. This is worth knowing before someone “fixes” it.
Checked is not colour alone
Section titled “Checked is not colour alone”A checked chip fills with --sdc-accent and takes --sdc-accent-text, and it
also grows a tick through ::before. The colour is the fast signal and the mark
is the one that survives a monochrome display, a colour vision difference, and a
screenshot printed in grey.
In code
Section titled “In code”src/components/ui/Choice.astro.
---import Choice from '../../components/ui/Choice.astro';import Fieldset from '../../components/ui/Fieldset.astro';---<Fieldset legend="What should people be able to do?"> <div class="sdc-choice-set"> <Choice name="can" label="Call me" /> <Choice name="can" label="Email me" /> <Choice name="can" label="Book an appointment" /> </div></Fieldset>| Prop | Type | Default | What it does |
|---|---|---|---|
label |
string | required | The visible label and the accessible name |
name |
string | required | The form field name. Radios in one group share it |
value |
string | the label | What is submitted |
radio |
boolean | false |
A radio instead of a checkbox |
checked |
boolean | false |
Starts checked |
disabled |
boolean | false |
Greys it out and stops it submitting |
Do
- Put a set inside a Fieldset with a legend.
- Use radio when exactly one answer is allowed.
- Keep labels short enough to read at a glance.
Don’t
- Hide the input with
display: none. - Put the focus ring on the input.
- Use a chip for a single yes or no. That is one checkbox with a label.
Checks before you ship
Section titled “Checks before you ship”- Tab reaches every chip and space toggles it.
- Arrow keys move within a radio group and skip the rest.
- The group has a legend, not just a heading.