Skip to the content
human-kit design
05 — Form

Checkbox Group

Several checkboxes answering one question, with its label, its hint and its error.

Installation

pnpm dlx @human-kit/design add checkbox-group
npx @human-kit/design add checkbox-group
yarn dlx @human-kit/design add checkbox-group
bunx @human-kit/design add checkbox-group

The 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.

Screening tests

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".

Days the clinic is open

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.Root with group for its question, a hint and an error, and spread control on 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 horizontal for a few short options, such as days of the week. Keep longer labels in a column.
  • required on the field draws a mark after the question and enforces no count. Check the value, and show what is missing in the field’s error.

Accessibility

  • The boxes sit in a role="group". Inside a Field.Root it is named by the question through aria-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. orientation changes the layout only.
  • invalid sets aria-invalid on 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 with aria-label or aria-labelledby.
  • disabled reaches every box, and their labels follow it.

API reference

PropTypeDefault
invalidboolean

Turns every box red. A Field.Root sets it from its error.

false
value(string | number)[]

The values of the ticked boxes. Bindable.

—
defaultValue(string | number)[]

The boxes ticked at the start, when you give no value.

—
onChange(value: (string | number)[]) => void

Called when the user ticks or clears a box.

—
namestring

The name of every box’s hidden input, for the form.

—
orientation'vertical' | 'horizontal'

A column of boxes, or a row that wraps.

'vertical'
requiredboolean

Sets required on the boxes’ inputs. The mark is the Field.Root’s.

false
disabledboolean

Turns off every box.

false
readonlyboolean

Shows the choice and refuses changes.

false
idstring

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.