Switch
A control that allows the user to toggle between checked and not checked.
import { Label } from "~/components/ui/label";import { Switch } from "~/components/ui/switch";
export default function SwitchDemo() { return ( <div class="flex items-center gap-3"> <Switch id="airplane-mode" /> <Label for="airplane-mode">Airplane Mode</Label> </div> );}Installation
Usage
import { Label } from "~/components/ui/label";import { Switch } from "~/components/ui/switch";<div class="flex items-center gap-3"> <Switch id="airplane-mode" /> <Label for="airplane-mode">Airplane Mode</Label></div>Description
Focus is shared across devices, and turns off when you leave the app.
import { Field, FieldContent, FieldDescription, FieldLabel } from "~/components/ui/field";import { Switch } from "~/components/ui/switch";
export default function SwitchDescription() { return ( <Field orientation="horizontal" class="max-w-sm"> <FieldContent> <FieldLabel for="switch-focus-mode">Share across devices</FieldLabel> <FieldDescription> Focus is shared across devices, and turns off when you leave the app. </FieldDescription> </FieldContent> <Switch id="switch-focus-mode" /> </Field> );}Choice Card
Card-style selection where FieldLabel wraps the entire Field for a clickable card pattern.
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel, FieldTitle,} from "~/components/ui/field";import { Switch } from "~/components/ui/switch";
export default function SwitchChoiceCard() { return ( <FieldGroup class="w-full max-w-sm"> <FieldLabel for="switch-share"> <Field orientation="horizontal"> <FieldContent> <FieldTitle>Share across devices</FieldTitle> <FieldDescription> Focus is shared across devices, and turns off when you leave the app. </FieldDescription> </FieldContent> <Switch id="switch-share" /> </Field> </FieldLabel> <FieldLabel for="switch-notifications"> <Field orientation="horizontal"> <FieldContent> <FieldTitle>Enable notifications</FieldTitle> <FieldDescription> Receive notifications when focus mode is enabled or disabled. </FieldDescription> </FieldContent> <Switch id="switch-notifications" defaultChecked /> </Field> </FieldLabel> </FieldGroup> );}Disabled
Add the disabled prop to Switch to disable it. Add data-disabled to Field for disabled styling.
import { Field, FieldLabel } from "~/components/ui/field";import { Switch } from "~/components/ui/switch";
export default function SwitchDisabled() { return ( <Field orientation="horizontal" data-disabled class="w-fit"> <Switch id="switch-disabled-unchecked" disabled /> <FieldLabel for="switch-disabled-unchecked">Disabled</FieldLabel> </Field> );}Invalid
Add aria-invalid to Switch to indicate an invalid state. Add data-invalid to Field for styling.
You must accept the terms and conditions to continue.
import { Field, FieldContent, FieldDescription, FieldLabel } from "~/components/ui/field";import { Switch } from "~/components/ui/switch";
export default function SwitchInvalid() { return ( <Field orientation="horizontal" class="max-w-sm" data-invalid> <FieldContent> <FieldLabel for="switch-terms">Accept terms and conditions</FieldLabel> <FieldDescription>You must accept the terms and conditions to continue.</FieldDescription> </FieldContent> <Switch id="switch-terms" aria-invalid /> </Field> );}Size
Use the size prop to change the size of the switch.
import { Field, FieldGroup, FieldLabel } from "~/components/ui/field";import { Switch } from "~/components/ui/switch";
export default function SwitchSizes() { return ( <FieldGroup class="w-full max-w-40"> <Field orientation="horizontal"> <Switch id="switch-size-sm" size="sm" /> <FieldLabel for="switch-size-sm">Small</FieldLabel> </Field> <Field orientation="horizontal"> <Switch id="switch-size-default" size="default" /> <FieldLabel for="switch-size-default">Default</FieldLabel> </Field> </FieldGroup> );}API Reference
Switch wraps @kobalte/core/switch, which implements the WAI-ARIA switch role, including keyboard, disabled, and form behavior. See the Kobalte API reference for the full set of props, such as checked, defaultChecked, onChange, disabled, required, readOnly, name, and value.