Switch
A control that toggles between on and off states.
Usage
<script setup lang="ts">
import { ref } from 'vue'
const enabled = ref(true)
</script>
<template>
<TuffSwitch v-model="enabled" />
</template>
Toggle State
Click, Enter, or Space toggles the switch.
Loading demo...
Sizes
size also sets the label's font size and gap.
Loading demo...
Disabled
Loading demo...
Loading
Set loading while a server confirms the change. The thumb stays on the old value as a spinning ring, and toggling is blocked.
Loading demo...
Labels
A changing label crossfades; labelPlacement puts it before or after the track. The default slot renders as-is, without the transition.
Loading demo...
Custom Colors
Override --tuff-switch-active-color, --tuff-switch-track-color, or --tuff-switch-thumb-color.
Loading demo...
Settings Row
When state text sits outside the switch, render it with TxTextTransformer to get the same transition.
Loading demo...
Best Practices
- Use a switch for settings that apply immediately; use a checkbox for values submitted with a form.
- When the server must confirm, set
loadingand writemodelValueonly on success. On failure, just clearloading. - Put the control's name in
label("Compact mode"). Keep state text such as "On" outside, rendered withTxTextTransformer, so it isn't announced twice. - Recolor through the CSS variables, not by rewriting
.tuff-switch__trackrules.
API Reference
Props
| Prop | Type | Default | Description |
|---|---|---|---|
modelValue | boolean | false | The on/off state, bound with v-model. |
label | string | - | Text beside the track; crossfades when it changes. |
labelPlacement | 'start' | 'end' | 'end' | Puts the label before or after the track. |
size | 'small' | 'default' | 'large' | 'default' | Size; also sets the label's font size and gap. |
disabled | boolean | false | Blocks toggling and removes the switch from the tab order. |
loading | boolean | false | Shows a spinning ring and blocks toggling. |
Events
| Event | Params | Description |
|---|---|---|
update:modelValue | (value: boolean) => void | Fires after a user toggle with the new value. |
change | (value: boolean) => void | Fires together with update:modelValue. |
Slots
| Slot | Props | Description |
|---|---|---|
default | - | Custom label content; overrides label and renders without the transition. |
Overview
- The root is a native
<button role="switch">witharia-checked. - It is controlled: a toggle only emits, and the switch updates once the parent writes
modelValueback. disabledandloadingboth set the nativedisabledattribute;loadingkeeps full opacity and setsaria-busy="true".- With visible text (
labelor the slot), noaria-labelis rendered. - Under reduced motion, the loading ring stops spinning.
Technologies
- The state classes
is-active,is-disabled,is-loading, andhas-labelsit on the root; the track and thumb are.tuff-switch__trackand.tuff-switch__thumb. - Source:
packages/tuffex/packages/components/src/switch/.
查看源码
packages/tuffex/packages/components/src/switch/index.ts