Switch checkbox

checkbox-switch is a family of five switch-styled checkbox components (ToggleCheckbox, PillCheckbox, SlideCheckbox, TrackCheckbox, CompactSwitchCheckbox) that render a role="switch" button with a spring-animated thumb and a clickable label. They are built on the ai2 token system, use framer-motion, and switch instantly under reduced motion.

componentstyledcheckboxregistry-item.json
Switch checkbox
LIVE · running in a sandboxed iframe
Installed with shadcn add · 2 workarounds · theme from registry.json
  • applied the registry's theme (tokens, fonts and CSS from the registry preview settings)
  • installed base item https://ai2.design/r/tokens.json (registry preview settings)
See how it was built

Installation

pnpm dlx shadcn@latest add @ai2/checkbox-switch

Requires @ai2 in the registries of your components.json (shadcn adds official directory namespaces automatically).

Usage

usage.tsx
import { ToggleCheckbox } from "@/components/ui/checkbox-switch"export function Example() {  return <ToggleCheckbox defaultChecked label="Airplane mode" />}
  • Use a toggle for an on/off setting that takes effect immediately, such as enabling a feature or switching a mode.
  • Pick PillCheckbox when the On/Off state should be visible as text inside the control, or SlideCheckbox for a more expressive fill animation.
  • Use TrackCheckbox for a lighter, outlined look on forms or settings screens, and CompactSwitchCheckbox for dense lists or tables.
  • Choose the size prop (sm to xl) to match the surrounding typography and touch targets; the invisible touch-area extension keeps small tracks usable.

Examples

Controlled toggle with state

Use checked and onCheckedChange to control the switch from parent state.

controlled-toggle-with-state.tsx
import * as React from "react"import { ToggleCheckbox } from "@/components/ui/checkbox-switch"export function NotificationSetting() {  const [enabled, setEnabled] = React.useState(false)  return (    <ToggleCheckbox      label="Email notifications"      checked={enabled}      onCheckedChange={(next) => setEnabled(next)}    />  )}

Pill switch with On/Off text

PillCheckbox shows the current state as text inside the track.

pill-switch-with-on/off-text.tsx
import { PillCheckbox } from "@/components/ui/checkbox-switch"export function SyncSetting() {  return <PillCheckbox size="lg" defaultChecked label="Auto sync" />}

Size scale for a settings list

Several switches in different sizes and variants for a compact settings panel.

size-scale-for-a-settings-list.tsx
import { SlideCheckbox, TrackCheckbox, CompactSwitchCheckbox } from "@/components/ui/checkbox-switch"export function DisplaySettings() {  return (    <div className="flex flex-col gap-3">      <SlideCheckbox size="sm" label="Dark theme" defaultChecked />      <TrackCheckbox size="md" label="Outlined switch" />      <CompactSwitchCheckbox size="xl" label="Compact row" />    </div>  )}

API reference

PropTypeDefaultDescription
size"sm" | "md" | "lg" | "xl""md"Size of the track, thumb, label text and PillCheckbox cap text. Exported as the StyledSize type.
labelstringToggleCheckbox: "Airplane mode"; PillCheckbox: "Auto sync"; SlideCheckbox: "Dark theme"; TrackCheckbox: "Outlined switch"; CompactSwitchCheckbox: "Compact row"Text rendered next to the switch. Clicking the label also toggles the switch.
checkedboolean—Controlled checked state. When provided, the component does not keep its own state and relies on onCheckedChange to update the parent.
defaultCheckedbooleanfalseInitial checked state for uncontrolled usage.
onCheckedChange(checked: boolean) => void—Called with the next checked value each time the switch is toggled, from either the track or the label.
classNamestring—Additional classes merged onto the button element (the track), not the row wrapper.
disabledboolean—Inherited from the native button props; disables interaction and applies disabled styling (opacity-50, not-allowed cursor).

Accessibility

  • The track is a button with role="switch" and aria-checked reflecting the current state, so screen readers announce on/off instead of checked/unchecked.
  • The track is labelled through aria-labelledby pointing to the label span, whose id comes from React.useId().
  • The PillCheckbox On/Off text is aria-hidden because the state is already exposed through aria-checked.
  • Keyboard operation follows the switch contract; the track is focusable and shows a visible focus-visible ring (3px ring-ring/50).
  • The clickable label span is not focusable, so keyboard users should use the track button.
  • Track hit areas are extended with after:-inset-1.5 so small tracks remain comfortable to tap.
  • Under prefers-reduced-motion, thumb and fill transitions are instant (duration 0).
  • Invalid state is styled with aria-invalid classes; set aria-invalid on the button via props so the styling applies.
  • Passing your own onClick overrides the internal toggle, because the spread props come after onClick on the track button; prefer onCheckedChange.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "Switch checkbox" component (ai2/checkbox-switch) from its shadcn registry.1. Install it with: npx shadcn@latest add @ai2/checkbox-switch2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: size, label, checked, defaultChecked, onCheckedChange, className, disabled.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { ToggleCheckbox } from "@/components/ui/checkbox-switch"export function Example() {  return <ToggleCheckbox defaultChecked label="Airplane mode" />}```

Files & dependencies

  • registry/ai2/styled/checkbox-switch.tsx→ components/ui/checkbox-switch.tsx
dependenciesmotion@^12.42.2
registryDependencies@ai2/tokens

Looks similar, elsewhere