Skip to content
On this page

Switch

Switch is used for switching between two opposing states.

Basic usage

Bind 'v-model' to a variable of type 'Boolean'.

<>
<template>
    <kl-switch v-model="value1"></kl-switch>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value1 = ref(true)
const value2 = ref(false)
</script>

<style lang="scss" scoped>
* {
    margin-left: 5px;
}
</style>

Color settings

You can use the 'active-color' property and the 'inactive-color' property to set the background color of the switch.

CloseOpen
CloseOpen
<>
<template>
    <kl-switch v-model="value2" activeText="Open" inactiveText="Close"></kl-switch>
    <kl-switch
        v-model="value1"
        active-color="red"
        inactive-color="blue"
        active-text="Open"
        inactive-text="Close"
    ></kl-switch>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value1 = ref(true)
const value2 = ref(false)
</script>

<style lang="scss" scoped>
* {
    display: block;
}
</style>

Size

Use the size property to change the size of the switch. In addition to the default size, there are two other options: large, small.

CloseOpen
CloseOpen
CloseOpen
<>
<template>
    <kl-switch v-model="value" size="large" active-text="Open" inactive-text="Close"></kl-switch>
    <kl-switch v-model="value" size="default" active-text="Open" inactive-text="Close"></kl-switch>
    <kl-switch v-model="value" size="small" active-text="Open" inactive-text="Close"></kl-switch>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value = ref(true)
</script>

<style lang="scss" scoped>
* {
    display: block;
}
</style>

Disabled state

Add the 'disabled' attribute to set the switch to the disabled state.

CloseOpen
CloseOpen
<>
<template>
    <kl-switch v-model="value1" active-text="Open" inactive-text="Close"></kl-switch>
    <kl-switch v-model="value2" disabled active-text="Open" inactive-text="Close"></kl-switch>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value1 = ref(true)
const value2 = ref(false)
</script>

<style lang="scss" scoped>
* {
    display: block;
}
</style>

Text description

Text description

The UnforgottenYasuo
<>
<template>
    <kl-switch v-model="value1"></kl-switch>
    <kl-switch v-model="value2" activeText="Yasuo" inactiveText="The Unforgotten"></kl-switch>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value1 = ref(true)
const value2 = ref(false)
</script>

<style lang="scss" scoped>
* {
    display: block;
}
</style>

Bind the data type

Use the 'active-value' attribute and the 'inactive-value' property to set the text description of the switch.

value:

The UnforgottenYasuo

value:

999111
<>
<template>
    <h3>value:{{ value }}</h3>
    <kl-switch
        v-model="value"
        activeText="Yasuo"
        inactiveText="The Unforgotten"
        activeValue="Yasuo"
        inactiveValue="The Unforgotten"
    ></kl-switch>
    <hr />
    <h3>value:{{ value1 }}</h3>
    <kl-switch
        v-model="value1"
        activeText="111"
        inactiveText="999"
        activeValue="111"
        inactiveValue="999"
    ></kl-switch>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value = ref()
const value1 = ref()
</script>

<style lang="scss" scoped>
* {
    display: block;
}
</style>

Display the custom icon

Use the inactive-icon and active-icon properties to add icons. Use the inline-prompt property to control the display of icons within points.

<>
<template>
    <kl-switch v-model="value1"></kl-switch>
    <kl-switch v-model="value2" activeIcon="KlOtherCorrect" inactiveIcon="KlOtherError"></kl-switch>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const value1 = ref(true)
const value2 = ref(false)
</script>

<style lang="scss" scoped>
* {
    display: block;
}
</style>

API

Attribute

nameDescriptionTypeThe default value is
v-modelBinding valueboolean
disabledWhether to disablebooleanfales
active-valueThe value when the switch state is onboolean / string / numbertrue
inactive-valueThe value when the status of the switch is offboolean / string / numberfalse
active-iconThe icon displayed when the switch state is on, usingwith Icon string
inactive-iconThe icon displayed when the switch state is off, usingwith Icon string
active-textThe text description when the switch is opened isstring
inactive-textThe text description when the switch is turned offstring
active-colorWhen in the on state, the background colorstring
inactive-colorThe background color of the off state isstring
nameEquivalent to the native input name attributestring

Event

NameDescriptionCallback parameters
changeTriggered only when the modelValue changesval

Released under the MIT License.