Radio Group
One answer out of a few options, all of them in sight.
Installation
pnpm dlx @human-kit/design add radio-groupnpx @human-kit/design add radio-groupyarn dlx @human-kit/design add radio-groupbunx @human-kit/design add radio-groupThe command copies the files to src/lib/components/ui/radio-group. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Field from '$lib/components/ui/field';
import * as RadioGroup from '$lib/components/ui/radio-group';
let shift = $state('morning');
</script>
<Field.Root label="Shift" group>
{#snippet children(control)}
<RadioGroup.Root {...control} bind:value={shift}>
<RadioGroup.Item value="morning" label="Morning" />
<RadioGroup.Item value="afternoon" label="Afternoon" />
</RadioGroup.Root>
{/snippet}
</Field.Root> Examples
With its question
The group is the options alone. Its question comes from a Field.Root with group, which names the set; an option keeps a line of detail in its own description.
It is billed at the end of the month.
Invalid
error on the field turns the question and every circle red, and takes the place of the hint. Clear it as soon as there is an answer.
Answer before the prescription.
Answer before the prescription.Disabled options
disabled on an item turns that option off alone; the arrows then skip it. Say why in the field's description, so the reader does not wonder.
The plan is changed by the company.
Guidelines
- Use it for one answer out of two to six options that the reader should see side by side.
- For more options, or ones that need no comparing, use a
Select.Root. For a setting that turns on or off, aSwitch.Rootor aCheckbox.Root. - Ask the question in a
Field.Rootwithgrouparound the group, and keep each option's label short; a line of detail goes in itsdescription. - Use
horizontalfor two or three short options, as Yes and No. Longer labels read better one under the other. - Choose a sensible answer up front when there is one. Leave it empty, with
required, when the answer must be a decision.
Accessibility
- The group is a
role="radiogroup", named by the field’s question througharia-labelledby(or by anaria-labelalone); each option is arole="radio"named by its own. - Tab enters the group on the chosen option and leaves it in one step. The arrows move the choice, and
HomeandEndgo to the first and the last option. - A hidden input carries each option, so the group takes part in a native form with
name, andrequiredblocks the submit. - The field’s hint, or its error while there is one, is read with the group through
aria-describedby;invalidsetsaria-invalid. - The circle is 16px, but the area that takes a press is 24px, and the label takes it too.
readonlykeeps the options in the tab order and the answer readable;disabledtakes them out.
API reference
RadioGroup.Root
| Prop | Type | Default |
|---|---|---|
value | string | null The value of the chosen option. Bindable. | — |
defaultValue | string | null The choice at the start, when you give no | — |
onChange | (value: string) => void Called when an option is chosen. | — |
invalid | boolean Turns every circle red. A | false |
orientation | 'vertical' | 'horizontal' One option under the other, or side by side. | 'vertical' |
required | boolean Sets | false |
readonly | boolean Shows the answer and refuses a change. | false |
disabled | boolean Turns every option off. | false |
name | string The | — |
form | string The id of the form of the hidden inputs, when the group is outside it. | — |
class | string Classes for the group. | — |
Plus every attribute of a <div>, on the group.
RadioGroup.Item
| Prop | Type | Default |
|---|---|---|
value | string The value this option gives the group. Unique within it. | — |
label | string The words beside the circle. Without them, name the option with | — |
description | string A line under the label. | — |
disabled | boolean Turns this option off alone. | false |
id | string The id of the hidden input, which the label points at. One is made up when none is given. | — |
element | HTMLSpanElement | null The circle. Bindable. | — |
Plus the attributes of a <span>, on the circle.