Switch
Switch is a boolean control for an immediate on/off setting, built on reka-ui. It renders a real <button role="switch"> with an aria-checked state and a token-based track and thumb. Use it for settings that apply instantly; use a Checkbox for a choice that is submitted with a form.
vue
<script setup lang="ts">
import { ref } from 'vue'
import { Switch } from '@myghf/ui'
const alerts = ref(true)
</script>
<template>
<label class="flex items-center gap-2">
Enable alerts
<Switch v-model="alerts" />
</label>
</template>Examples
Sizes, labels, and disabled state
Wrap the control in a <label> (a <button> is a labelable element) for a click target and an accessible name, or point a label at its id. size="sm" renders a smaller track, and disabled removes it from interaction and dims it.
vue
<script setup lang="ts">
import { ref } from 'vue'
import { Switch } from '@myghf/ui'
const alerts = ref(true)
const digest = ref(false)
</script>
<template>
<div class="grid max-w-md gap-3">
<label class="flex items-center justify-between gap-3 text-sm text-foreground">
Enable alerts
<Switch v-model="alerts" />
</label>
<label class="flex items-center justify-between gap-3 text-sm text-foreground">
Daily digest
<Switch v-model="digest" size="sm" />
</label>
<label class="flex items-center justify-between gap-3 text-sm text-muted">
Locked by your administrator
<Switch :model-value="true" disabled />
</label>
</div>
</template>Props
| Prop | Type | Default | Description |
|---|---|---|---|
modelValue | boolean | false | On/off state. |
disabled | boolean | false | Disables the control and blocks interaction. |
invalid | boolean | false | Marks the control invalid. Falls through to the FormField context when omitted. |
required | boolean | false | Marks the control required. Falls through to the FormField context when omitted. |
size | 'sm' | 'default' | 'default' | Track size. |
id | string | — | Applied to the switch button so an external <label for> matches. |
name | string | — | Submitted with the owning form as a name/value pair. |
Events
| Event | Payload | Description |
|---|---|---|
update:modelValue | boolean | Emitted when the on/off state changes. |
Slots
Switch renders no slots. The track and thumb are built in.
Exposed methods
None. Switch does not call defineExpose.
Accessibility
- reka-ui renders a
<button role="switch">witharia-checkedreflecting the model, and wires Space/Enter to toggle. It is keyboard reachable by default. - Provide an accessible name by wrapping the control in a
<label>, by pointing a<label for>at itsid, or witharia-label; extra attributes fall through to the root button. - Inside a
FormFieldit picks upid,aria-describedby,aria-invalid, andaria-requiredthrough the shareduseFieldControlcomposable, so an explicit prop always wins and the context fills the rest.
Dark mode & RTL
- The track styles from semantic tokens (
bg-surface-mutedoff,bg-primary-500on) and the thumb isbg-surface, so it adapts to dark mode automatically. The focus ring usesdark:focus-visible:ring-offset-backgroundso it stays visible on dark surfaces. - The thumb animates with logical translation:
data-[state=checked]:translate-x-*in LTR andrtl:data-[state=checked]:-translate-x-*in RTL, so the knob moves toward the trailing edge in both directions.
