Checkbox Group
Several checkboxes answering one question, with its label, its hint and its error.
Installation
pnpm dlx @human-kit/design add checkbox-groupnpx @human-kit/design add checkbox-groupyarn dlx @human-kit/design add checkbox-groupbunx @human-kit/design add checkbox-groupThe command copies the files to src/lib/components/ui/checkbox-group. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as Checkbox from '$lib/components/ui/checkbox';
import * as CheckboxGroup from '$lib/components/ui/checkbox-group';
import * as Field from '$lib/components/ui/field';
let tests = $state<string[]>([]);
</script>
<Field.Root label="Screening tests" group>
{#snippet children(control)}
<CheckboxGroup.Root {...control} bind:value={tests}>
<Checkbox.Root value="hiv" label="HIV" />
<Checkbox.Root value="hepatitis-b" label="Hepatitis B" />
</CheckboxGroup.Root>
{/snippet}
</Field.Root> Examples
With its question
The group is the boxes alone. Its question and a hint come from a Field.Root with group, which names the set and links the line under it.
Pick every test the lab will run.
Error
error on the field takes the place of the hint and turns the question and every box red. The group does not count the ticks for you: set the error from the value, as here with "at least one".
Pick at least one day.
Pick at least one day.Guidelines
- Use a checkbox group for several answers to one question. For one answer out of several, use a radio group.
- Wrap the group in a
Field.Rootwithgroupfor its question, a hint and an error, and spreadcontrolon it. The boxes keep labels of their own. - Give each box a
value: the group’s value is the list of the ticked ones. - Use
horizontalfor a few short options, such as days of the week. Keep longer labels in a column. requiredon the field draws a mark after the question and enforces no count. Check the value, and show what is missing in the field’serror.
Accessibility
- The boxes sit in a
role="group". Inside aField.Rootit is named by the question througharia-labelledby, and described by the hint or the error. - Each box keeps its own tab stop and ticks with
Space; the arrow keys do nothing.orientationchanges the layout only. invalidsetsaria-invalidon the group. The required mark is hidden from screen readers: say it in the question or the hint too.- Outside a
Field.Root, name the group witharia-labeloraria-labelledby. disabledreaches every box, and their labels follow it.
API reference
| Prop | Type | Default |
|---|---|---|
invalid | boolean Turns every box red. A | false |
value | (string | number)[] The values of the ticked boxes. Bindable. | — |
defaultValue | (string | number)[] The boxes ticked at the start, when you give no | — |
onChange | (value: (string | number)[]) => void Called when the user ticks or clears a box. | — |
name | string The | — |
orientation | 'vertical' | 'horizontal' A column of boxes, or a row that wraps. | 'vertical' |
required | boolean Sets | false |
disabled | boolean Turns off every box. | false |
readonly | boolean Shows the choice and refuses changes. | false |
id | string The id of the group. One is made when you give none. | — |
Plus every attribute of a <div>, set on the group. class goes on the group.