Toggle group

Slant / Controls

A row of toggle buttons for one choice or several, with one tab stop and arrow keys.

Demo

Usage

@tuttn/slant
import { ToggleButton, ToggleGroup } from '@tuttn/slant'

// The value is always an array, in the items' order.
const marks = [{ value: 'bold', label: 'Bold' }, { value: 'italic', label: 'Italic' }] as const
const [style, setStyle] = useState<string[]>(['bold'])
<ToggleGroup type="multiple" items={marks} value={style} onValueChange={setStyle} label="Text style" />

// Single (the default) keeps at most one pressed; pressing it again empties the value.
// Children instead of items, here for icons; uncontrolled from defaultValue.
<ToggleGroup defaultValue={['list']} label="Layout">
  <ToggleButton value="list" icon aria-label="List"><ListIcon /></ToggleButton>
  <ToggleButton value="grid" icon aria-label="Grid"><GridIcon /></ToggleButton>
</ToggleGroup>

// Spaced instead of one bar, or a column, where Up and Down move.
<ToggleGroup items={views} label="View" variant="ghost" attached={false} />
<ToggleGroup items={marks} label="Text style" orientation="vertical" />

// In a form: one hidden input per pressed value. In a Field, the label names the group.
<Field>
  <Field.Label>Labels</Field.Label>
  <ToggleGroup type="multiple" items={labels} name="labels" attached={false} />
</Field>
// new FormData(form).getAll('labels') → ['bug', 'docs']

Props

PropTypeDefaultDescription
type'single' | 'multiple''single'`single` keeps at most one item pressed (pressing another replaces it); `multiple` toggles each on its own.
valuestring[]—The pressed items' values, in document order. Pass it to control the group; leave it out to let the group keep its own.
defaultValuestring[][]The values an uncontrolled group starts with.
onValueChange(value: string[]) => void—Called with the pressed values, in the items' document order, whenever an item is pressed or released.
itemsreadonly ToggleGroupItem[]—The items, for uniform content. Leave it out and pass `<ToggleButton value="…">` children instead.
labelstring—The group's accessible name. A Field's label names it instead, through aria-labelledby.
orientation'horizontal' | 'vertical''horizontal'A row (left and right arrows, swapped under RTL) or a column (up and down).
size'sm' | 'md' | 'lg'—The size every item takes, unless it sets its own.
variant'primary' | 'secondary' | 'ghost''secondary' (Button's)The variant every item takes, unless it sets its own.
attachedbooleantrueJoins the items into one bar with shared borders, as an attached ButtonGroup does.
disabledboolean—Disables every item and leaves no tab stop. A disabled group is not submitted with its form.
namestring—Submits the group with a form: one hidden input with this name per pressed value.
formstring—The id of the form the hidden inputs belong to, when the group sits outside it.
items[]{ value, label, aria-label?, disabled? }—One ToggleButton per entry. `label` is the visible content; give `aria-label` when it is only an icon.
…restHTMLAttributes<HTMLDivElement>—className, style, aria-* and handlers go on the role="group" div, and so does the ref. Your onKeyDown runs first: call preventDefault in it to keep the arrows from moving focus.

Keyboard

Tabmoves into the group, to its first pressed item (else the first enabled one), and on out of it: the group is one tab stop
← / →move focus to the previous or next item without pressing it; they wrap at the ends, skip disabled items and swap under right-to-left text
↑ / ↓do the same in a column (orientation="vertical")
Home / Endjump to the first or last enabled item
Space / Enterpress or release the focused item

Accessibility

  • A role="group" named by label, or inside a Field by the field's label through aria-labelledby, with its description and error in aria-describedby. Each item is a native button with aria-pressed.
  • One tab stop, a roving tabIndex. While focus is inside, the tab stop follows it, so Tab and Shift+Tab leave from where you are; once focus has left, it goes back to the pressed item.
  • The arrows only move focus, so a screen reader user hears each item before pressing one.
  • Name icon-only items: aria-label on the entry in items, or on the ToggleButton child.
  • It is not a radio group: even type="single" can be emptied, and each item reads as a toggle button. For a choice that always has an answer, use Segmented, a radiogroup.
  • Disabled items keep their place and the arrows pass over them. A disabled group leaves no tab stop and submits nothing.

Single or multiple

The value is the pressed items' values, in the items' document order rather than the order they were pressed in. type="single", the default, keeps at most one: pressing another replaces it, and pressing the pressed one empties the array. type="multiple" toggles each item on its own. For a choice that must always have exactly one answer, use Segmented.

Look

The items join into one bar with shared borders, as in an attached ButtonGroup, unless attached={false}: then they sit apart and wrap. They take the group's size and variant (secondary unless you set one), and any item can set its own. A pressed item shows Segmented's raised surface and inset hairline, or fills with its tone in a primary group.

Forms

With a name, the group renders one hidden input per pressed value, so FormData.getAll(name) returns the array; form ties them to a form elsewhere on the page. Inside a Field, the field's label names the group, and its description and error describe it.