Sakelar
Tombol pengalih on/off untuk pengaturan instan — notifikasi, mode gelap, bendera fitur — dengan sentuhan desain neobrutalis berupa garis tepi dan bayangan.
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
Switch adalah sakelar dua keadaan — nyala atau mati, berlaku seketika saat dibalik, tanpa tombol kirim. Dibangun di atas komponen Base UI Switch dan ditata dengan resep neobrutalis: garis tepi tebal, bayangan keras, dan tipografi tebal.
Cocok digunakan untuk:
- Panel pengaturan — notifikasi, mode gelap, putar otomatis: preferensi yang langsung berlaku.
- Feature flag — mengaktifkan fitur beta atau berpindah antara tagihan bulanan dan tahunan.
- Kontrol per baris — mengaktifkan atau menjeda item di tabel atau daftar tanpa membuka formulir.
Instalasi#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/switch.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/switch.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/switch.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/base/switch.json
Instal dependensi berikut:
pnpm add @base-ui/reactnpm install @base-ui/reactyarn add @base-ui/reactbun add @base-ui/react
Salin dan tempel kode berikut ke dalam proyek Anda.
"use client"
import * as React from "react"
import { Switch as SwitchPrimitive } from "radix-ui"
import { cn } from "@/lib/utils"
function Switch({
className,
size = "default",
...props
}: React.ComponentProps<typeof SwitchPrimitive.Root> & {
size?: "sm" | "default"
}) {
return (
<SwitchPrimitive.Root
data-slot="switch"
data-size={size}
className={cn(
"peer group/switch relative inline-flex shrink-0 cursor-pointer items-center border-2 border-foreground transition-all outline-none after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary aria-invalid:border-destructive data-[size=default]:h-6 data-[size=default]:w-11 data-[size=sm]:h-5 data-[size=sm]:w-9 data-checked:bg-primary data-disabled:cursor-not-allowed data-disabled:opacity-50",
className
)}
{...props}
>
<SwitchPrimitive.Thumb
data-slot="switch-thumb"
className="pointer-events-none mx-0.5 block border-2 border-foreground bg-primary transition-transform group-data-[size=default]/switch:size-4 group-data-[size=sm]/switch:size-3 group-data-[size=default]/switch:data-checked:translate-x-5 group-data-[size=sm]/switch:data-checked:translate-x-4 data-checked:bg-background data-unchecked:translate-x-0"
/>
</SwitchPrimitive.Root>
)
}
export { Switch }
Sesuaikan jalur impor dengan pengaturan proyek Anda.
Penggunaan#
import { Switch } from "@/components/ui/switch"<Switch />Contoh#
Deskripsi#
import {
Field,
FieldContent,Kartu Pilihan#
Pemilihan bergaya kartu, tempat FieldLabel membungkus seluruh Field untuk pola kartu yang dapat diklik.
import {
Field,
FieldContent,Nonaktif#
Tambahkan prop disabled ke komponen Switch untuk menonaktifkan switch. Tambahkan prop data-disabled ke komponen Field untuk penataan gaya.
import { Field, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
Tidak Valid#
Tambahkan prop aria-invalid ke komponen Switch untuk menunjukkan status tidak valid. Tambahkan prop data-invalid ke komponen Field untuk penataan gaya.
import {
Field,
FieldContent,Ukuran#
Gunakan prop size untuk mengubah ukuran switch.
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
RTL#
Untuk mengaktifkan dukungan RTL di Neobrutalism, lihat panduan konfigurasi RTL.
"use client"
import * as React from "react"Aksesibilitas#
Switch mengikuti pola Switch WAI-ARIA: ia merender tombol sungguhan dengan role="switch" dan aria-checked, plus input tersembunyi agar nilainya ikut terkirim bersama formulir yang melingkupinya.
Interaksi keyboard:
| Tombol | Aksi |
|---|---|
Tab / Shift + Tab | Memindahkan fokus ke / dari switch |
Space | Membalik keadaan switch |
Enter | Membalik keadaan switch |
Referensi API#
Lihat dokumentasi Base UI Switch.