ToggleGroup
A row or column of related toggle buttons where one or several can be pressed: text formatting (bold, italic, underline), alignment, view switches. Compose it from ToggleGroup.Items; each item is a native <button> that takes disabled, aria-label, title and event handlers directly.
Reach for a different component when the choice is different:
- Exactly one option must always be chosen, with a sliding indicator:
SegmentedControl. - A form value with radio semantics (
name, validation, labels):RadioGroup. - A single independent on/off button:
ToggleButton.
Examples
Single
Without multiple at most one item is pressed. Pressing the pressed item turns it off, and onValueChange receives null. Add required to keep one item pressed.
import { ToggleGroup } from '@var-ui/react';
<ToggleGroup aria-label="View" defaultValue="list">
<ToggleGroup.Item value="list">List</ToggleGroup.Item>
<ToggleGroup.Item value="grid">Grid</ToggleGroup.Item>
<ToggleGroup.Item value="board">Board</ToggleGroup.Item>
</ToggleGroup>Multiple
With multiple, any number of items can be pressed and value / onValueChange use string[].
import { ToggleGroup } from '@var-ui/react';
<ToggleGroup aria-label="Text style" multiple defaultValue={['bold']}>
<ToggleGroup.Item value="bold">Bold</ToggleGroup.Item>
<ToggleGroup.Item value="italic">Italic</ToggleGroup.Item>
<ToggleGroup.Item value="underline">Underline</ToggleGroup.Item>
<ToggleGroup.Item value="strike" disabled>
Strike
</ToggleGroup.Item>
</ToggleGroup>Icons only
Icon-only items must carry an aria-label. Use required for a choice that always has a value, like alignment.
import { ToggleGroup } from '@var-ui/react';
function AlignIcon({ align }: { align: 'left' | 'center' | 'right' }) {
const widths = [14, 9, 12];
return (
<svg width="16" height="16" viewBox="0 0 16 16" aria-hidden="true" fill="currentColor">
{widths.map((w, i) => {
const x = align === 'left' ? 1 : align === 'right' ? 15 - w : (16 - w) / 2;
return <rect key={i} x={x} y={3 + i * 4} width={w} height="2" rx="1" />;
})}
</svg>
);
}
<ToggleGroup aria-label="Text alignment" defaultValue="left" required>
<ToggleGroup.Item value="left" aria-label="Align left" title="Align left">
<AlignIcon align="left" />
</ToggleGroup.Item>
<ToggleGroup.Item value="center" aria-label="Align center" title="Align center">
<AlignIcon align="center" />
</ToggleGroup.Item>
<ToggleGroup.Item value="right" aria-label="Align right" title="Align right">
<AlignIcon align="right" />
</ToggleGroup.Item>
</ToggleGroup>Sizes, orientation, tone and appearance
size, tone, appearance (outline, subtle, ghost) and orientation use the same names as the recipe axes. A vertical group moves focus with Up and Down.
import { HStack, ToggleGroup } from '@var-ui/react';
<HStack gap="lg">
{(['sm', 'md', 'lg'] as const).map((size) => (
<ToggleGroup key={size} aria-label={"Size " + size} size={size} defaultValue="a">
<ToggleGroup.Item value="a">One</ToggleGroup.Item>
<ToggleGroup.Item value="b">Two</ToggleGroup.Item>
</ToggleGroup>
))}
<ToggleGroup aria-label="Vertical" orientation="vertical" tone="success" defaultValue="b">
<ToggleGroup.Item value="a">Top</ToggleGroup.Item>
<ToggleGroup.Item value="b">Middle</ToggleGroup.Item>
<ToggleGroup.Item value="c">Bottom</ToggleGroup.Item>
</ToggleGroup>
<ToggleGroup aria-label="Ghost" appearance="ghost" multiple defaultValue={['a']}>
<ToggleGroup.Item value="a">Ghost</ToggleGroup.Item>
<ToggleGroup.Item value="b">Group</ToggleGroup.Item>
</ToggleGroup>
</HStack>In a toolbar
The group works inside a Toolbar. Toolbar has no arrow-key handling of its own, so each group keeps its own Tab stop and arrow-key roving; they do not conflict. Give the toolbar a label and each group an aria-label.
import { Button, ToggleGroup, Toolbar } from '@var-ui/react';
<Toolbar
label="Formatting"
startContent={
<ToggleGroup aria-label="Text style" multiple>
<ToggleGroup.Item value="bold">Bold</ToggleGroup.Item>
<ToggleGroup.Item value="italic">Italic</ToggleGroup.Item>
</ToggleGroup>
}
endContent={<Button>Share</Button>}
/>Controlled
Drive the pressed items with value and onValueChange.
Pressed: bold
import { ToggleGroup } from '@var-ui/react';
import { useState } from 'react';
const [value, setValue] = useState<string[]>(['bold']);
<ToggleGroup aria-label="Text style" multiple value={value} onValueChange={setValue}>
<ToggleGroup.Item value="bold">Bold</ToggleGroup.Item>
<ToggleGroup.Item value="italic">Italic</ToggleGroup.Item>
<ToggleGroup.Item value="underline">Underline</ToggleGroup.Item>
</ToggleGroup>
<p>Pressed: {value.join(', ') || 'none'}</p>Props
ToggleGroup.Item takes value (required), disabled and everything a <button> takes. Pressing it calls your onClick first; call event.preventDefault() there to cancel the toggle.
Accessibility
- Pattern: the APG toggle button pattern. The root is
role="group"(name it witharia-labeloraria-labelledby) and every item is a native<button type="button">witharia-pressed. This is the correct semantics for deselectable single-select and for multi-select. ToggleGroup never usesrole="radiogroup", because radios cannot be turned off; useSegmentedControlorRadioGroupwhen you want radio behaviour. - Focus: roving tabindex (the APG toolbar keyboard model). The group is a single Tab stop, on the pressed item or else the first enabled item. Arrow keys move focus without pressing, and do not wrap. Disabled items are skipped.
- RTL: Left and Right are mirrored in a right-to-left context (read from the computed
direction); the connected corners and borders use logical properties. - Forced colors: the pressed item is drawn with
Highlight/HighlightText, so the state does not depend on a background color. - Static markup: items render as plain buttons with no
tabindex, so before hydration every item is tabbable and pressed items already carryaria-pressedanddata-selected. The roving Tab stop is applied on the client. There is no Astro component because pressing needs client state. - The group is not a form control and submits nothing. Mirror
valueinto a hidden<input>if you need it in a form.
| Key | Action |
|---|---|
| Tab / Shift+Tab | Moves focus into or out of the group (one Tab stop) |
| Right / Left arrow | Next / previous item (mirrored in RTL), horizontal |
| Down / Up arrow | Next / previous item, vertical |
| Home / End | First / last enabled item |
| Space / Enter | Toggles the focused item |