Skip to main content
The Switch component provides a toggle control for binary on/off states with multiple visual variants and sizes.

Installation

npx shadcn@latest add @eo-n/switch

Usage

import { Switch } from "@/components/ui/switch";

export default function Example() {
  return <Switch />;
}

Examples

Default

<Switch />

With Label

import { Label } from "@/components/ui/label";

<div className="flex items-center space-x-2">
  <Switch id="airplane-mode" />
  <Label htmlFor="airplane-mode">Airplane Mode</Label>
</div>

Controlled

import { useState } from "react";

function ControlledSwitch() {
  const [checked, setChecked] = useState(false);

  return (
    <div className="flex items-center space-x-2">
      <Switch
        id="notifications"
        checked={checked}
        onCheckedChange={setChecked}
      />
      <Label htmlFor="notifications">
        {checked ? "Notifications enabled" : "Notifications disabled"}
      </Label>
    </div>
  );
}

Disabled

<Switch disabled />
<Switch disabled checked />

Square Variant

<Switch variant="square" />

Sizes

<Switch size="sm" />

Square with Sizes

<Switch variant="square" size="sm" />
<Switch variant="square" size="default" />
<Switch variant="square" size="lg" />

In a Form

function FormExample() {
  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault();
    const formData = new FormData(e.target as HTMLFormElement);
    console.log(formData.get('marketing'));
  };

  return (
    <form onSubmit={handleSubmit}>
      <div className="flex items-center space-x-2">
        <Switch id="marketing" name="marketing" />
        <Label htmlFor="marketing">Marketing emails</Label>
      </div>
      <button type="submit">Submit</button>
    </form>
  );
}

API Reference

Switch

Extends all props from @base-ui/react Switch.Root component.
variant
string
default:"default"
The visual style of the switch.Options: default | square
size
string
default:"default"
The size of the switch.Options: sm | default | lg
checked
boolean
The controlled checked state.
defaultChecked
boolean
The default checked state when uncontrolled.
onCheckedChange
function
Callback fired when the checked state changes.
(checked: boolean) => void
disabled
boolean
Whether the switch is disabled.
required
boolean
Whether the switch is required in a form.
name
string
The name attribute for form submission.
value
string
The value attribute for form submission.
className
string
Additional CSS classes to apply.

TypeScript

import { Switch as SwitchPrimitive } from "@base-ui/react";
import { VariantProps } from "class-variance-authority";

type SwitchVariants = VariantProps<typeof switchVariants>;

interface SwitchProps
  extends React.ComponentProps<typeof SwitchPrimitive.Root>,
    SwitchVariants {}

Styling Features

  • Smooth animations with 150ms transition
  • Focus visible indicator
  • Disabled state styling
  • Dark mode support
  • Customizable variants (rounded/square)
  • Multiple size options
  • Shadow effects for depth

Accessibility

  • Fully keyboard accessible (Space to toggle)
  • Proper ARIA attributes
  • Focus visible indicator
  • Works with form labels
  • Screen reader support

Build docs developers (and LLMs) love