Skip to the content
human-kit design
18 — Form

Toggle Group

Toggles side by side, one of them in or several, reached by the arrow keys.

Installation

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

The command copies the files to src/lib/components/ui/toggle-group. It also installs the packages that the files import.

Usage

<script lang="ts">
	import * as ToggleGroup from '$lib/components/ui/toggle-group';

	let shown = $state(['all']);
</script>

<ToggleGroup.Root selectionMode="single" aria-label="Show" bind:value={shown}>
	<ToggleGroup.Item value="all">All</ToggleGroup.Item>
	<ToggleGroup.Item value="mine">Mine</ToggleGroup.Item>
</ToggleGroup.Root>

Examples

A toolbar of icons

With selectionMode="multiple" each toggle turns on and off by itself, as text styles do. An icon alone needs its aria-label.

Guidelines

  • Use selectionMode="single" to pick one of a few values, such as a filter. Add disallowEmptySelection when one must always be on.
  • Use selectionMode="multiple" for options that combine: bold, italic and underline.
  • Use ghost in a toolbar, outline beside the buttons of a form, and segment for a choice of one on a grey panel, where the hover grey of a picked toggle would not show.
  • To switch between views of the same thing, a list and a calendar, use segmented Tabs.Root: they pick a view, a toggle group picks a value.
  • Keep to two to five toggles. More options read better in a Select.Root.

Accessibility

  • The group is a role="group". Name it with aria-label or aria-labelledby.
  • The group is one tab stop. The arrow keys move between its toggles, Home and End go to the first and the last.
  • Each toggle is a native button with aria-pressed; Enter and Space press it.
  • A toggle with an icon alone needs an aria-label, the same in and out.

API reference

ToggleGroup.Root

PropTypeDefault
variant'ghost' | 'outline' | 'segment'

Joined toggles with no box, one box ruled between its toggles, or chips on a rail. The toggles read it from here.

'ghost'
size'sm' | 'default' | 'lg' | 'icon-sm' | 'icon' | 'icon-lg'

The size of every toggle, as a Toggle.Root's.

'default'
selectionMode'single' | 'multiple'

Whether one toggle or several can be on.

'single'
value(string | number)[]

The values of the toggles that are on. An array in both modes. Bindable.

—
defaultValue(string | number)[]

The values on at the start, when you give no value.

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

Called when a press changes what is on.

—
disallowEmptySelectionboolean

Keeps one toggle on: a press on the last one does not turn it off.

false
orientation'horizontal' | 'vertical'

The direction of the row, and of the arrow keys.

'horizontal'
disabledboolean

Turns off every toggle.

false
elementHTMLDivElement | null

The rendered element. Bindable.

—

Plus every attribute of a <div>.

ToggleGroup.Item

PropTypeDefault
valuestring | number

What the toggle puts in the group value when it is on.

—
disabledboolean

Turns this toggle off; the arrow keys skip it.

false
elementHTMLButtonElement | null

The rendered element. Bindable.

—

Plus every attribute of a native <button>. Its own selected and defaultSelected are ignored inside a group.