Skip to content

Switch

A switch component used to toggle between two states.

Basic Usage

Bind a boolean variable using v-model.

在 github 中打开
展开代码
复制代码
<script lang="ts" setup>
import { ref } from 'vue'

const checked = ref(false)
</script>

<template>
  <ol-switch v-model="checked" />
</template>

Disabled State

Specify whether the Switch component is disabled using the disabled attribute.

在 github 中打开
展开代码
复制代码
<template>
  <ol-switch disabled />
</template>

Size

Provides three sizes for the switch: large, default, and small.

在 github 中打开
展开代码
复制代码
<script lang="ts" setup>
import { ref } from 'vue'

const checked = ref(false)
</script>

<template>
  <div class="flex flex-col gap-2">
    <ol-switch v-model="checked" size="large" />
    <ol-switch v-model="checked" size="default" />
    <ol-switch v-model="checked" size="small" />
  </div>
</template>

Before Switch Hook

Use the before-switch attribute to execute specific logic before switching. This function needs to return a Promise.

在 github 中打开
展开代码
复制代码
<script lang="ts" setup>
function handleBeforeSwitch(): Promise<boolean> {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true)
    }, 1000)
  })
}
</script>

<template>
  <ol-switch :before-switch="handleBeforeSwitch" />
</template>

Attributes

Attribute NameDescriptionTypeDefault Value
v-modelBound valuebooleanfalse
disabledWhether disabledbooleanfalse
sizeSwitch size'large' | 'default' | 'small''default'
checkedWhether checkedbooleanfalse
before-switchHook function before switching() => Promise<boolean>() => Promise.resolve(true)

Events

Event NameDescriptionCallback Parameters
changeTriggered when the state changes(value: boolean)
clickTriggered when clicked(value: boolean)