Switch
Switch component to show a boolean state (similar to a checkbox).
Basic Usage
switchValue1 : false
vue
<template>
<div>
<spr-switch v-model="switchValue1">Switch</spr-switch>
<p>switchValue1: {{ switchValue1 }}</p>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import SprSwitch from '@/components/switch/switch.vue';
const switchValue1 = ref(false);
</script>
Text Label
vue
<!-- Default text position -->
<spr-switch v-model="switchValue2">Left</spr-switch>
<!-- Text position using the rightText slot -->
<spr-switch v-model="switchValue3">
<template #rightText>Right</template>
</spr-switch>
<!-- Text position using both the leftText and rightText slots -->
<spr-switch v-model="switchValue4">
<template #leftText>Left</template>
<template #rightText>Right</template>
</spr-switch>
Disabled
vue
<!-- Declare the disabled property -->
<spr-switch v-model="switchValue5" disabled>
Disabled false switch
</spr-switch>
<!-- or set a value to the disabled property -->
<spr-switch v-model="switchValue6" :disabled="true">
Disabled true switch
</spr-switch>
Emit
The switch component uses @vueuse/core
's useVModel
for properties and emit v-model binding. By default, update:modelValue
emit is defined and can be used to listen for any value changes with modelValue
property.
No event yet.
vue
<template>
<div>
<spr-switch v-model="switchValue7" @update:modelValue="switch7UpdateHandler">Switch</spr-switch>
<p>{{ switch7Label }}</p>
</div>
</template>
<script lang="ts" setup>
import { ref } from 'vue';
import SprSwitch from '@/components/switch/switch.vue';
const switchValue7 = ref(true);
const switch7Label = ref('No event yet.');
const switch7UpdateHandler = (value) => {
switch7Label.value = 'Value changed to ' + value;
};
</script>
Slots
Name | Description | Type |
---|---|---|
rightText | right text label | string |
leftText | left text label | string |
API Reference
Name | Description | Type | Default |
---|---|---|---|
disabled | disable the switch | boolean | false |