Switch

Alignment target:@astryxdesign/core v0.7.1 · Switch

Usage #

Switch toggles a single boolean state.

Vue
<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

ElementDescription
FieldContainer arranging the switch, label, and feedback.
TrackPill-shaped surface that shows the off or on state.
ThumbIndicator that moves across the track when state changes.
LabelText identifying the setting controlled by the switch.
DescriptionHelper text below the label.
SpinnerLoading indicator shown inside the thumb while busy.
Status messageError, warning, or success message below the switch.

Best practices #

GuidancePractices
DoUse for settings that apply immediately; the toggle should take effect without a separate save action.
DoPair with a clear, concise label that describes the setting being controlled.
Don'tUse for options that require a form submission to take effect; use a checkbox instead.
Don'tWrap 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

Upgrade to enable this option
Open in Playground

A disabled switch with a label and a description, for a feature that is not available yet.

Switch — Settings panel

Open in Playground

Several settings in one card, each one a switch.

Switch — With description

Switch to a darker color scheme.
Open in Playground

A line under the label says what turning the switch on will do.

Switch — With status

You must accept the terms to continue
Help us improve by sharing anonymous usage statistics
This data may be shared with partners
Your account is now more secure
Open in Playground

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.