Dokumentation (english)
Components: Shadcn UI

Switch

Understand the design system and contribute to it

Component preview

The live preview for UI components is handled by components/atoms/codedocs/ComponentPreview.tsx. Use it directly inside MDX whenever you want a playground-style embed. Provide componentProps to set default values and propOptions to surface toggle-able props next to the preview.

Preview

Prop reference

Auto-generated from the component source

Switch

  • className

React.ComponentProps<typeof SwitchPrimitive.Root>

Code
// owner: Sona
"use client";

import * as React from "react";

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

export default function SwitchExample() {
  const [settings, setSettings] = React.useState({
    alerts: true,
    marketing: false,
  });

  return (
    <div className="space-y-4">
      <label className="flex items-center justify-between gap-4 text-sm">
        <div>
          <p className="font-medium">Incident alerts</p>
          <p className="text-muted-foreground">
            Send push notifications for live incidents.
          </p>
        </div>
        <Switch
          checked={settings.alerts}
          onCheckedChange={(value) =>
            setSettings((prev) => ({ ...prev, alerts: Boolean(value) }))
          }
        />
      </label>
      <label className="flex items-center justify-between gap-4 text-sm">
        <div>
          <p className="font-medium">Marketing updates</p>
          <p className="text-muted-foreground">
            Occasional product announcements.
          </p>
        </div>
        <Switch
          checked={settings.marketing}
          onCheckedChange={(value) =>
            setSettings((prev) => ({ ...prev, marketing: Boolean(value) }))
          }
        />
      </label>
    </div>
  );
}

On this page


Command Palette

Search for a command to run...

Schnellzugriffe
STRG + KSuche
STRG + DNachtmodus / Tagmodus
STRG + LSprache ändern

Software-Details
Kompiliert vor 15 Tagen
Release: v4.0.0-production
Buildnummer: master@6fbd7b5
Historie: 13 Items