Toggle
A two-state toggle button built on Base UI
Loading...
Installation
pnpm dlx shadcn@latest add https://prototyper-ui.com/r/toggle.jsonnpx shadcn@latest add https://prototyper-ui.com/r/toggle.jsonyarn dlx shadcn@latest add https://prototyper-ui.com/r/toggle.jsonbunx --bun shadcn@latest add https://prototyper-ui.com/r/toggle.jsonThis will add the following files to your project:
components/ui/toggle.tsx
Usage
import { Toggle } from "@/components/ui/toggle"
<Toggle>Bold</Toggle>Examples
Outline
Loading...
Small
Loading...
Large
Loading...
Disabled
Loading...
With Text
Loading...
Styling
Data Slots
Use data-slot attributes to target the toggle in CSS:
| Slot name | Element |
|---|---|
toggle | The <button> root |
Customization Examples
/* Style all pressed toggles */
[data-slot="toggle"][aria-pressed="true"] {
@apply bg-primary text-primary-foreground;
}{/* Use className for one-off overrides */}
<Toggle className="rounded-full">Pill Toggle</Toggle>API Reference
Toggle
A two-state toggle button that can be pressed or unpressed.
Prop
Type
All Base UI Toggle props are forwarded via ...props.
toggleVariants
A cva helper exported for use outside of the <Toggle> component.
import { toggleVariants } from "@/components/ui/toggle"
<button className={toggleVariants({ variant: "outline", size: "sm" })}>Custom Toggle</button>Accessibility
Keyboard Interactions
| Key | Action |
|---|---|
Space | Toggles the pressed state |
Enter | Toggles the pressed state |
Tab | Moves focus to / away from the toggle |
ARIA Attributes
- Renders as a
<button>element witharia-pressedattribute. aria-pressed="true"when the toggle is in the pressed state,aria-pressed="false"when unpressed.- Screen readers announce the toggle as a toggle button with its current pressed/unpressed state.
disabledattribute is set when the toggle is disabled.