Component
Vue Toggle Switch Component
<dom-toggle>Accessible switch with role=switch, keyboard support, and a smooth thumb animation.
Examples
Make On and Off explicit
Pair the switch with visible state text so its value is clear without relying on colour or thumb position. Keep the setting label the same as the state changes; On and Off describe the current value.
On / Off inside the switch
Use showState to put the current value in the track. The wider switch keeps the text clear of the thumb at every size.
npm install @getdom/studio<script setup>
import '@getdom/studio/style.css';
import { ref } from 'vue';
import { DomToggle } from '@getdom/studio';
const compact = ref(true);
const notifications = ref(false);
const sync = ref(true);
</script>
<template>
<div class="grid w-full max-w-md gap-5">
<DomToggle v-model="compact" show-state size="sm" label="Compact view" />
<DomToggle v-model="notifications" show-state label="Notifications" />
<DomToggle v-model="sync" show-state size="lg" label="Automatic sync" />
</div>
</template>
An On / Off badge
A check or minus symbol and a written state update together when you toggle the switch.
npm install @getdom/studio<script setup>
import '@getdom/studio/style.css';
import { ref } from 'vue';
import { DomToggle } from '@getdom/studio';
const preferences = ref([
{ id: 'autosave', label: 'Auto-save', enabled: true },
{ id: 'email-alerts', label: 'Email alerts', enabled: false },
]);
</script>
<template>
<div class="grid w-full max-w-md gap-5">
<div v-for="preference in preferences" :key="preference.id" class="flex items-center justify-between gap-4">
<DomToggle v-model="preference.enabled" :label="preference.label" />
<span
aria-hidden="true"
class="inline-flex min-w-18 shrink-0 items-center justify-center gap-1.5 rounded-full border px-3 py-1 text-xs font-semibold"
:class="preference.enabled ? 'border-primary bg-primary text-primary-fg' : 'border-border bg-secondary text-canvas-fg'"
>
<svg viewBox="0 0 16 16" class="size-3.5" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path :d="preference.enabled ? 'm3 8 3 3 7-7' : 'M4 8h8'" />
</svg>
{{ preference.enabled ? 'On' : 'Off' }}
</span>
</div>
</div>
</template>
Both states always visible
Keep Off and On on either side of the switch. Bold, underlined text marks the current state.
Sound effects
Desktop alerts
npm install @getdom/studio<script setup>
import '@getdom/studio/style.css';
import { ref } from 'vue';
import { DomToggle } from '@getdom/studio';
const preferences = ref([
{ id: 'sound', label: 'Sound effects', enabled: false },
{ id: 'desktop', label: 'Desktop alerts', enabled: true },
]);
</script>
<template>
<div class="grid w-full max-w-md gap-5">
<div v-for="preference in preferences" :key="preference.id" class="flex flex-wrap items-center justify-between gap-x-6 gap-y-2">
<p class="text-sm font-medium text-canvas-fg">{{ preference.label }}</p>
<div class="flex shrink-0 items-center gap-3">
<span
aria-hidden="true"
class="min-w-6 text-center text-sm underline-offset-4"
:class="!preference.enabled ? 'font-bold text-canvas-fg underline decoration-2' : 'text-muted-fg'"
>Off</span>
<DomToggle v-model="preference.enabled" :label="preference.label" chrome="none" />
<span
aria-hidden="true"
class="min-w-6 text-center text-sm underline-offset-4"
:class="preference.enabled ? 'font-bold text-canvas-fg underline decoration-2' : 'text-muted-fg'"
>On</span>
</div>
</div>
</div>
</template>
Explain what the state means
Settings rows pair the current value with its effect. Disabled switches still show whether they are On or Off.
npm install @getdom/studio<script setup>
import '@getdom/studio/style.css';
import { ref } from 'vue';
import { DomToggle } from '@getdom/studio';
const settings = ref([
{
id: 'backups',
label: 'Automatic backups',
enabled: true,
onDescription: 'On — A backup is saved every day.',
offDescription: 'Off — Automatic backups are paused.',
},
{
id: 'summary',
label: 'Weekly summary',
enabled: false,
onDescription: 'On — A summary arrives every Monday.',
offDescription: 'Off — No summary emails will be sent.',
},
{
id: 'security',
label: 'Security alerts',
enabled: true,
disabled: true,
onDescription: 'On — Required by your organisation.',
offDescription: 'Off — Disabled by your organisation.',
},
{
id: 'public-sharing',
label: 'Public sharing',
enabled: false,
disabled: true,
onDescription: 'On — Enabled by your organisation.',
offDescription: 'Off — Disabled by your organisation.',
},
]);
</script>
<template>
<div class="w-full max-w-lg divide-y divide-border rounded-xl border border-border bg-canvas">
<div v-for="setting in settings" :key="setting.id" class="p-4 sm:p-5">
<DomToggle
v-model="setting.enabled"
:label="setting.label"
:description="setting.enabled ? setting.onDescription : setting.offDescription"
:disabled="setting.disabled"
/>
</div>
</div>
</template>
Playground
Try every prop live
Toggle playground
Edit props in the inspector — the live switch and synthesised source stay in sync.
Properties
Control props
Switch size. Matches DomButton size names.
Field props
Optional ID override. By default parent forms derive the input ID from the field path using underscores.
Local field name. Parent forms derive the full field path and native HTML name from the form hierarchy.
Visible field label.
Optional helper copy below the field.
Placeholder shown when the control is empty.
Validation errors for this field.
Validators
No validators yet.
Validators attached to this field. Use functions in Vue code, or serializable records such as { name: "minLength", props: { min: 2 } } in generated schemas.
Render default field chrome, or hide chrome while keeping form state wiring.
npm install @getdom/studio<script setup>
import '@getdom/studio/style.css';
import { reactive } from 'vue';
import { DomToggle } from '@getdom/studio';
const data = reactive({
"modelValue": true,
"id": "",
"name": "",
"label": "Notifications",
"description": "",
"placeholder": "",
"required": false,
"disabled": false,
"readOnly": false,
"invalid": false,
"errors": [],
"visible": true,
"validators": [],
"validateOnBlur": true,
"chrome": "field",
"size": "md",
"showState": false
});
</script>
<template>
<DomToggle
v-bind="data"
@update:modelValue="data.modelValue = $event"
/>
</template>Demo
Live preview
Usage
Vue
<script setup>
import { DomToggle } from '@getdom/studio/vue';
import { ref } from 'vue';
const on = ref(true);
</script>
<template>
<DomToggle v-model="on" label="Notifications" size="md" />
</template>Usage
Web component
<dom-toggle checked aria-label="Notifications"></dom-toggle>
<script type="module">
import '@getdom/studio/headless';
document.querySelector('dom-toggle')
.addEventListener('dom:change', (e) => console.log(e.detail.checked));
</script>Props
Props
| Name | Type | Default | Description |
|---|---|---|---|
| v-model | boolean | false | Checked state. |
| label | string | — | Visible label next to the toggle. |
| size | 'sm' | 'md' | 'lg' | 'md' | Switch size. Uses the same size names as DomButton. |
| showState | boolean | false | Show On or Off inside a wider switch track. |
| disabled | boolean | false | Disable interaction. |
Keyboard
- Space / EnterToggle the value.
- TabMove focus through toggles in source order.