Skip to the content
human-kit design
11 — Form

Radio Group

One answer out of a few options, all of them in sight.

Installation

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

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

Who pays the consultation

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.

Is the patient allergic to penicillin

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.

Plan

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, a Switch.Root or a Checkbox.Root.
  • Ask the question in a Field.Root with group around the group, and keep each option's label short; a line of detail goes in its description.
  • Use horizontal for 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 through aria-labelledby (or by an aria-label alone); each option is a role="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 Home and End go to the first and the last option.
  • A hidden input carries each option, so the group takes part in a native form with name, and required blocks the submit.
  • The field’s hint, or its error while there is one, is read with the group through aria-describedby; invalid sets aria-invalid.
  • The circle is 16px, but the area that takes a press is 24px, and the label takes it too.
  • readonly keeps the options in the tab order and the answer readable; disabled takes them out.

API reference

RadioGroup.Root

PropTypeDefault
valuestring | null

The value of the chosen option. Bindable.

—
defaultValuestring | null

The choice at the start, when you give no value.

—
onChange(value: string) => void

Called when an option is chosen.

—
invalidboolean

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

false
orientation'vertical' | 'horizontal'

One option under the other, or side by side.

'vertical'
requiredboolean

Sets aria-required and blocks a native submit with no answer. The mark is the Field.Root’s.

false
readonlyboolean

Shows the answer and refuses a change.

false
disabledboolean

Turns every option off.

false
namestring

The name of the hidden inputs, for a native form.

—
formstring

The id of the form of the hidden inputs, when the group is outside it.

—
classstring

Classes for the group.

—

Plus every attribute of a <div>, on the group.

RadioGroup.Item

PropTypeDefault
valuestring

The value this option gives the group. Unique within it.

—
labelstring

The words beside the circle. Without them, name the option with aria-label.

—
descriptionstring

A line under the label.

—
disabledboolean

Turns this option off alone.

false
idstring

The id of the hidden input, which the label points at. One is made up when none is given.

—
elementHTMLSpanElement | null

The circle. Bindable.

—

Plus the attributes of a <span>, on the circle.