Components
- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Checkbox Group
- Collapsible
- Combobox
- Command
- Context Menu
- Date Picker
- Dialog
- Drawer
- Dropdown Menu
- Empty
- Field
- Fieldset
- Form
- Frame
- Group
- Input
- Input Group
- Kbd
- Label
- Menu
- Menubar
- Message
- Meter
- Number Field
- OTP Field
- Pagination
- Popover
- Preview Card
- Progress
- Questionnaire
- Radio Group
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toggle
- Toggle Group
- Toolbar
- Tooltip
Shared
- Artifact
- Audio Player
- Canvas
- Chain of Thought
- Checkpoint
- Code Block
- Commit
- Connection
- Context
- Controls
- Edge
- Environment Variables
- Mic Selector
- Model Selector
- Node
- Node Toolbar
- Open in Chat
- Panel
- Plan
- Prompt Input
- Queue
- Reasoning
- Sandbox
- Schema Display
- Shimmer
- Snippet
- Sources
- Speech Input
- Stack Trace
- Suggestion
- Tool
- Transcription
- Web Preview
Hooks
Resources
Switch
A control that indicates whether a setting is on or off.
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
export default function Particle() {
return (
<Label>
<Switch />
Marketing emails
</Label>
);
}
Installation
pnpm dlx shadcn@latest add @frontal-labs/switch
Usage
import { Switch } from "@/components/ui/switch";<Switch />API Reference
Switch
Styled wrapper for Switch.Root from Base UI with built-in thumb.
| Prop | Type | Default | Description |
|---|---|---|---|
size | "sm" | "default" | "default" | Controls the track and thumb size |
All other props from Switch.Root are supported.
The checked track uses the brand colour (bg-brand) with a bg-brand-foreground thumb, so an enabled switch reads as a brand affordance rather than a neutral primary fill. The unchecked track uses bg-input. The root carries cursor-pointer, and cursor-not-allowed when disabled.
Examples
For accessible labelling and validation, prefer using the Field component to wrap checkboxes. See the related example: Switch field.
Disabled
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
export default function Particle() {
return (
<Label>
<Switch disabled />
Marketing emails
</Label>
);
}
With Description
By enabling marketing emails, you agree to receive emails.
import { useId } from "react";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
export default function Particle() {
const id = useId();
return (
<div className="flex items-start gap-2">
<Switch defaultChecked id={id} />
<div className="flex flex-col gap-1">
<Label htmlFor={id}>Marketing emails</Label>
<p className="text-muted-foreground text-xs">
By enabling marketing emails, you agree to receive emails.
</p>
</div>
</div>
);
}
Small Size
Use the size prop to render a smaller switch. Track and thumb scale together.
<Switch size="sm" />Card Style
import { useId } from "react";
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";
export default function Particle() {
const id = useId();
return (
<Label
className="flex items-center gap-6 rounded-md border p-3 hover:bg-accent/50 has-data-checked:border-primary/48 has-data-checked:bg-accent/50"
htmlFor={id}
>
<div className="flex flex-col gap-1">
<p>Enable notifications</p>
<p className="text-muted-foreground text-xs">
You can enable or disable notifications at any time.
</p>
</div>
<Switch
className="[--thumb-size:--spacing(4)] sm:[--thumb-size:--spacing(3)]"
defaultChecked
id={id}
/>
</Label>
);
}
Form Integration
"use client";
import type { FormEvent } from "react";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Field, FieldLabel } from "@/components/ui/field";
import { Spinner } from "@/components/ui/spinner";
import { Switch } from "@/components/ui/switch";
export default function Particle() {
const [loading, setLoading] = useState(false);
const onSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
setLoading(true);
await new Promise((r) => setTimeout(r, 800));
setLoading(false);
console.log(formData.get("marketing"));
const enabled = formData.get("marketing");
alert(`Marketing emails: ${enabled}`);
};
return (
<form className="flex flex-col gap-4" onSubmit={onSubmit}>
<Field>
<FieldLabel>
<Switch defaultChecked name="marketing" />
Enable marketing emails
</FieldLabel>
</Field>
<Button disabled={loading} type="submit">
{loading && <Spinner />}
Submit
</Button>
</form>
);
}