Switch
Usage
Use the v-model directive to control whether the switch is on.
<script setup lang="ts">
import { ref } from 'vue'
const hasTwoFactorAuthentication = ref(true)
</script>
<template>
<KSwitch v-model="hasTwoFactorAuthentication" label="Two-factor authentication" />
</template>
Label
Use the label prop to set the text next to the switch. Without a visible label, set aria-label so screen readers can still name the switch.
<KSwitch v-model="sendsWeeklySummary" label="Weekly summary" />
<KSwitch v-model="hidesBalances" aria-label="Hide balances" />
Description
Use the description prop to explain the setting below the label. It is linked to the switch with aria-describedby.
<KSwitch
v-model="sendsLargeTransactionAlerts"
label="Large transaction alerts"
description="Get notified when a payment goes over $500."
/>
Label position
Use the label-position prop to put the label before the switch (start) or after it (end, default).
<KSwitch v-model="showsCents" label="Show cents" label-position="start" />
<KSwitch v-model="showsCents" label="Show cents" />
Block
Set block to make the switch fill the width of its container, with the label and the switch at both ends. It suits lists of settings.
<KSwitch
v-model="sendsEmailNotifications"
label="Email notifications"
description="A summary of your spending every Monday morning."
label-position="start"
block
/>
Color
Use the color prop to change the background of the switch once it is on: primary, secondary or neutral (default). It also takes any CSS color, see With a custom color.
<KSwitch v-model="isPrimaryOn" label="Primary" color="primary" />
<KSwitch v-model="isSecondaryOn" label="Secondary" color="secondary" />
<KSwitch v-model="isNeutralOn" label="Neutral" />
Disabled
Use the disabled prop to disable the switch.
<KSwitch v-model="usesBiometricLogin" label="Biometric login" disabled />
<KSwitch v-model="hasAutomaticBackups" label="Automatic backups" disabled />
Examples
With a custom color
Pass any CSS color to the color prop, like a hex code. Use light-dark() to set one color for the light theme and another for the dark theme.
<KSwitch v-model="isSkyBlueOn" label="Sky blue" color="#0ea5e9" />
<KSwitch
v-model="isAmberOrLimeOn"
label="Amber in light theme, lime in dark theme"
color="light-dark(#d97706, #a3e635)"
/>
API
Props
| Prop | Type | Default |
|---|---|---|
label | string | |
description | string | |
modelValue | boolean | false |
color | 'primary' | 'secondary' | 'neutral' | string | 'neutral' |
labelPosition | 'start' | 'end' | 'end' |
block | boolean | false |
disabled | boolean | false |
ui | { root?, base?, thumb?, wrapper?, label?, description? } | |
class | string |
Other attributes, like name or aria-label, go to the <input>.
Emits
| Event | Payload |
|---|---|
update:modelValue | boolean |