Toggle
A pressable on/off button for binary states like bold, italic, mute, or favorite. Built on Radix UI Toggle.
Overview
The Toggle component is a pressable on/off button for binary toggleable actions. Distinct from Switch — Toggle acts like a button that stays pressed (e.g. bold, italic, mute), while Switch is a sliding track for settings/preferences. Built on Radix UI's Toggle primitive for full accessibility.
Installation
Install the component via the CLI in one command.
npx @work-rjkashyap/unified-ui add togglepnpm dlx @work-rjkashyap/unified-ui add togglenpx @work-rjkashyap/unified-ui add togglebunx @work-rjkashyap/unified-ui add toggleIf you haven't initialized your project yet, run npx @work-rjkashyap/unified-ui init first. See the CLI docs for details.
Or install the full package
Use this approach if you prefer to install the entire design system as a dependency instead of copying individual components.
npm install @work-rjkashyap/unified-uipnpm add @work-rjkashyap/unified-uiyarn add @work-rjkashyap/unified-uibun add @work-rjkashyap/unified-uiAnatomy
import { Toggle } from "@work-rjkashyap/unified-ui";Examples
Basic Usage
Variants
Sizes
| Size | Height | Padding | Font Size |
|---|---|---|---|
sm | 32px | 8px | 12px |
md | 36px | 12px | 14px |
lg | 40px | 16px | 14px |
With Text Labels
With Icon Props
Default Pressed
Disabled State
Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "default" | "outline" | "ghost" | "default" | Visual variant of the toggle. |
size | "sm" | "md" | "lg" | "md" | Size of the toggle. |
pressed | boolean | — | Controlled pressed state. |
defaultPressed | boolean | — | Default pressed state for uncontrolled usage. |
onPressedChange | (pressed: boolean) => void | — | Callback fired when the pressed state changes. |
disabled | boolean | false | Disables the toggle. |
iconLeft | ReactNode | — | Icon rendered before the label. |
iconRight | ReactNode | — | Icon rendered after the label. |
className | string | — | Additional CSS classes. |
Accessibility
Icon-only toggles must include an aria-label to be accessible to screen readers.
- aria-pressed — Radix automatically manages the
aria-pressedattribute based on the toggle state. - Keyboard — Space and Enter keys toggle the pressed state (handled by Radix).
- Focus ring — Visible on keyboard navigation via
focus-visible. - Disabled — Uses both the
disabledattribute and Radix'sdata-disabledfor styling. - Role — Renders as a
<button>with implicit button role.
Toggle vs Switch
| Feature | Toggle | Switch |
|---|---|---|
| Looks like | A button that stays pressed | A sliding track with thumb |
| Best for | Toolbar actions (bold, italic) | Settings and preferences |
| Groupable | Yes, via ToggleGroup | No |
| Label | Icon or text inside the button | Label beside the track |
Design Tokens
| Token | Usage |
|---|---|
--muted | Hover background |
--secondary | Pressed state background (default) |
--foreground | Pressed state text color |
--muted-foreground | Unpressed text color |
--border | Outline variant border |
--border-strong | Outline pressed border |
--radius-md | Border radius |
--duration-fast | Transition speed |
--easing-standard | Easing curve |