Skip to content

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.

tsx
import { Toggle } from 'neba';

<Toggle defaultPressed>Bold</Toggle>;

Props

PropTypeDefaultDescription
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
elevationshared0 | 1 | 2 | 30Drop shadow depth. 0 means no shadow at all
pressedbooleanWhether it is on. With onPressedChange it makes the toggle controlled
defaultPressedbooleanfalseWhether it starts on, for an uncontrolled toggle
onPressedChange(pressed: boolean) => voidFired on every change
valuestringIdentifies the toggle inside a ToggleGroup
startIconReactNodeContent before the label. Sized in em, so it tracks the label
endIconReactNodeContent after the label
disabledbooleanfalseUnavailable. Drops the colour family for neutral grey
fullWidthbooleanfalseStretches to the width of the container
childrenReactNodeThe 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> carrying aria-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.

Released under the MIT License