Saklar nyala/mati dari checkbox native dengan role switch, tanpa JavaScript di Blade.
<x-markas.switch label="Airplane mode" /><x-markas.switch label="Airplane mode" disabled />import { Switch } from "@/components/markas/Switch";
<Switch label="Airplane mode" />import { Switch } from "@/components/markas/Switch";
<Switch label="Airplane mode" disabled />import Switch from "@/components/markas/Switch.vue";
<Switch label="Airplane mode" />import Switch from "@/components/markas/Switch.vue";
<Switch label="Airplane mode" disabled /><script lang="ts">
import Switch from "$lib/components/markas/Switch.svelte";
</script>
<div class="grid gap-4">
<Switch name="notifications" label="Email notifications" checked />
<Switch name="saver" label="Data saver mode" />
<Switch name="beta" label="Beta features" disabled />
</div>---
import Switch from "@/components/markas/Switch.astro";
---
<div class="grid gap-4">
<Switch name="notifications" label="Email notifications" checked />
<Switch name="saver" label="Data saver mode" />
<Switch name="beta" label="Beta features" disabled />
</div>Contoh
Status
<div class="grid gap-4">
<x-markas.switch name="wifi" label="Wi-Fi" checked />
<x-markas.switch name="bluetooth" label="Bluetooth" />
<x-markas.switch name="location" label="Location services" disabled />
<x-markas.switch name="find_device" label="Find my device" checked disabled />
</div><script lang="ts">
import Switch from "$lib/components/markas/Switch.svelte";
</script>
<div class="grid gap-4">
<Switch name="wifi" label="Wi-Fi" checked />
<Switch name="bluetooth" label="Bluetooth" />
<Switch name="location" label="Location services" disabled />
<Switch name="find_device" label="Find my device" checked disabled />
</div>---
import Switch from "@/components/markas/Switch.astro";
---
<div class="grid gap-4">
<Switch name="wifi" label="Wi-Fi" checked />
<Switch name="bluetooth" label="Bluetooth" />
<Switch name="location" label="Location services" disabled />
<Switch name="find_device" label="Find my device" checked disabled />
</div>Daftar pengaturan
Push notifications
Alerts on your phone for new messages.
Weekly digest
A summary of activity every Monday.
Product news
Occasional emails about new features.
Push notifications
Alerts on your phone for new messages.
Weekly digest
A summary of activity every Monday.
Product news
Occasional emails about new features.
Push notifications
Alerts on your phone for new messages.
Weekly digest
A summary of activity every Monday.
Product news
Occasional emails about new features.
Push notifications
Alerts on your phone for new messages.
Weekly digest
A summary of activity every Monday.
Product news
Occasional emails about new features.
Push notifications
Alerts on your phone for new messages.
Weekly digest
A summary of activity every Monday.
Product news
Occasional emails about new features.
Kartu fitur
Two-factor authentication
Ask for a code from your authenticator app each time you sign in.
Two-factor authentication
Ask for a code from your authenticator app each time you sign in.
Two-factor authentication
Ask for a code from your authenticator app each time you sign in.
Two-factor authentication
Ask for a code from your authenticator app each time you sign in.
Two-factor authentication
Ask for a code from your authenticator app each time you sign in.
<div class="flex items-start gap-4 rounded-lg border border-line bg-surface p-4 transition-colors duration-150 ease-out has-checked:border-primary motion-reduce:transition-none">
<span class="flex size-10 shrink-0 items-center justify-center rounded-md bg-primary-soft text-primary">
<svg class="size-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 3 5 6v5c0 4.5 3 8.5 7 10 4-1.5 7-5.5 7-10V6l-7-3z" /><path d="m9 12 2 2 4-4" /></svg>
</span>
<div class="grid flex-1 gap-0.5">
<p class="text-sm font-medium text-ink">Two-factor authentication</p>
<p class="text-sm text-muted">Ask for a code from your authenticator app each time you sign in.</p>
</div>
<x-markas.switch name="two_factor" aria-label="Two-factor authentication" checked />
</div>Horizontal
<div class="flex flex-wrap gap-x-6 gap-y-3">
<x-markas.switch name="autoplay" label="Autoplay" checked />
<x-markas.switch name="captions" label="Captions" />
<x-markas.switch name="loop" label="Loop" />
</div>import { Switch } from "@/components/markas/Switch";
export default function Example() {
return (
<div className="flex flex-wrap gap-x-6 gap-y-3">
<Switch name="autoplay" label="Autoplay" defaultChecked />
<Switch name="captions" label="Captions" />
<Switch name="loop" label="Loop" />
</div>
);
}<script lang="ts">
import Switch from "$lib/components/markas/Switch.svelte";
</script>
<div class="flex flex-wrap gap-x-6 gap-y-3">
<Switch name="autoplay" label="Autoplay" checked />
<Switch name="captions" label="Captions" />
<Switch name="loop" label="Loop" />
</div>---
import Switch from "@/components/markas/Switch.astro";
---
<div class="flex flex-wrap gap-x-6 gap-y-3">
<Switch name="autoplay" label="Autoplay" checked />
<Switch name="captions" label="Captions" />
<Switch name="loop" label="Loop" />
</div>Instalasi
npx markasui@latest add switchpnpm dlx markasui@latest add switchyarn dlx markasui@latest add switchbunx --bun markasui@latest add switch- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
resources/views/components/markas/switch.blade.php.
- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
src/components/markas/Switch.tsx.
- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
src/components/markas/Switch.vue.
- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
src/lib/components/markas/Switch.svelte.
- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
src/components/markas/Switch.astro.
- Pastikan Tailwind CSS v4 dan markas.css sudah terpasang. Lihat Instalasi.
- Salin source komponen di bawah ini ke
src/app/markas/switch.component.ts.
Pemakaian
<x-markas.switch name="notifikasi" label="Notifikasi email" :checked="$user->wants_email" />Tidak butuh Alpine.js.
import { Switch } from "@/components/markas/Switch";
<Switch
name="notifikasi"
label="Notifikasi email"
defaultChecked
onCheckedChange={(checked) => console.log(checked)}
/>;<script setup lang="ts">
import { ref } from "vue";
import Switch from "@/components/markas/Switch.vue";
const notifikasi = ref(true);
</script>
<template>
<Switch v-model="notifikasi" name="notifikasi" label="Notifikasi email" />
</template><script lang="ts">
import Switch from "$lib/components/markas/Switch.svelte";
let notifikasi = $state(true);
</script>
<Switch
bind:checked={notifikasi}
name="notifications"
label="Notifikasi email"
/>---
import Switch from "@/components/markas/Switch.astro";
---
<Switch name="notifications" label="Notifikasi email" checked />Tanpa script klien: switch adalah checkbox native.
import { Component, signal } from "@angular/core";
import { Switch } from "./markas/switch.component";
@Component({
selector: "app-pengaturan",
imports: [Switch],
template: `
<markas-switch
[(checked)]="notifikasi"
name="notifications"
label="Notifikasi email"
/>
`,
})
export class Pengaturan {
readonly notifikasi = signal(true);
}- Switch adalah
<input type="checkbox" role="switch">, jadi nilainya ikut terkirim bersama form seperti checkbox biasa. - Status aktif dibacakan dari
checkednative, sehingga tidak perluaria-checked. Tampilan memakaichecked:danpeer-checked:, bukandata-state. - Komponen ini tidak punya teks bawaan. Cukup isi
labeldalam Bahasa Indonesia.
Props
| Nama | Tipe | Default | Keterangan |
|---|---|---|---|
label | string | - | Teks label di samping saklar, terhubung lewat for/id. |
checked / defaultChecked / v-model / bind:checked | boolean | false | Keadaan aktif. Blade dan Astro memakai atribut checked, React checked atau defaultChecked, Vue v-model, Svelte bind:checked, Angular checked atau [(checked)]. |
onCheckedChange | (checked: boolean) => void | - | Khusus React. Dipanggil dengan status baru setiap kali saklar berubah. |
checkedChange | output<boolean> | - | Khusus Angular. Memancarkan keadaan baru setiap kali switch berubah. |
disabled | boolean | false | Menonaktifkan saklar dan meredupkan label. |
id | string | auto | Id input. Dibuat otomatis kalau kosong. |
Aksesibilitas
- Input checkbox native dengan role switch, sehingga screen reader membacakan aktif atau nonaktif dari status checked.
- Spasi mengubah status, label yang diklik ikut mengubah status.
- Thumb hanya dekorasi (aria-hidden) dan bergerak lewat CSS peer-checked.
Komponen terkait
Komponen form lain dengan class yang sama di setiap framework.
- ButtonTombol dengan varian primary, secondary, ghost, dan danger.
- CheckboxKotak centang native dengan label dan deskripsi opsional, tampilan kustom tanpa JavaScript.
- ComboboxInput teks dengan daftar saran terfilter di popover, mengikuti pola WAI-ARIA Combobox dengan autocomplete list.
- InputKolom teks dengan label, petunjuk, dan pesan error yang terhubung lewat ARIA.
- Input OTPInput kode sekali pakai yang dipecah menjadi kotak satu digit, dengan maju otomatis, dukungan tempel, dan satu input tersembunyi untuk submit form.
- LabelLabel form dengan gaya konsisten serta penanda wajib atau opsional yang dibacakan screen reader dengan benar.