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-groupnpx @human-kit/design add toggle-groupyarn dlx @human-kit/design add toggle-groupbunx @human-kit/design add toggle-groupThe 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. AdddisallowEmptySelectionwhen one must always be on. - Use
selectionMode="multiple"for options that combine: bold, italic and underline. - Use
ghostin a toolbar,outlinebeside the buttons of a form, andsegmentfor 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 witharia-labeloraria-labelledby. - The group is one tab stop. The arrow keys move between its toggles,
HomeandEndgo to the first and the last. - Each toggle is a native button with
aria-pressed;EnterandSpacepress it. - A toggle with an icon alone needs an
aria-label, the same in and out.
API reference
ToggleGroup.Root
| Prop | Type | Default |
|---|---|---|
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 | '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 | — |
onChange | (value: (string | number)[]) => void Called when a press changes what is on. | — |
disallowEmptySelection | boolean 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' |
disabled | boolean Turns off every toggle. | false |
element | HTMLDivElement | null The rendered element. Bindable. | — |
Plus every attribute of a <div>.
ToggleGroup.Item
| Prop | Type | Default |
|---|---|---|
value | string | number What the toggle puts in the group value when it is on. | — |
disabled | boolean Turns this toggle off; the arrow keys skip it. | false |
element | HTMLButtonElement | null The rendered element. Bindable. | — |
Plus every attribute of a native <button>. Its own selected and defaultSelected are ignored inside a group.