GitHub

Switch

switch-demo.tsx
1
import { Label } from "@/components/ui/label"
2
import { Switch } from "@/components/ui/switch"
3
4
export function SwitchDemo() {
5
return (
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/switch

Usage

switch-usage.tsx
1
import { Switch } from "@/components/ui/switch"
2
3
<Switch aria-label="Airplane Mode" />

Examples

Description

Focus is shared across devices, and turns off when you leave the app.

switch-description.tsx
1
import {
2
Field,
3
FieldContent,
4
FieldDescription,
5
FieldLabel,
6
} from "@/components/ui/field"
7
import { Switch } from "@/components/ui/switch"
8
9
export function SwitchDescription() {
10
return (
11
<Field orientation="horizontal" className="max-w-sm">
12
<FieldContent>
13
<FieldLabel htmlFor="switch-focus-mode">
14
Share across devices
15
</FieldLabel>
16
<FieldDescription>
17
Focus 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
1
import {
2
Field,
3
FieldContent,
4
FieldDescription,
5
FieldGroup,
6
FieldLabel,
7
FieldTitle,
8
} from "@/components/ui/field"
9
import { Switch } from "@/components/ui/switch"
10
11
export function SwitchChoiceCard() {
12
return (
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>
19
Focus is shared across devices, and turns off when you leave the
20
app.
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>
31
Receive 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
1
import { Field, FieldLabel } from "@/components/ui/field"
2
import { Switch } from "@/components/ui/switch"
3
4
export function SwitchDisabled() {
5
return (
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
1
import {
2
Field,
3
FieldContent,
4
FieldDescription,
5
FieldLabel,
6
} from "@/components/ui/field"
7
import { Switch } from "@/components/ui/switch"
8
9
export function SwitchInvalid() {
10
return (
11
<Field orientation="horizontal" className="max-w-sm" data-invalid>
12
<FieldContent>
13
<FieldLabel htmlFor="switch-terms">
14
Accept terms and conditions
15
</FieldLabel>
16
<FieldDescription>
17
You 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
1
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
2
import { Switch } from "@/components/ui/switch"
3
4
export function SwitchSizes() {
5
return (
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"
2
3
import {
4
useTranslation,
5
type Translations,
6
} from "./language-selector"
7
import {
8
Field,
9
FieldContent,
10
FieldDescription,
11
FieldLabel,
12
} from "@/components/ui/field"
13
import { Switch } from "@/components/ui/switch"
14
15
const translations: Translations = {
16
en: {
17
dir: "ltr",
18
values: {
19
label: "Share across devices",
20
description:
21
"Focus is shared across devices, and turns off when you leave the app.",
22
},
23
},
24
ar: {
25
dir: "rtl",
26
values: {
27
label: "المشاركة عبر الأجهزة",
28
description:
29
"يتم مشاركة التركيز عبر الأجهزة، ويتم إيقاف تشغيله عند مغادرة التطبيق.",
30
},
31
},
32
he: {
33
dir: "rtl",
34
values: {
35
label: "שיתוף בין מכשירים",
36
description: "המיקוד משותף בין מכשירים, וכבה כשאתה עוזב את האפליקציה.",
37
},
38
},
39
}
40
41
export function SwitchRtl() {
42
const { dir, t } = useTranslation(translations, "ar")
43
44
return (
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"
2
3
import { createContext, useContext, useState, type ReactNode } from "react"
4
5
const languages = [
6
["ar", "Arabic (العربية)"],
7
["en", "English"],
8
["he", "Hebrew (עברית)"],
9
] as const
10
11
type Language = (typeof languages)[number][0]
12
13
export type Translations = Record<
14
Language,
15
{ dir: "ltr" | "rtl"; values: Record<string, string> }
16
>
17
18
const LanguageContext = createContext<Language | undefined>(undefined)
19
20
export function useTranslation(translations: Translations, defaultLanguage: Language) {
21
const { dir, values: t } = translations[useContext(LanguageContext) ?? defaultLanguage]
22
return { dir, t }
23
}
24
25
export function LanguageSelector({
26
children,
27
defaultLanguage,
28
}: {
29
children: ReactNode
30
defaultLanguage: Language
31
}) {
32
const [language, setLanguage] = useState(defaultLanguage)
33
34
return (
35
<LanguageContext value={language}>
36
<div className="grid justify-items-center gap-4">
37
<select
38
aria-label="Language"
39
className="rounded-md border border-input bg-background px-2 py-1 text-sm"
40
value={language}
41
onChange={(event) => {
42
const selected = languages.find(([value]) => value === event.currentTarget.value)
43
if (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
1
import { LanguageSelector } from "./language-selector"
2
import { SwitchRtl } from "./switch-rtl"
3
4
export function SwitchRtlExample() {
5
return (
6
<LanguageSelector defaultLanguage="ar">
7
<SwitchRtl />
8
</LanguageSelector>
9
)
10
}

Controlled form

switch-form.tsx
1
"use client"
2
3
import { useState } from "react"
4
5
import { Field, FieldLabel } from "@/components/ui/field"
6
import { Switch } from "@/components/ui/switch"
7
8
export function SwitchForm() {
9
const [checked, setChecked] = useState(false)
10
const [submitted, setSubmitted] = useState("")
11
12
return (
13
<form
14
aria-label="Notification preferences"
15
className="flex flex-col gap-4"
16
onSubmit={(event) => {
17
event.preventDefault()
18
const value = new FormData(event.currentTarget).get("updates")
19
setSubmitted(typeof value === "string" ? value : "No updates")
20
}}
21
>
22
<Field orientation="horizontal">
23
<Switch
24
id="switch-product-updates"
25
name="updates"
26
value="weekly"
27
checked={checked}
28
onCheckedChange={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)}>
39
Turn off product updates
40
</button>
41
<button type="reset">Reset</button>
42
</div>
43
<output>{submitted}</output>
44
</form>
45
)
46
}

API Reference

Switch props
PropTypeDefault
size"sm" | "default""default"
UI SDK Switch API Reference