Skip to content

Switch

Indicates a switch between two opposing states. Used to trigger on/off.

Basic Usage

Bind v-model to a Boolean variable. The --es-switch-on-color and --es-switch-off-color properties are used to set the background color of the switch.

<template>
    <Switch v-model="value" />
</template>

<script setup>
import { ref } from 'vue'
import Switch from '@/components/Switch/Switch.vue'
const value = ref(true)
</script>

Size

Set the size property to accept large/small and render different sizes.

<script setup>
import { ref } from 'vue'
import Switch from '@/components/Switch/Switch.vue'
const test = ref(false)
</script>
<template>
  <div class="switch-size-container">
    <Switch v-model="test" size="large"/>
    <Switch v-model="test"/>
    <Switch v-model="test" size="small"/>
  </div>
</template>
<style scoped>
.switch-size-container {
  display: flex;
  align-items: center;
  .es-switch {
    margin-right: 10px;
  }
}
</style>

Disabled State

Set the disabled property, which accepts a boolean and is set to true to disable it.

Normal:

Disabled:
<script setup>
import { ref } from 'vue'
import Switch from '@/components/Switch/Switch.vue'
const test = ref(false)
const test2 = ref(false)
</script>
<template>
  Normal: <Switch v-model="test" /> <br/>
  Disabled: <Switch v-model="test2" disabled/>
</template>

Display Inner Text

Use the active-text and inactivity-text properties to set the text description of the switch.

ON
<template>
    <Switch v-model="value" activeText="ON" inactiveText="OFF"/>
</template>

<script setup>
import { ref } from 'vue'
import Switch from '@/components/Switch/Switch.vue'
const value = ref(true)
</script>

Custom Active/Inactive Value

You can set active-value and inactivity-value properties, which accept values of type boolean | string | number.

model-value: right

<template>
    <Switch v-model="test" activeValue="right" inactiveValue="wrong" />
    <h4>model-value: {{ test }}</h4>
</template>
<script setup>
import { ref } from 'vue'
import Switch from '@/components/Switch/Switch.vue'
const test = ref('right')
</script>

Switch Attributes

NameDescriptionTypeDefault
modelValuecurrent valueany
disabledwhether to disable the switchbooleanfalse
activeValuevalue when switch is activeanytrue
inactiveValuevalue when switch is inactiveanyfalse
namenative input namestring
activeTexttext when activestring
inactiveTexttext when inactivestring
sizeswitch size (e.g. 'small', 'large')string

Switch Events

NameDescriptionType
changetriggers when switch value changes(value: any) => void
update:modelValuetriggers when value changes (for v-model)(value: any) => void