import{o as a,a as t,y as n}from"./vue-libs.b44bc779.js";const e={class:"van-doc-markdown-body"},l=n(`

Switch

Intro

Used to switch between open and closed states.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

import { createApp } from 'vue';
import { Switch } from 'vant';

const app = createApp();
app.use(Switch);

Usage

Basic Usage

<van-switch v-model="checked" />
import { ref } from 'vue';

export default {
  setup() {
    const checked = ref(true);
    return { checked };
  },
};

Disabled

<van-switch v-model="checked" disabled />

Loading

<van-switch v-model="checked" loading />

Custom Size

<van-switch v-model="checked" size="24px" />

Custom Color

<van-switch v-model="checked" active-color="#ee0a24" inactive-color="#dcdee0" />

Custom Node

Using node slot to custom the content of the node.

<van-switch v-model="checked">
  <template #node>
    <div class="icon-wrapper">
      <van-icon :name="checked ? 'success' : 'cross'" />
    </div>
  </template>
</van-switch>

<style>
  .icon-wrapper {
    display: flex;
    width: 100%;
    justify-content: center;
    font-size: 18px;
  }

  .icon-wrapper .van-icon-success {
    line-height: 32px;
    color: var(--van-blue);
  }

  .icon-wrapper .van-icon-cross {
    line-height: 32px;
    color: var(--van-gray-5);
  }
</style>

Async Control

<van-switch :model-value="checked" @update:model-value="onUpdateValue" />
import { ref } from 'vue';
import { Dialog } from 'vant';

export default {
  setup() {
    const checked = ref(true);
    const onUpdateValue = (newValue) => {
      Dialog.confirm({
        title: 'Confirm',
        message: 'Are you sure to toggle switch?',
      }).then(() => {
        checked.value = newValue;
      });
    };

    return {
      checked,
      onUpdateValue,
    };
  },
};

Inside a Cell

<van-cell center title="Title">
  <template #right-icon>
    <van-switch v-model="checked" size="24" />
  </template>
</van-cell>

API

Props

AttributeDescriptionTypeDefault
v-modelCheck status of SwitchActiveValue | InactiveValuefalse
loadingWhether to show loading iconbooleanfalse
disabledWhether to disable switchbooleanfalse
sizeSize of switchnumber | string30px
active-colorBackground color when activestring#1989fa
inactive-colorBackground color when inactivestringwhite
active-valueValue when activeanytrue
inactive-valueValue when inactiveanyfalse

Events

EventDescriptionParameters
changeEmitted when check status changedvalue: any
clickEmitted when component is clickedevent: MouseEvent

Slots

NameDescriptionSlotProps
node v3.5.0Custom the content of node-
background v3.5.0Custom the background of switch-

Types

The component exports the following type definitions:

import type { SwitchProps } from 'vant';

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-switch-size30px-
--van-switch-width2em-
--van-switch-height1em-
--van-switch-node-size1em-
--van-switch-node-background-colorvar(--van-white)-
--van-switch-node-box-shadow0 3px 1px 0 rgba(0, 0, 0, 0.05)-
--van-switch-background-colorvar(--van-background-color-light)-
--van-switch-on-background-colorvar(--van-primary-color)-
--van-switch-transition-durationvar(--van-animation-duration-base)-
--van-switch-disabled-opacityvar(--van-disabled-opacity)-
--van-switch-bordervar(--van-border-width-base) solid rgba(0, 0, 0, 0.1)-
`,19),c=[l],i={__name:"README",setup(d,{expose:s}){return s({frontmatter:{}}),(o,r)=>(a(),t("div",e,c))}};export{i as default};