Switch
Web/MobileA control that allows the user to toggle between checked and not checked.
Installation
pnpm dlx cubix-ui@latest add switchUsage
import { Label } from "@/components/cubix/label"
import { Switch } from "@/components/cubix/switch"
<div className="flex items-center gap-2">
<Switch id="airplane-mode" />
<Label htmlFor="airplane-mode">حالت هواپیما</Label>
</div>
Composition
Switch is the control only. Pair it with Label using matching id and htmlFor. The thumb moves toward the inline end when on, so it follows the page direction.
Switch
Label (htmlFor → Switch id)Examples
States
On, off, and disabled cover the common conditions.
Sizes
Use size to pick sm or default.
With description
Pair a switch with a title and helper line for settings rows.
Controlled
Pass checked and onCheckedChange to own the state from outside.
وضعیت: خاموش
Invalid
Set aria-invalid when the choice is required and missing. The switch itself stays visually unchanged; show the error in helper text.
برای ادامه باید این گزینه را روشن کنید.
Custom styling
Switch accepts a className merged with the shipped cn helper. Target both data-checked / data-unchecked (Base UI and React Aria) and data-[state=checked] / data-[state=unchecked] (Radix) so one class list works on every base.
API Reference
Note: Parts render data-slot attributes (switch, switch-thumb) for targeting in tests and parent selectors. Use the same props on Base UI, React Aria, and Radix.
Switch
The toggle control. Pair it with Cubix Label via matching id and htmlFor.
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | boolean | false | Controlled checked state of the switch. Use with onCheckedChange. |
| defaultChecked | boolean | false | Default 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. sm is 28x16px, default is 36x20px. |
| disabled | boolean | false | Disables the switch. |
| name | string | - | The name attribute passed when submitting a form. |
| value | string | on | The value attribute passed when submitting a form. |
| aria-invalid | boolean | false | When true, marks the switch invalid for assistive technology. The switch itself looks unchanged; show the error in helper text. |
| className | string | - | Additional Tailwind classes merged with the component styles (last one wins). |
States
| Prop | Type | Default | Description |
|---|---|---|---|
| on | - | - | The track uses the primary color and the thumb sits at the inline end. |
| off | - | - | The track uses the input color and the thumb sits at the inline start. |
| disabled | - | - | Disabled switches cannot be interacted with and are dimmed. |
| invalid | - | - | Exposed to assistive technology only. Show the error message in helper text. |