ToggleGroup
A set of toggles that share one state. The corners facing a neighbour are squared off, the set owns the value, and the shared props are set once for every member.
import { Toggle, ToggleGroup } from 'neba';
<ToggleGroup aria-label="Text alignment" defaultValue={['left']}>
<Toggle value="left">Left</Toggle>
<Toggle value="center">Center</Toggle>
<Toggle value="right">Right</Toggle>
</ToggleGroup>;Props
| Prop | Type | Default | Description |
|---|---|---|---|
| variantshared | 'solid' | 'outline' | 'text' | — | Applied to every toggle in the set. Unset means the Toggle's own default stands |
| sizeshared | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | — | Height and type scale for every toggle in the set |
| colorshared | 'primary' | 'secondary' | 'success' | 'warning' | 'danger' | 'info' | — | Colour role for every toggle in the set. A toggle's own prop still wins |
| densityshared | 'default' | 'compact' | — | Horizontal padding for every toggle in the set |
| elevationshared | 0 | 1 | 2 | 3 | — | Drop shadow depth for every toggle in the set |
| value | readonly string[] | — | Which toggles are on, by their value. An array in both the single and the multiple case |
| defaultValue | readonly string[] | — | Which start on, for an uncontrolled set |
| onValueChange | (value: string[]) => void | — | Fired on every change |
| multiple | boolean | false | Whether more than one can be on at a time. Off, turning one on turns the last one off |
| orientationshared | 'horizontal' | 'vertical' | 'horizontal' | Which way the toggles run. Vertical flattens the top and bottom corners instead |
| disabled | boolean | — | Disables every toggle in the set at once |
| loopFocus | boolean | true | Whether the arrow keys wrap around at the ends |
| fullWidth | boolean | false | Stretches to the container and divides the width evenly between the toggles |
| children | ReactNode | — | The toggles. The shared props reach them even through a wrapper |
Every <div> attribute passes through. variant, size, color, density, elevation and disabled are passed to every Toggle in the set; a toggle's own prop still wins, so one danger toggle in a set of neutral ones is a normal thing to write.
Examples
value and onValueChange
The value is an array in both the single and the multiple case, so turning multiple on does not change its type. value with onValueChange makes the set controlled; defaultValue makes it uncontrolled.
multiple
Off, which is the default, means turning one toggle on turns the last one off. If what is being chosen is a value rather than a set of states, SegmentedButton or RadioGroup is the component that says so.
variant
The set's variant reaches every toggle in it.
orientation
vertical stacks the toggles and squares off the horizontal seams instead. The arrow keys follow the orientation.
fullWidth
Stretches to the container and divides the width evenly between the toggles.
Accessibility
- The set is one tab stop; the arrow keys move between the toggles and
loopFocusdecides whether they wrap at the ends. - A group has no name of its own: give it an
aria-label.