Toggle
A button that stays down. It holds a state rather than running an action: bold on the selected words, the grid on the canvas, a filter on the list.
import { Toggle } from 'neba';
<Toggle defaultPressed>Bold</Toggle>;Props
| Prop | Type | Default | Description |
|---|---|---|---|
| variantshared | 'solid' | 'outline' | 'text' | 'outline' | How the toggle looks while it is **off**. On is always the colour family asserting itself |
| sizeshared | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'md' | Height and type scale |
| colorshared | 'primary' | 'secondary' | 'success' | 'warning' | 'danger' | 'info' | 'primary' | The semantic role it turns when it goes on. Off it is neutral in every family |
| densityshared | 'default' | 'compact' | 'default' | Padding only: never the height, never the type scale |
| elevationshared | 0 | 1 | 2 | 3 | 0 | Drop shadow depth. 0 means no shadow at all |
| pressed | boolean | — | Whether it is on. With onPressedChange it makes the toggle controlled |
| defaultPressed | boolean | false | Whether it starts on, for an uncontrolled toggle |
| onPressedChange | (pressed: boolean) => void | — | Fired on every change |
| value | string | — | Identifies the toggle inside a ToggleGroup |
| startIcon | ReactNode | — | Content before the label. Sized in em, so it tracks the label |
| endIcon | ReactNode | — | Content after the label |
| disabled | boolean | false | Unavailable. Drops the colour family for neutral grey |
| fullWidth | boolean | false | Stretches to the width of the container |
| children | ReactNode | — | The label. Without one the toggle goes square around its icon and needs an aria-label |
Every <button> attribute passes through except value, which identifies the toggle inside a ToggleGroup, and color, which is the semantic family. The shared axes are described in prop conventions.
A Switch changes a setting and the change itself is the point; a Checkbox is an answer that goes in a form. This is neither: it is a control that acts on whatever is beside it.
Examples
pressed and onPressedChange
pressed with onPressedChange makes it controlled; defaultPressed makes it uncontrolled.
variant
variant says how the toggle looks while it is off. outline is the default, solid is a filled plate that fills with the accent, and text has no surface at all until it is hovered or on.
An off toggle carries no colour family in any of the three: the plate is neutral and the label is muted. Turning it on moves the plate, the label and the hairline into the accent together.
Icon-only
With no children the toggle goes square around whatever startIcon it was given, which is the shape a toolbar wants. An icon carries no accessible name, so give it an aria-label.
size
size is the same control height a Button, a TextField and a Chip use, so a toggle keeps the baseline in a mixed row.
color
color is what the toggle turns when it goes on. Off it is neutral in every family.
Accessibility
- Renders a
<button>carryingaria-pressed. - An icon-only toggle needs an
aria-label; there is no text to take a name from. - Inside a ToggleGroup the set is one tab stop and the arrow keys move between the members.