Radio group
A single choice from native radios, as a list or as cards.
Demo
Usage
import { Field, RadioGroup } from '@tuttn/slant'
const reviewers = [
{ value: 'anyone', label: 'Anyone on the team' },
{ value: 'owners', label: 'Code owners', description: 'From the CODEOWNERS file' },
]
// Controlled: value and onValueChange.
const [reviewer, setReviewer] = useState('owners')
<RadioGroup label="Reviewers" options={reviewers} value={reviewer} onValueChange={setReviewer} />
// Uncontrolled: it keeps its own choice from defaultValue, and a form submits
// it under name.
<RadioGroup label="Reviewers" options={reviewers} defaultValue="anyone" name="reviewers" />
// Cards, composed: an aside sits at the inline end (a price, say), and the
// chosen card shows a check.
<RadioGroup label="Plan" variant="card" defaultValue="studio" name="plan">
<RadioGroup.Item value="solo" description="One workspace" aside="Free">Solo</RadioGroup.Item>
<RadioGroup.Item value="studio" description="Unlimited workspaces" aside="$12/mo">Studio</RadioGroup.Item>
</RadioGroup>
// Choice cards: media on top, in equal columns across the row.
<RadioGroup label="Layout" variant="card" orientation="horizontal" value={layout} onValueChange={setLayout}
options={[{ value: 'board', label: 'Board', media: <img src="/board.png" alt="" /> }, …]} />
// In a Field, the field's label names the group, and its description and error describe it.
<Field invalid={!plan} required>
<Field.Label>Plan</Field.Label>
<RadioGroup options={plans} value={plan} onValueChange={setPlan} />
<Field.Error>Choose a plan.</Field.Error>
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | — | The chosen value. Pass it to control the group; leave it out to let the group keep its own. |
defaultValue | string | none chosen | The value an uncontrolled group starts with. |
onValueChange | (value: string) => void | — | Called with the option's value when a click or an arrow key chooses it. |
options | readonly RadioOption[] | — | The radios, for uniform content. Leave it out and pass `<RadioGroup.Item>` children instead. |
label | string | — | The group's accessible name. Inside a Field, the field's label names it instead. |
name | string | generated | The radios' shared name, which ties them into one group and which a form submits the chosen value under. |
form | string | — | The id of the form the radios belong to, when the group sits outside it. |
variant | 'default' | 'card' | 'default' | `default` is a mark and a label per option; `card` is a bordered card per option that fills with the accent and shows a check while chosen, with room for `media` on top and an `aside` at the end. |
orientation | 'vertical' | 'horizontal' | 'vertical' | A column of options, or a row that wraps (cards share it in equal columns). |
size | 'sm' | 'md' | 'md' | `md` is a 14px mark with 13px text; `sm` a 12px mark with 12px text. |
disabled | boolean | — | Disables every radio. A disabled group is not submitted with its form. |
required | boolean | — | Requires a choice: aria-required on the group, and `required` on the radios for the form's own validation. |
invalid | boolean | — | Sets aria-invalid on the group and a danger border on its unchosen marks. Inside a Field, the field's `invalid` does this. |
…rest | HTMLAttributes<HTMLDivElement> | — | Other attributes and the ref reach the radiogroup element. |
RadioGroup.Item | value, children, description, aside, media, disabled | — | One radio, for the compound form; its options mirror the `options` entries. Its ref and other input attributes reach the native radio; className and style go on the <label> around it. |
Keyboard
The radios are native, so the browser does the keys: Tab enters the group at the chosen radio (the first, while none is chosen), the arrow keys move the choice and skip disabled radios, and Tab leaves the group.
Accessibility
The root is a role="radiogroup" named by label, or, inside a Field, by the field's label through aria-labelledby, with the field's description and error in its aria-describedby. required and invalid become aria-required and aria-invalid on the group, and required also reaches the radios, so the form's own validation asks for a choice.
Each radio's label alone names it; its description and aside describe it. Media is decorative unless it carries its own alt text, and it never changes the radio's name. Media shows on cards only.
Layout
A horizontal group of cards shares its row in equal columns that wrap below --sl-radio-card-min (10rem). The plan picker is a radio group of cards. For a compact choice in a toolbar, use a segmented control; for several choices at once, checkboxes.