Switch

A control that allows the user to toggle between checked and not checked.

Installation

pnpm dlx cubix@latest add switch

Usage

Import
import { Switch } from "@/components/cubix/switch"
Example
<div className="flex items-center gap-2">
  <Switch id="airplane-mode" />
  <label htmlFor="airplane-mode">Airplane Mode</label>
</div>

Examples

With label

Pass children to render a label next to the switch, or compose your own label with htmlFor.

Disabled

Sizes

Settings row

Pair a switch with a title and description for preference panels.

Share analyticsHelp us improve Cubix by sending anonymous usage data.

API Reference

Props

PropTypeDefaultDescription
checkedbooleanfalseControlled checked state of the switch. Use with `onCheckedChange`.
defaultCheckedbooleanfalseDefault checked state for uncontrolled usage.
onCheckedChange(checked: boolean) => void-Called with the next checked value whenever the switch is toggled.
size"default" | "sm""default"The size of the switch.
disabledbooleanfalseDisables the switch.
namestring-The name attribute passed when submitting a form.
valuestringonThe value attribute passed when submitting a form.
aria-invalidbooleanfalseWhen true, indicates the switch is in an invalid state.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).
...propsReact.ComponentProps<'input'>-All native input attributes (id, form, tabindex, aria-*, ...) are forwarded to the rendered element.

Variants

PropTypeDefaultDescription
default--On state uses the primary background; off uses the input track.
disabled--Disabled switches cannot be interacted with.