- Docs
- Components
- Actions
Toggle
A tool that's pressed or not. With only an icon, its name shows as its tooltip.
- Import
- @qeetrix/react
- Status
- StableSince 0.0.2
- Renders
<button aria-pressed>Server-rendered- Tested
- WCAG 2.2 AAaxe on every pull request
01
Import
// From the package: a build keeps only what it uses.
import { Toggle } from "@qeetrix/react";
// Or from its own path.
import { Toggle } from "@qeetrix/react/toggle";02
Props
And every native attribute of the element it renders.
| Prop | Type | Default | What it does |
|---|---|---|---|
pressed | boolean | — | Pressed, controlled. |
defaultPressed | boolean | false | Pressed to start with, uncontrolled. |
onPressedChange | (pressed, details) => void | — | Called with the new state. |
icon | IconSource | — | An icon from @qeetrix/icons, drawn sharp, before the label. |
filledWhenPressed | boolean | false | The icon's filled drawing while pressed; for icons that have one. |
aria-label | string | — | Required with an icon alone: its name and its tooltip. |
tooltip | ReactNode | false | aria-label | An icon-only toggle's tooltip: other text, or false. |
tooltipSide | "top" | "bottom" | "left" | "right" | "inline-start" | "inline-end" | "top" | The side it opens on; it flips when there's no room. |
size | "sm" | "md" | "lg" | "md" | Height from the density tokens; a group's size wins. |
value | string | — | Its value inside a Toggle group. |
disabled | boolean | false | It can't be pressed. |
children | ReactNode | — | The label. |
03
Parts
Each part carries a data-slot, for tests and placement.
| data-slot | Element | What it is |
|---|---|---|
toggle | <button> | The root. data-pressed while on; data-size; data-icon-only. |
toggle-icon | <svg> | The icon, outline. |
toggle-icon-filled | <svg> | The icon, filled: shown while pressed, with filledWhenPressed. |
04
Controlled
pressed and onPressedChange, for a toggle the page reads from.
const [pinned, setPinned] = useState(false);
<Toggle icon={PinIcon} filledWhenPressed pressed={pinned} onPressedChange={setPinned}>
Pin to top
</Toggle>