Switch
A control that shows whether a setting is on or off.
vue
<script setup lang="ts">
import { Switch } from '@vase-ui/vue/switch';
import styles from './index.module.css';
</script>
<template>
<label :class="styles.Label">
<span :class="styles.Track">
<Switch default-checked :class="styles.Switch" />
<span :class="styles.Thumb" />
</span>
Notifications
</label>
</template>css
.Label {
display: flex;
align-items: center;
gap: 0.5rem;
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 400;
color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Track {
position: relative;
display: flex;
flex-shrink: 0;
}
.Switch {
appearance: none;
box-sizing: border-box;
width: 2.25rem;
height: 1.25rem;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
margin: 0;
background-color: white;
transition: background-color 150ms ease;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
}
&:checked {
background-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: 2px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.Thumb {
position: absolute;
top: 0.1875rem;
left: 0.1875rem;
width: 0.875rem;
height: 0.875rem;
pointer-events: none;
background-color: oklch(14.5% 0 0deg);
transition:
translate 150ms ease,
background-color 150ms ease;
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
.Switch:checked + .Thumb {
translate: 1rem 0;
background-color: white;
@media (prefers-color-scheme: dark) {
background-color: oklch(14.5% 0 0deg);
}
}vue
<script setup lang="ts">
import { Switch } from '@vase-ui/vue/switch';
</script>
<template>
<label class="flex items-center gap-2 text-sm font-normal text-neutral-950 dark:text-white">
<span class="relative flex shrink-0">
<Switch
default-checked
class="peer m-0 h-5 w-9 appearance-none rounded-none border border-neutral-950 bg-white transition-colors duration-150 ease-[ease] checked:bg-neutral-950 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-neutral-950 dark:border-white dark:bg-neutral-950 dark:checked:bg-white dark:focus-visible:outline-white"
/>
<span
class="pointer-events-none absolute top-[0.1875rem] left-[0.1875rem] size-3.5 bg-neutral-950 transition-[translate,background-color] duration-150 ease-[ease] peer-checked:translate-x-4 peer-checked:bg-white dark:bg-white dark:peer-checked:bg-neutral-950"
/>
</span>
Notifications
</label>
</template>Usage guidelines
- Form controls must have an accessible name: Give the switch a
<label>element, or aField.Labelin aField. See Labeling a switch and the forms guide.
Anatomy
Import the component:
vue
<script setup lang="ts">
import { Switch } from '@vase-ui/vue/switch';
</script>
<template>
<Switch />
</template>Examples
Labeling a switch
A <label> around the switch is the simplest pattern to label it:
vue
<script setup lang="ts">
import { Switch } from '@vase-ui/vue/switch';
</script>
<template>
<label>
<Switch name="notifications" />
Notifications
</label>
</template>A <label> beside the switch names it with for and id:
vue
<script setup lang="ts">
import { Switch } from '@vase-ui/vue/switch';
</script>
<template>
<Switch id="notifications" name="notifications" />
<label for="notifications">Notifications</label>
</template>Show a thumb
Give the input appearance: none, and put an element of your own after it for the thumb. Select the thumb with :checked + .thumb:
vue
<script setup lang="ts">
import { Switch } from '@vase-ui/vue/switch';
</script>
<template>
<label class="label">
<span class="track">
<Switch class="switch" />
<span class="thumb" />
</span>
Notifications
</label>
</template>
<style scoped>
.track {
position: relative;
display: inline-flex;
}
.switch {
appearance: none;
width: 2.25rem;
height: 1.25rem;
margin: 0;
border: 1px solid;
}
.thumb {
position: absolute;
top: 0.1875rem;
left: 0.1875rem;
width: 0.875rem;
height: 0.875rem;
background: currentColor;
pointer-events: none;
transition: translate 150ms;
}
.switch:checked + .thumb {
translate: 1rem 0;
}
</style>The element slot renders the input and gives its state. Render the thumb from state.checked:
vue
<script setup lang="ts">
import { Switch } from '@vase-ui/vue/switch';
</script>
<template>
<label>
<Switch>
<template #element="{ props, elementRef, state }">
<span class="track">
<input v-bind="props" :ref="elementRef" class="switch" />
<span class="thumb" :class="{ on: state.checked }" />
</span>
</template>
</Switch>
Notifications
</label>
</template>
<style scoped>
.track {
position: relative;
display: inline-flex;
}
.switch {
appearance: none;
width: 2.25rem;
height: 1.25rem;
margin: 0;
border: 1px solid;
}
.thumb {
position: absolute;
top: 0.1875rem;
left: 0.1875rem;
width: 0.875rem;
height: 0.875rem;
background: currentColor;
pointer-events: none;
}
.thumb.on {
translate: 1rem 0;
}
</style>API reference
Switch
A control that the user can set to on or off.
Renders an <input> element with the type checkbox and the role switch.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultChecked | boolean | undefined | false | The switch is on at the mount and at a native reset of the form, when it has no modelValue. |
disabled | boolean | undefined | false | Disables the switch. A disabled field or fieldset around the switch also disables it. |
id | string | undefined | The id of the switch. With no id, the switch has an id of its own. | |
modelValue | boolean | undefined | The switch is on. Bind it with v-model. With no modelValue, the switch keeps its own state. | |
name | string | undefined | The name of the switch in the data of the form. The name of a Field.Root around it wins. | |
readonly | boolean | undefined | false | The user cannot change the state. The switch stays in the tab order and is not validated. |
required | boolean | undefined | false | The user must set the switch to on before a submit of the form. |
uncheckedValue | string | undefined | The value in the data of a form while the switch is off. The form gets it after the mount, from an enabled switch that has a name. |
Slots
| Slot | Slot props | Description |
|---|---|---|
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SwitchState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Events
| Event | Arguments | Description |
|---|---|---|
checkedChange | [checked: boolean, details: SwitchChangeEventDetails] | The user presses the switch. details.event is the native click event of the press.
details.cancel() keeps the state as it was. |
update:modelValue | [checked: boolean] | Comes after the change event with the new value, unless a listener cancels the change. v-model uses it. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLInputElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
:checked | The switch is on. |
:disabled | The switch is disabled. |
:required | The switch is required and not read-only. |
[aria-readonly="true"] | The switch is read-only. |
[data-valid] | The field of the switch is valid. |
[aria-invalid="true"] | The field of the switch is invalid. |
[data-touched] | In a field, the focus left the switch. |
[data-dirty] | In a field, the value differs from the first value. |
Differences from Base UI
Vase UI ports Switch from Base UI 1.9.0.
API
| Base UI | Vase UI |
|---|---|
Switch.Root | Switch is the part: one native <input type="checkbox" role="switch">, so it needs no nativeButton. Its types are Switch.Props, Switch.State and Switch.ChangeEventReason, and the flat names SwitchProps, SwitchState and SwitchChangeEventReason. readonly is in place of readOnly, and value and form are plain attributes. The element of the template ref is the input, in place of inputRef. CSS selects :checked, :disabled, :required, :focus, [aria-readonly="true"] and [aria-invalid="true"], so the input has no data-checked, data-unchecked, data-filled, data-disabled, data-required, data-focused, data-readonly and data-invalid. |
Switch.Thumb | An element of the developer beside the input, or the element slot with state, gives the thumb. |
Behavior
| Vase UI | Evidence |
|---|---|
| Space changes a switch. Enter does not change it: the switch is a native checkbox, and Enter on it gives its form the implicit submission of the browser, as Enter on each native checkbox does. The WAI-ARIA Authoring Practices give Enter as optional for a switch. Base UI changes the switch at Enter. | |
A control in a field keeps the id of the developer in the server HTML when no Field.Label of the field renders before it, and a label that renders after the control has that id in for. When a label renders before the control, the control has the generated id of the field until the hydration, because that label names the generated id. Then the control takes its id, and the label follows. In each order, the label names the control in the server HTML. Base UI gives each control in a field the generated id until the hydration, also where no label needs it. |
|
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.