Skip to content

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.

tsx
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

PropTypeDefaultDescription
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
elevationshared0 | 1 | 2 | 3Drop shadow depth for every toggle in the set
valuereadonly string[]Which toggles are on, by their value. An array in both the single and the multiple case
defaultValuereadonly string[]Which start on, for an uncontrolled set
onValueChange(value: string[]) => voidFired on every change
multiplebooleanfalseWhether 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
disabledbooleanDisables every toggle in the set at once
loopFocusbooleantrueWhether the arrow keys wrap around at the ends
fullWidthbooleanfalseStretches to the container and divides the width evenly between the toggles
childrenReactNodeThe 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 loopFocus decides whether they wrap at the ends.
  • A group has no name of its own: give it an aria-label.

Released under the MIT License