vael-ui v0.5.7

Forms & inputs

Layout

Switch

An on and off toggle styled like a physical switch. Use it for settings that take effect at once.

Validating a form? vael-form wires this up with a schema

Install

import { Switch } from 'vael-ui'

Playground

Placement, behaviour and animation

Usage
<script setup lang="ts">
import { ref } from 'vue'
import { Switch } from 'vael-ui'

const value = ref(false)
</script>

<template>
  <Switch label="Switch label" v-model="value" />
</template>

Props

NameTypeDefaultDescription
labelstringLabel text; the default slot replaces it.
disabledbooleanfalseDisables the switch and blocks interaction.
invalidbooleanfalseShows the invalid state. A surrounding Field in error sets it too.
size'sm' | 'md''md'Control size.
namestringNative name for form submission.
motionSwitchMotiontrueHow it moves: its appearance ({ initial: { thumb: { opacity: 0, scale: 0.6 } } }) and its states, like checked, dragging or invalid ({ states: { checked: { thumb: { scale: '1.1' } } } }). false turns its own animation off, built-in transitions too; animate the exposed trackEl/thumbEl yourself.
uiPartial<Record<'root' | 'track' | 'thumb' | 'label', UiPartValue>>Class and style overrides for each part.
modelValuebooleanfalseWhether the switch is on.

Events

NameTypeDescription
update:modelValue(value: boolean)Fires when modelValue changes (v-model).

Slots

NameTypeDescription
defaultanyLabel content; replaces the label text.

Exposed

NameTypeDescription
elHTMLElement | nullRoot element.
inputElHTMLInputElement | nullNative checkbox input (role="switch").
trackElHTMLElement | nullTrack element.
thumbElHTMLElement | nullThumb element.
draggingbooleanWhether the thumb is being dragged.
partsRecord<string, HTMLElement | HTMLElement[] | null>Its elements by part name, for your own animation; a repeated part is an array.

Motion New

Switch: its appearance and states.

<Switch
  :motion="{ states: { checked: { root: { scale: 1.04 } } } }"
/>
NameTypeDescription
initialexitroottrackthumbSets where each part comes in from and leaves to: any CSS property, plus x, y and blur. A plain number is px, or degrees for rotate.
statescheckeduncheckedinvaliddisabledfocusactivehoverdraggingSets values per part that hold while a state is on. An array is a set of keyframes that plays once as the state turns on, such as a shake on invalid.
enterleave(parts, context) => animationAnimates the parts with GSAP, motion or element.animate(). Return the animation and the component waits for it, and stops it if interrupted.
springdurationeasing{ damping, response } | number | stringTimes all of it: a spring, or a duration in ms with an easing. Never both.
falsebooleanTurns its motion off, for an animation of your own on its exposed elements.

Accessibility

Follows the Switch pattern from the WAI-ARIA Authoring Practices Guide.

KeyAction
SpaceTurns it on or off.
  • A native checkbox with role="switch" and aria-checked, inside a <label>, so the label prop or the default slot names it.
  • Inside a Field, the description and error are linked with aria-describedby, and it sets aria-invalid and aria-required.
  • id and aria-* attributes you pass go to the input.