Toggle
A button that stays pressed or unpressed: bold, favorite, mute. Sizes match Button. For several related toggles, use Toggle Group; for on/off settings in a form, use Switch.
Installation
Add Toggle to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.
- Adds
Toggle.tsxto your CK-UI folder (src/components/ck-ui by default)- Installs
@radix-ui/react-toggleclass-variance-authorityif your project does not have them yet
First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.
Manual installationInstall the packages and copy the files yourself.
Install the following dependencies
Run these commands in your terminal to install the required packages
Create or update your Toggle component
Copy this code to create your Toggle component
Test Your Component
- 1. Add the component to a test page
- 2. Check all props and variants work as expected
- 3. Verify responsive behavior
- 4. Test with different content/data
- 5. Ensure accessibility features work correctly
Favorite
Controlled with pressed + onPressedChange. Swap the icon (outline → filled) so the state is visible without relying on the background.
Mute
An icon-only toggle with a fixed accessible name.
Sizes and States
xs, sm, md and lg match Button heights; disabled toggles cannot be pressed.
Accessibility
- A
<button>witharia-pressed. Space and Enter toggle it. - Icon-only toggles need an
aria-labelthat names the action ("Bold"). Do not change the label with the state; assistive technology already reports pressed / not pressed. - The pressed state adds a strong border, a background and the foreground text color, so it does not rely on color alone; also swap the icon for important states.
- Use Switch for settings that take effect immediately in a form, and Checkbox for values submitted with a form.
API Reference
Built on Radix Toggle. Import with import * as Toggle from "@/components/ui/Toggle".
Toggle.Root
See Radix UI docs for base props.
| Prop | Type | Default | Description |
|---|---|---|---|
pressed | "boolean" | "undefined" | Pressed state (controlled). |
defaultPressed | "boolean" | "false" | Initial state (uncontrolled). |
onPressedChange | "(pressed: boolean) => void" | "undefined" | Called when toggled. |
variant | "ghost""stroke" | "ghost" | Treatment. |
size | "xs""sm""md""lg" | "md" | Height, matching Button. |
disabled | "boolean" | "false" | Prevents toggling. |
asChild | "boolean" | "false" | Render the single child element instead, merging this part's props and behaviour onto it. |
className | "string" | "undefined" | Additional classes, merged last. |