Switch
Alignment target:@astryxdesign/core v0.7.1 · Switch
Usage #
Switch toggles a single boolean state.
<script setup lang="ts">
import { Switch } from '@astryx-vue/core/Switch'
import { shallowRef } from 'vue'
const enabled = shallowRef(false)
</script>
<template>
<Switch v-model="enabled" label="Notifications" />
</template>Anatomy
| Element | Description |
|---|---|
| Field | Container arranging the switch, label, and feedback. |
| Track | Pill-shaped surface that shows the off or on state. |
| Thumb | Indicator that moves across the track when state changes. |
| Label | Text identifying the setting controlled by the switch. |
| Description | Helper text below the label. |
| Spinner | Loading indicator shown inside the thumb while busy. |
| Status message | Error, warning, or success message below the switch. |
Best practices #
| Guidance | Practices |
|---|---|
| Do | Use for settings that apply immediately; the toggle should take effect without a separate save action. |
| Do | Pair with a clear, concise label that describes the setting being controlled. |
| Don't | Use for options that require a form submission to take effect; use a checkbox instead. |
| Don't | Wrap a disabled switch in Tooltip to explain why it is disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead. |
Examples #
Switch — Disabled
A disabled switch with a label and a description, for a feature that is not available yet.
Switch — Settings panel
Several settings in one card, each one a switch.
Switch — With description
A line under the label says what turning the switch on will do.
Switch — With status
Error, warning, and success messages under the switch.
Keyboard interactions #
Switch is built on a native checkbox, so its keyboard behaviour follows the browser's defaults and every action is reachable without a mouse. With focus on the switch, these keys apply:
- Tab moves focus to the switch.
- Space toggles the checked state.
- Escape closes the disabled-reason tooltip.
The disabled-reason tooltip only appears when the switch is disabled and a disabledMessage is provided, so Escape has an effect only in that case.
Usage notes #
Use a switch for a two-state option that takes effect right away, such as turning notifications on or off. If an option only applies once the form is submitted, use a checkbox instead; otherwise users may assume the switch has already saved the change.
isRequired only shows the required indicator and sets aria-required; it does not perform validation or block form submission.