Embossed UI

Search documentation

Search for a page or component.

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 function SwitchDemo() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <div className="flex items-center gap-2">
        <Switch id="sw-default" />
        <Label htmlFor="sw-default">Default</Label>
      </div>
      <div className="flex items-center gap-2">
        <Switch id="sw-on" defaultChecked />
        <Label htmlFor="sw-on">On</Label>
      </div>
      <div className="flex items-center gap-2">
        <Switch id="sw-sm" size="sm" defaultChecked />
        <Label htmlFor="sw-sm">Small</Label>
      </div>
      <div className="flex items-center gap-2">
        <Switch id="sw-disabled" disabled />
        <Label htmlFor="sw-disabled">Disabled</Label>
      </div>
    </div>
  )
}

Installation

pnpm dlx shadcn@latest add https://ui.ikramhasan.com/r/switch.json

Usage

import { Switch } from "@/components/ui/switch"
<Switch />

Examples

Settings

Update products every hour.

Let Marketer read and update your storefront theme.

import {
  Field,
  FieldContent,
  FieldDescription,
  FieldGroup,
  FieldLabel,
} from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"

export function SwitchSettings() {
  return (
    <FieldGroup className="max-w-sm">
      <Field orientation="horizontal">
        <FieldContent>
          <FieldLabel htmlFor="sw-sync">Data sync</FieldLabel>
          <FieldDescription>Update products every hour.</FieldDescription>
        </FieldContent>
        <Switch id="sw-sync" defaultChecked />
      </Field>
      <Field orientation="horizontal">
        <FieldContent>
          <FieldLabel htmlFor="sw-theme">Edit theme</FieldLabel>
          <FieldDescription>
            Let Marketer read and update your storefront theme.
          </FieldDescription>
        </FieldContent>
        <Switch id="sw-theme" />
      </Field>
    </FieldGroup>
  )
}

API Reference

Switch is built on the Base UI Switch primitive. See the Base UI documentation for every prop.