Switch
switch-demo.tsx
1import { Label } from "@/components/ui/label"2import { Switch } from "@/components/ui/switch"34export function SwitchDemo() {5return (6<div className="flex items-center space-x-2">7<Switch id="airplane-mode" />8<Label htmlFor="airplane-mode">Airplane Mode</Label>9</div>10)11}
Installation
pnpm dlx shadcn@latest add @ui-sdk/switchUsage
switch-usage.tsx
1import { Switch } from "@/components/ui/switch"23<Switch aria-label="Airplane Mode" />
Examples
Description
Focus is shared across devices, and turns off when you leave the app.
switch-description.tsx
1import {2Field,3FieldContent,4FieldDescription,5FieldLabel,6} from "@/components/ui/field"7import { Switch } from "@/components/ui/switch"89export function SwitchDescription() {10return (11<Field orientation="horizontal" className="max-w-sm">12<FieldContent>13<FieldLabel htmlFor="switch-focus-mode">14Share across devices15</FieldLabel>16<FieldDescription>17Focus is shared across devices, and turns off when you leave the app.18</FieldDescription>19</FieldContent>20<Switch id="switch-focus-mode" />21</Field>22)23}
Choice Card
switch-choice-card.tsx
1import {2Field,3FieldContent,4FieldDescription,5FieldGroup,6FieldLabel,7FieldTitle,8} from "@/components/ui/field"9import { Switch } from "@/components/ui/switch"1011export function SwitchChoiceCard() {12return (13<FieldGroup className="w-full max-w-sm">14<FieldLabel htmlFor="switch-share">15<Field orientation="horizontal">16<FieldContent>17<FieldTitle>Share across devices</FieldTitle>18<FieldDescription>19Focus is shared across devices, and turns off when you leave the20app.21</FieldDescription>22</FieldContent>23<Switch id="switch-share" />24</Field>25</FieldLabel>26<FieldLabel htmlFor="switch-notifications">27<Field orientation="horizontal">28<FieldContent>29<FieldTitle>Enable notifications</FieldTitle>30<FieldDescription>31Receive notifications when focus mode is enabled or disabled.32</FieldDescription>33</FieldContent>34<Switch id="switch-notifications" defaultChecked />35</Field>36</FieldLabel>37</FieldGroup>38)39}
Disabled
switch-disabled.tsx
1import { Field, FieldLabel } from "@/components/ui/field"2import { Switch } from "@/components/ui/switch"34export function SwitchDisabled() {5return (6<Field orientation="horizontal" data-disabled className="w-fit">7<Switch id="switch-disabled-unchecked" disabled />8<FieldLabel htmlFor="switch-disabled-unchecked">Disabled</FieldLabel>9</Field>10)11}
Invalid
You must accept the terms and conditions to continue.
switch-invalid.tsx
1import {2Field,3FieldContent,4FieldDescription,5FieldLabel,6} from "@/components/ui/field"7import { Switch } from "@/components/ui/switch"89export function SwitchInvalid() {10return (11<Field orientation="horizontal" className="max-w-sm" data-invalid>12<FieldContent>13<FieldLabel htmlFor="switch-terms">14Accept terms and conditions15</FieldLabel>16<FieldDescription>17You must accept the terms and conditions to continue.18</FieldDescription>19</FieldContent>20<Switch id="switch-terms" aria-invalid />21</Field>22)23}
Size
switch-sizes.tsx
1import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"2import { Switch } from "@/components/ui/switch"34export function SwitchSizes() {5return (6<FieldGroup className="w-full max-w-[10rem]">7<Field orientation="horizontal">8<Switch id="switch-size-sm" size="sm" />9<FieldLabel htmlFor="switch-size-sm">Small</FieldLabel>10</Field>11<Field orientation="horizontal">12<Switch id="switch-size-default" size="default" />13<FieldLabel htmlFor="switch-size-default">Default</FieldLabel>14</Field>15</FieldGroup>16)17}
RTL
يتم مشاركة التركيز عبر الأجهزة، ويتم إيقاف تشغيله عند مغادرة التطبيق.
switch-rtl.tsx
1"use client"23import {4useTranslation,5type Translations,6} from "./language-selector"7import {8Field,9FieldContent,10FieldDescription,11FieldLabel,12} from "@/components/ui/field"13import { Switch } from "@/components/ui/switch"1415const translations: Translations = {16en: {17dir: "ltr",18values: {19label: "Share across devices",20description:21"Focus is shared across devices, and turns off when you leave the app.",22},23},24ar: {25dir: "rtl",26values: {27label: "المشاركة عبر الأجهزة",28description:29"يتم مشاركة التركيز عبر الأجهزة، ويتم إيقاف تشغيله عند مغادرة التطبيق.",30},31},32he: {33dir: "rtl",34values: {35label: "שיתוף בין מכשירים",36description: "המיקוד משותף בין מכשירים, וכבה כשאתה עוזב את האפליקציה.",37},38},39}4041export function SwitchRtl() {42const { dir, t } = useTranslation(translations, "ar")4344return (45<Field orientation="horizontal" className="max-w-sm" dir={dir}>46<FieldContent>47<FieldLabel htmlFor="switch-focus-mode-rtl" dir={dir}>48{t.label}49</FieldLabel>50<FieldDescription dir={dir}>{t.description}</FieldDescription>51</FieldContent>52<Switch id="switch-focus-mode-rtl" dir={dir} />53</Field>54)55}
language-selector.tsx
1"use client"23import { createContext, useContext, useState, type ReactNode } from "react"45const languages = [6["ar", "Arabic (العربية)"],7["en", "English"],8["he", "Hebrew (עברית)"],9] as const1011type Language = (typeof languages)[number][0]1213export type Translations = Record<14Language,15{ dir: "ltr" | "rtl"; values: Record<string, string> }16>1718const LanguageContext = createContext<Language | undefined>(undefined)1920export function useTranslation(translations: Translations, defaultLanguage: Language) {21const { dir, values: t } = translations[useContext(LanguageContext) ?? defaultLanguage]22return { dir, t }23}2425export function LanguageSelector({26children,27defaultLanguage,28}: {29children: ReactNode30defaultLanguage: Language31}) {32const [language, setLanguage] = useState(defaultLanguage)3334return (35<LanguageContext value={language}>36<div className="grid justify-items-center gap-4">37<select38aria-label="Language"39className="rounded-md border border-input bg-background px-2 py-1 text-sm"40value={language}41onChange={(event) => {42const selected = languages.find(([value]) => value === event.currentTarget.value)43if (selected) setLanguage(selected[0])44}}45>46{languages.map(([value, label]) => (47<option key={value} value={value}>{label}</option>48))}49</select>50<div lang={language}>{children}</div>51</div>52</LanguageContext>53)54}
switch-rtl-example.tsx
1import { LanguageSelector } from "./language-selector"2import { SwitchRtl } from "./switch-rtl"34export function SwitchRtlExample() {5return (6<LanguageSelector defaultLanguage="ar">7<SwitchRtl />8</LanguageSelector>9)10}
Controlled form
switch-form.tsx
1"use client"23import { useState } from "react"45import { Field, FieldLabel } from "@/components/ui/field"6import { Switch } from "@/components/ui/switch"78export function SwitchForm() {9const [checked, setChecked] = useState(false)10const [submitted, setSubmitted] = useState("")1112return (13<form14aria-label="Notification preferences"15className="flex flex-col gap-4"16onSubmit={(event) => {17event.preventDefault()18const value = new FormData(event.currentTarget).get("updates")19setSubmitted(typeof value === "string" ? value : "No updates")20}}21>22<Field orientation="horizontal">23<Switch24id="switch-product-updates"25name="updates"26value="weekly"27checked={checked}28onCheckedChange={setChecked}29/>30<FieldLabel htmlFor="switch-product-updates">Product updates</FieldLabel>31</Field>32<Field orientation="horizontal">33<Switch id="switch-security-alerts" name="security" defaultChecked />34<FieldLabel htmlFor="switch-security-alerts">Security alerts</FieldLabel>35</Field>36<div className="flex gap-3">37<button type="submit">Save</button>38<button type="button" onClick={() => setChecked(false)}>39Turn off product updates40</button>41<button type="reset">Reset</button>42</div>43<output>{submitted}</output>44</form>45)46}
API Reference
| Prop | Type | Default |
|---|---|---|
size | "sm" | "default" | "default" |