Switch

On/off switch on Base UI with a pill track and sliding thumb, in default and small sizes. Shows focus rings and invalid styling, and inverts its thumb colors in dark mode.

Open the preview in a new tab
"use client"

import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"

/** Switch, on, with its Label. */
export default function SwitchDemo() {
  return (
    <div className="flex items-center gap-3">
      <Switch id="grain" defaultChecked />
      <Label htmlFor="grain">Film grain</Label>
    </div>
  )
}

Installation

pnpm dlx shadcn add @01orbital/switch
npx shadcn add @01orbital/switch
yarn shadcn add @01orbital/switch
bunx --bun shadcn add @01orbital/switch

Add the @01orbital registry to components.json once (install guide), then run the command.

It lands at components/ui/switch.tsx, as source you own and edit.

Examples

01

switch-cloud-dancer

Example 01 · switch-cloud-dancer

Cloud Dancer

The Cloud Dancer Switch: a pale pill track, teal-shaded and warming to rose when on, with a dark orb thumb, styled through the stock Switch's slots.

pnpm dlx shadcn add @01orbital/switch-cloud-dancer
npx shadcn add @01orbital/switch-cloud-dancer
yarn shadcn add @01orbital/switch-cloud-dancer
bunx --bun shadcn add @01orbital/switch-cloud-dancer
"use client"

import { useId } from "react"

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

// The Cloud Dancer pill as the track, teal-shaded, warming to rose when on,
// with a dark orb for the thumb. The thumb is styled through its slot.
export default function SwitchCloudDancer() {
  const id = useId()
  return (
    <FieldGroup className="w-64 gap-4">
      <Field orientation="horizontal">
        <Switch
          id={`${id}-weekly`}
          name="weekly"
          defaultChecked
          className="border border-transparent shadow-[inset_-2px_-3px_5px_rgb(var(--s-main)/0.28),inset_-1px_-1px_3px_rgb(var(--s-main)/0.15),inset_1.5px_2px_3px_rgb(255_255_255/0.85),inset_1px_1px_2px_rgb(255_255_255/0.60),inset_-3px_-3px_6px_rgb(var(--s-accent)/0.10),0_1.5px_4px_rgb(var(--s-main)/0.18),0_1px_2px_rgb(0_0_0/0.06)] ring-1 ring-foreground/10 transition-[box-shadow] duration-200 ease-out [--s-accent:184_92_76] [--s-main:90_144_144] focus-visible:ring-ring/50 data-[size=default]:h-[18.4px] data-[size=default]:w-8 data-[size=sm]:h-3.5 data-[size=sm]:w-6 **:data-[slot=switch-thumb]:size-4! **:data-[slot=switch-thumb]:bg-[radial-gradient(circle_at_40%_38%,oklch(0.28_0.008_60),oklch(0.16_0.006_60))]! **:data-[slot=switch-thumb]:shadow-[inset_1.5px_2px_4px_rgb(255_255_255/0.07),inset_-1px_-1.5px_4px_rgb(0_0_0/0.45),0_2px_5px_rgb(0_0_0/0.14),0_1px_2px_rgb(0_0_0/0.08)] **:data-[slot=switch-thumb]:duration-200 **:data-[slot=switch-thumb]:ease-out dark:ring-foreground/25 data-checked:border-transparent data-checked:bg-[radial-gradient(circle_at_35%_35%,#ffffff_0%,oklch(0.995_0.002_90)_40%,oklch(0.915_0.006_85)_100%)] data-checked:[--s-accent:90_144_144] data-checked:[--s-main:196_112_112] **:data-[slot=switch-thumb]:data-checked:translate-x-[calc(100%-2px)]! data-unchecked:bg-[radial-gradient(circle_at_35%_35%,#ffffff_0%,oklch(0.995_0.002_90)_40%,oklch(0.915_0.006_85)_100%)]"
        />
        <FieldLabel htmlFor={`${id}-weekly`} className="font-normal">
          Weekly summary
        </FieldLabel>
      </Field>
      <Field orientation="horizontal">
        <Switch
          id={`${id}-mentions`}
          name="mentions"
          className="border border-transparent shadow-[inset_-2px_-3px_5px_rgb(var(--s-main)/0.28),inset_-1px_-1px_3px_rgb(var(--s-main)/0.15),inset_1.5px_2px_3px_rgb(255_255_255/0.85),inset_1px_1px_2px_rgb(255_255_255/0.60),inset_-3px_-3px_6px_rgb(var(--s-accent)/0.10),0_1.5px_4px_rgb(var(--s-main)/0.18),0_1px_2px_rgb(0_0_0/0.06)] ring-1 ring-foreground/10 transition-[box-shadow] duration-200 ease-out [--s-accent:184_92_76] [--s-main:90_144_144] focus-visible:ring-ring/50 data-[size=default]:h-[18.4px] data-[size=default]:w-8 data-[size=sm]:h-3.5 data-[size=sm]:w-6 **:data-[slot=switch-thumb]:size-4! **:data-[slot=switch-thumb]:bg-[radial-gradient(circle_at_40%_38%,oklch(0.28_0.008_60),oklch(0.16_0.006_60))]! **:data-[slot=switch-thumb]:shadow-[inset_1.5px_2px_4px_rgb(255_255_255/0.07),inset_-1px_-1.5px_4px_rgb(0_0_0/0.45),0_2px_5px_rgb(0_0_0/0.14),0_1px_2px_rgb(0_0_0/0.08)] **:data-[slot=switch-thumb]:duration-200 **:data-[slot=switch-thumb]:ease-out dark:ring-foreground/25 data-checked:border-transparent data-checked:bg-[radial-gradient(circle_at_35%_35%,#ffffff_0%,oklch(0.995_0.002_90)_40%,oklch(0.915_0.006_85)_100%)] data-checked:[--s-accent:90_144_144] data-checked:[--s-main:196_112_112] **:data-[slot=switch-thumb]:data-checked:translate-x-[calc(100%-2px)]! data-unchecked:bg-[radial-gradient(circle_at_35%_35%,#ffffff_0%,oklch(0.995_0.002_90)_40%,oklch(0.915_0.006_85)_100%)]"
        />
        <FieldLabel htmlFor={`${id}-mentions`} className="font-normal">
          Mention alerts
        </FieldLabel>
      </Field>
    </FieldGroup>
  )
}

Dependencies

npm: @base-ui/react, cn. The CLI installs them for you.

No other registry pieces.