Components
- Accordion
- Alert
- Alert Dialog
- Autocomplete
- Avatar
- Badge
- Breadcrumb
- Button
- Card
- Checkbox
- Checkbox Group
- Collapsible
- Combobox
- Dialog
- Field
- Fieldset
- Form
- Frame
- Group
- Input
- Label
- Menu
- Meter
- Number Field
- Pagination
- Popover
- Preview Card
- Progress
- Radio Group
- Scroll Area
- Select
- Separator
- Sheet
- Slider
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Toolbar
- Tooltip
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 function SwitchWithLabel() {
return (
<Label>
<Switch />
Marketing emails
</Label>
)
}
Installation
pnpm dlx shadcn@latest add https://coss.com/ui/r/switch.json
Usage
import { Switch } from "@/components/ui/switch"
<Switch />
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 function SwitchWithLabel() {
return (
<Label>
<Switch disabled />
Marketing emails
</Label>
)
}
With Description
By enabling marketing emails, you agree to receive emails.
import * as React from "react"
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
export function SwitchWithDescriptionDemo() {
const id = React.useId()
return (
<div className="flex items-start gap-2">
<Switch id={id} defaultChecked />
<div className="flex flex-col gap-1">
<Label htmlFor={id}>Marketing emails</Label>
<p className="text-xs text-muted-foreground">
By enabling marketing emails, you agree to receive emails.
</p>
</div>
</div>
)
}
Card Style
import * as React from "react"
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
export function SwitchCardDemo() {
const id = React.useId()
return (
<Label
htmlFor={id}
className="flex items-center gap-6 rounded-lg border p-3 hover:bg-accent/50 has-data-checked:border-primary/48 has-data-checked:bg-accent/50"
>
<div className="flex flex-col gap-1">
<p className="text-sm leading-4">Enable notifications</p>
<p className="text-xs text-muted-foreground">
You can enable or disable notifications at any time.
</p>
</div>
<Switch id={id} defaultChecked />
</Label>
)
}
Form Integration
"use client"
import * as React from "react"
import { Button } from "@/components/ui/button"
import { Field, FieldLabel } from "@/components/ui/field"
import { Form } from "@/components/ui/form"
import { Switch } from "@/components/ui/switch"
export function SwitchFormDemo() {
const [loading, setLoading] = React.useState(false)
const onSubmit = async (e: React.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 onSubmit={onSubmit} className="w-auto">
<Field name="marketing">
<FieldLabel>
<Switch name="marketing" defaultChecked disabled={loading} />
Enable marketing emails
</FieldLabel>
</Field>
<Button type="submit" disabled={loading}>
Submit
</Button>
</Form>
)
}
Comparing with Radix / shadcn
If you’re already familiar with Radix UI and shadcn/ui, this guide highlights the small differences and similarities so you can get started with coss.com ui quickly.
Quick Checklist
- If you used
asChild
on parts, switch to therender
prop