Skip to content

Switch

A switch component for toggling between two states.

Basic Usage

The simplest form of switch, using v-model for two-way binding.

vue
<template>
  <div class="switch-group">
    <u-switch v-model="checked1"></u-switch>
    <u-switch v-model="checked2"></u-switch>
  </div>
</template>

<script setup>
  import { ref } from 'vue';

  const checked1 = ref(false);
  const checked2 = ref(true);
</script>

Different Colors

Switch supports multiple theme colors: primary, success, warning, error.

vue
<template>
  <div class="switch-group">
    <u-switch v-model="checked1" color="primary"></u-switch>
    <u-switch v-model="checked2" color="success"></u-switch>
    <u-switch v-model="checked3" color="warning"></u-switch>
    <u-switch v-model="checked4" color="error"></u-switch>
  </div>
</template>

<script setup>
  import { ref } from 'vue';

  const checked1 = ref(true);
  const checked2 = ref(true);
  const checked3 = ref(true);
  const checked4 = ref(true);
</script>

Different Sizes

Switch supports multiple sizes: mini, small, medium, large.

vue
<template>
  <div class="switch-group">
    <u-switch v-model="checked" size="mini"></u-switch>
    <u-switch v-model="checked" size="small"></u-switch>
    <u-switch v-model="checked" size="medium"></u-switch>
    <u-switch v-model="checked" size="large"></u-switch>
  </div>
</template>

<script setup>
  import { ref } from 'vue';

  const checked = ref(true);
</script>

Disabled State

Disable the switch using the disabled property.

vue
<template>
  <div class="switch-group">
    <u-switch v-model="checked1" disabled></u-switch>
    <u-switch v-model="checked2" disabled></u-switch>
  </div>
</template>

<script setup>
  import { ref } from 'vue';

  const checked1 = ref(false);
  const checked2 = ref(true);
</script>

Custom Text

Customize the checked and unchecked text using slots.

vue
<template>
  <u-switch v-model="checked">
    <template #checked>ON</template>
    <template #unchecked>OFF</template>
  </u-switch>
</template>

<script setup>
  import { ref } from 'vue';

  const checked = ref(true);
</script>

Custom Icons

Customize the checked and unchecked icons using slots.

vue
<template>
  <u-switch v-model="checked">
    <template #checked-icon>
      <u-icon name="check"></u-icon>
    </template>
    <template #unchecked-icon>
      <u-icon name="close"></u-icon>
    </template>
  </u-switch>
</template>

<script setup>
  import { ref } from 'vue';

  const checked = ref(true);
</script>

API

Switch Properties

PropertyDescriptionTypeAccepted ValuesDefault
modelValueBinding value, use v-modelboolean-false
disabledWhether disabledboolean-false
sizeSize of the switchstringmini small medium largemedium
colorTheme colorstringprimary success warning error-
checkedChecked textstring--
uncheckedUnchecked textstring--

Switch Events

Event NameDescriptionCallback Parameters
changeTriggered when binding value changes(value: boolean)
update:modelValueUpdate modelValue(value: boolean)

Switch Slots

NameDescription
checkedCustom checked text
uncheckedCustom unchecked text
checked-iconCustom checked icon
unchecked-iconCustom unchecked icon