Toggle button
Slant / Controls
A button that stays pressed until it is pressed again.
Demo
Usage
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
| Prop | Type | Default | Description |
|---|---|---|---|
pressed | boolean | — | 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. |
defaultPressed | boolean | false | The 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. |
value | string | — | 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 ToggleGroup | Emphasis. A primary toggle fills with its tone only while pressed. |
tone | Tone | — | 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's | Height on the control scale: 28, 34 or 40px at regular density. `lg` also sets larger text and padding. |
icon | boolean | — | A square button holding only an icon; give it an aria-label. |
fullWidth | boolean | — | Stretches the button to the width of its container. |
…rest | ButtonProps | — | Every Button prop but asChild and loading (tone, size, icon, fullWidth), and every button attribute. They and the ref reach the <button>. |
Keyboard
| Tab | moves to the toggle; each one outside a ToggleGroup is its own tab stop |
| Space / Enter | presses or releases it |
Accessibility
- A native
<button type="button">witharia-pressed: screen readers read "toggle button, pressed" or "not pressed". - Keep the name the same in both states ("Watch", not "Watch" then "Unwatch").
aria-pressedalready says whether it is on, so a changing name reads as "Unwatch, pressed". - An
icontoggle has no text: give it anaria-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.