Toggle button

Slant / Controls

A button that stays pressed until it is pressed again.

Demo

Usage

@tuttn/slant
import { ButtonGroup, ToggleButton } from '@tuttn/slant'

// Controlled: pass pressed and onPressedChange.
const [watching, setWatching] = useState(false)
<ToggleButton pressed={watching} onPressedChange={setWatching}><EyeIcon /> Watch</ToggleButton>

// Uncontrolled: leave pressed out; it keeps its own state from defaultPressed.
<ToggleButton defaultPressed icon aria-label="Notify me"><BellIcon /></ToggleButton>

// In a ButtonGroup it takes the group's size and variant, like any Button.
// A primary toggle fills with its tone only while pressed.
<ButtonGroup label="Editor" size="sm">
  <ToggleButton defaultPressed>Wrap</ToggleButton>
  <ToggleButton variant="primary">Preview</ToggleButton>
</ButtonGroup>

// For a set with one tab stop and arrow keys, put them in a ToggleGroup.

Props

PropTypeDefaultDescription
pressedboolean—Whether the button is pressed. Pass it to control the button; leave it out to let the button keep its own state. Inside a ToggleGroup the group's value decides instead.
defaultPressedbooleanfalseThe state an uncontrolled button starts in.
onPressedChange(pressed: boolean) => void—Called with the next state when a click, Space or Enter toggles a button outside a ToggleGroup.
valuestring—Inside a ToggleGroup: what pressing the button puts in the group's value. Required there.
variant'primary' | 'secondary' | 'ghost''ghost', or the group's inside a ButtonGroup or ToggleGroupEmphasis. A primary toggle fills with its tone only while pressed.
toneTone—Semantic colour. `primary` fills with it, so `tone="danger" variant="primary"` is a destructive button; `secondary` and `ghost` take it as their text and border colour. With `accent` and `neutral` they keep their usual look.
size'sm' | 'md' | 'lg''md', or the ButtonGroup'sHeight on the control scale: 28, 34 or 40px at regular density. `lg` also sets larger text and padding.
iconboolean—A square button holding only an icon; give it an aria-label.
fullWidthboolean—Stretches the button to the width of its container.
…restButtonProps—Every Button prop but asChild and loading (tone, size, icon, fullWidth), and every button attribute. They and the ref reach the <button>.

Keyboard

Tabmoves to the toggle; each one outside a ToggleGroup is its own tab stop
Space / Enterpresses or releases it

Accessibility

  • A native <button type="button"> with aria-pressed: screen readers read "toggle button, pressed" or "not pressed".
  • Keep the name the same in both states ("Watch", not "Watch" then "Unwatch"). aria-pressed already says whether it is on, so a changing name reads as "Unwatch, pressed".
  • An icon toggle has no text: give it an aria-label.
  • Pressed shows as a raised surface with an inset hairline, not as a change of colour alone.
  • Disabled, it keeps its state and ignores clicks and keys.

Pressed, not checked

A toggle button's state is pressed, defaultPressed and onPressedChange, after the aria-pressed it sets, rather than the checked a Switch or checkbox uses: a pressed button is still a button. Use a Switch for a setting that takes effect at once, and a toggle for a formatting or view option beside other buttons.

Look

On its own a toggle is ghost. Inside a ButtonGroup or ToggleGroup it takes the group's variant and size, unless it sets its own. Pressed, it shows the raised surface and inset hairline of Segmented's chosen option; released, an accent or neutral toggle's text is muted. data-pressed mirrors aria-pressed for your own styles.