← Components

Selection and input · Material 3: Switch

Switch

Turns a setting on or off at once. The thumb stretches toward its side, then snaps round.

Examples and variants

Basic

Hold the thumb to see it squash wider.

Example
function SwitchBasic() {
  return (
    <div className="flex flex-col gap-3">
      <Label className="flex items-center gap-3">
        <Switch defaultChecked />
        Quiet chrome
      </Label>
      <Label className="flex items-center gap-3">
        <Switch />
        Notifications
      </Label>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/switch.json
npx shadcn@latest add https://isotopi.com/r/label.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install @base-ui/react cn
  2. Copy these 2 files to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/switch.tsxswitch
      components/ui/switch.tsx
      "use client"
      
      import { Switch as SwitchPrimitive } from "@base-ui/react/switch"
      import { cn } from "cn"
      
      function Switch({
        className,
        size = "default",
        ...props
      }: SwitchPrimitive.Root.Props & {
        size?: "sm" | "default"
      }) {
        return (
          <SwitchPrimitive.Root
            data-slot="switch"
            data-size={size}
            className={cn(
              "hit-target group/switch relative inline-flex shrink-0 items-center rounded-(--switch-corner) border-0 p-inline-xs outline-none",
              "transition-colors duration-(--duration-fast) ease-(--ease-out)",
              "focus-visible:shadow-(--focus-ring)",
              "data-unchecked:bg-(--switch-track) data-checked:bg-(--switch-track-checked) data-unchecked:hover:bg-(--switch-track-hover) data-checked:hover:bg-(--switch-track-checked-hover)",
              "data-disabled:cursor-not-allowed data-disabled:data-unchecked:bg-(--switch-track-disabled) data-disabled:data-checked:bg-(--switch-track-checked-disabled)",
              "data-[size=default]:h-(--size-switch-h) data-[size=default]:w-(--size-switch-w)",
              "data-[size=sm]:h-icon-md data-[size=sm]:w-[calc(var(--size-icon-md)*2)]",
              className
            )}
            {...props}
          >
            <SwitchPrimitive.Thumb
              data-slot="switch-thumb"
              className={cn(
                "switch-thumb pointer-events-none block rounded-(--radius-pill) shadow-(--switch-thumb-elevation)",
                "data-unchecked:bg-(--color-fg-default) data-checked:bg-(--color-fg-on-accent)",
                "data-checked:translate-x-(--size-icon-md) data-[size=sm]:data-checked:translate-x-(--size-icon-md)",
                "group-data-[size=default]/switch:size-[calc(var(--size-switch-h)-var(--space-gap-inline-xs)*2)]",
                "group-data-[size=sm]/switch:size-[calc(var(--size-icon-md)-var(--space-gap-inline-xs))]",
                "dark:data-checked:bg-primary-foreground"
              )}
            />
          </SwitchPrimitive.Root>
        )
      }
      
      export { Switch }
      
    • components/ui/label.tsxlabel
      components/ui/label.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      
      function Label({ className, ...props }: React.ComponentProps<"label">) {
        return (
          <label
            data-slot="label"
            className={cn(
              "flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:text-(--field-label-disabled) group-data-[invalid=true]/field:text-(--color-danger-fg) peer-disabled:cursor-not-allowed peer-disabled:text-(--field-label-disabled)",
              className
            )}
            {...props}
          />
        )
      }
      
      export { Label }
      

Imports

What the examples on this page import:

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

Anatomy

  1. 1Track · --switch-width x --switch-height, --switch-track; on: --switch-track-checked.
  2. 2Thumb · --switch-thumb with --switch-thumb-elevation.

Props

PropTypeDefaultDescription
checked / defaultCheckedboolean—On state.
onCheckedChange(checked: boolean) => void—Called on change.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--switch-widthvar(--size-switch-w)36pxsame
--switch-heightvar(--size-switch-h)20pxsame
--switch-trackvar(--surface-container-high)oklch(0.94 0.007 95)oklch(0.27 0 0)
--switch-track-checkedvar(--color-brand)oklch(0.18 0.008 260)oklch(0.98 0 0)
--switch-track-hovercolor-mix(in oklch, var(--switch-track), var(--on-surface) var(--state-hover-mix))color-mix(in oklch, var(--switch-track), var(--on-surface) var(--state-hover-mix))same
--switch-track-checked-hovervar(--color-brand-hover)oklch(0.28 0.008 260)oklch(0.9 0 0)
--switch-track-disabledvar(--color-bg-disabled)color-mix(in oklch, var(--color-fg-default) var(--state-disabled-container-mix), transparent)same
--switch-track-checked-disabledcolor-mix(in oklch, var(--color-fg-disabled) 55%, transparent)color-mix(in oklch, var(--color-fg-disabled) 55%, transparent)same
--switch-thumbvar(--surface-container-lowest)oklch(1 0 0)oklch(0.14 0 0)
--switch-thumb-elevationvar(--elevation-1)0 1px 1px -0.5px oklch(0.22 0.01 260 / 7%), 0 2px 3px -1px oklch(0.22 0.01 260 / 10%), 0 4px 8px -4px oklch(0.22 0.01 260 / 6%)0 1px 2px oklch(0 0 0 / 35%), 0 2px 10px oklch(0 0 0 / 25%)
--switch-cornervar(--shape-full)999pxsame

Motion

TriggerWhat happensTokens
ToggleThumb stretches toward the other side, then snaps round.--spring-snappy

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • role=switch with aria-checked.
  • Space toggles.
  • Label says what turns on, not 'On/Off'.

Usage

Do

  • Use for settings that apply immediately.

Don’t

  • Don't use inside a form that needs a Save button (use Checkbox).

Code

components/ui/switch.tsx
"use client"

import { Switch as SwitchPrimitive } from "@base-ui/react/switch"
import { cn } from "cn"

function Switch({
  className,
  size = "default",
  ...props
}: SwitchPrimitive.Root.Props & {
  size?: "sm" | "default"
}) {
  return (
    <SwitchPrimitive.Root
      data-slot="switch"
      data-size={size}
      className={cn(
        "hit-target group/switch relative inline-flex shrink-0 items-center rounded-(--switch-corner) border-0 p-inline-xs outline-none",
        "transition-colors duration-(--duration-fast) ease-(--ease-out)",
        "focus-visible:shadow-(--focus-ring)",
        "data-unchecked:bg-(--switch-track) data-checked:bg-(--switch-track-checked) data-unchecked:hover:bg-(--switch-track-hover) data-checked:hover:bg-(--switch-track-checked-hover)",
        "data-disabled:cursor-not-allowed data-disabled:data-unchecked:bg-(--switch-track-disabled) data-disabled:data-checked:bg-(--switch-track-checked-disabled)",
        "data-[size=default]:h-(--size-switch-h) data-[size=default]:w-(--size-switch-w)",
        "data-[size=sm]:h-icon-md data-[size=sm]:w-[calc(var(--size-icon-md)*2)]",
        className
      )}
      {...props}
    >
      <SwitchPrimitive.Thumb
        data-slot="switch-thumb"
        className={cn(
          "switch-thumb pointer-events-none block rounded-(--radius-pill) shadow-(--switch-thumb-elevation)",
          "data-unchecked:bg-(--color-fg-default) data-checked:bg-(--color-fg-on-accent)",
          "data-checked:translate-x-(--size-icon-md) data-[size=sm]:data-checked:translate-x-(--size-icon-md)",
          "group-data-[size=default]/switch:size-[calc(var(--size-switch-h)-var(--space-gap-inline-xs)*2)]",
          "group-data-[size=sm]/switch:size-[calc(var(--size-icon-md)-var(--space-gap-inline-xs))]",
          "dark:data-checked:bg-primary-foreground"
        )}
      />
    </SwitchPrimitive.Root>
  )
}

export { Switch }