Switch
A control that allows the user to toggle between checked and not checked.
Loading…
"use client"
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
export default function SwitchDemo() {
return (
<div className="flex items-center gap-2">
<Switch id="airplane-mode" />
<Label htmlFor="airplane-mode">Airplane Mode</Label>
</div>
)
}Installation
pnpm dlx shadcn@latest add @kickresume/switchUsage
import { Switch } from "@/components/ui/switch"<div className="flex items-center gap-2">
<Switch id="airplane-mode" />
<Label htmlFor="airplane-mode">Airplane Mode</Label>
</div>Examples
Description
Loading…
"use client"
import {
Field,
FieldContent,
FieldDescription,
FieldLabel,
} from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
export default function SwitchDescription() {
return (
<Field orientation="horizontal" className="max-w-sm">
<FieldContent>
<FieldLabel htmlFor="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.
Loading…
"use client"
import {
Field,
FieldContent,
FieldDescription,
FieldGroup,
FieldLabel,
FieldTitle,
} from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
export default function SwitchChoiceCard() {
return (
<FieldGroup className="w-full max-w-sm">
<FieldLabel htmlFor="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 htmlFor="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 the Switch component to disable the switch. Add the data-disabled prop to the Field component for styling.
Loading…
"use client"
import { Field, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
export default function SwitchDisabled() {
return (
<Field orientation="horizontal" data-disabled className="w-fit">
<Switch id="switch-disabled-unchecked" disabled />
<FieldLabel htmlFor="switch-disabled-unchecked">Disabled</FieldLabel>
</Field>
)
}Invalid
Add the aria-invalid prop to the Switch component to indicate an invalid state. Add the data-invalid prop to the Field component for styling.
Loading…
"use client"
import {
Field,
FieldContent,
FieldDescription,
FieldLabel,
} from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
export default function SwitchInvalid() {
return (
<Field orientation="horizontal" className="max-w-sm" data-invalid>
<FieldContent>
<FieldLabel htmlFor="switch-terms">
Accept terms and conditions
</FieldLabel>
<FieldDescription id="switch-terms-description">
You must accept the terms and conditions to continue.
</FieldDescription>
</FieldContent>
<Switch
id="switch-terms"
aria-describedby="switch-terms-description"
aria-invalid
/>
</Field>
)
}Size
Use the size prop to change the size of the switch.
Loading…
"use client"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
export default function SwitchSizes() {
return (
<FieldGroup className="w-full max-w-[10rem]">
<Field orientation="horizontal">
<Switch id="switch-size-sm" size="sm" />
<FieldLabel htmlFor="switch-size-sm">Small</FieldLabel>
</Field>
<Field orientation="horizontal">
<Switch id="switch-size-default" size="default" />
<FieldLabel htmlFor="switch-size-default">Default</FieldLabel>
</Field>
</FieldGroup>
)
}API Reference
See the Base UI Switch documentation.