Skip to content

Switch - Vant 4

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.

js
import { createApp } from'vue'; import { Switch } from'vant'; const app = createApp(); app.use(Switch);

Usage ​

Basic Usage ​

html
js
import { ref } from'vue'; exportdefault { setup() { const checked = ref(true); return { checked }; }, };

Disabled ​

html

Loading ​

html

Custom Size ​

html

Custom Color ​

html

Custom Node ​

Using node slot to custom the content of the node.

html

Async Control ​

html
js
import { ref } from'vue'; import { showConfirmDialog } from'vant'; exportdefault { setup() { const checked = ref(true); constonUpdateValue = (newValue) => { showConfirmDialog({ title: 'Confirm', message: 'Are you sure to toggle switch?', }).then(() => { checked.value = newValue; }); }; return { checked, onUpdateValue, }; }, };

Inside a Cell ​

html

API ​

Props ​

AttributeDescriptionTypeDefault
v-modelCheck status of Switch*ActiveValueInactiveValue*
loadingWhether to show loading iconbooleanfalse
disabledWhether to disable switchbooleanfalse
sizeSize of switch button*numberstring*
active-colorBackground color when activestring#1989fa

| inactive-color | Background color when inactive | string | rgba(120, 120, 128, 0.16) | | active-value | Value when active | any | true | | inactive-value | Value when inactive | any | false |

Events ​

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

Slots ​

NameDescriptionSlotProps
nodeCustom the content of node-
backgroundCustom the background of switch-

Types ​

The component exports the following type definitions:

ts
importtype { 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-size26px-
--van-switch-widthcalc(1.8em + 4px)-
--van-switch-heightcalc(1em + 4px)-
--van-switch-node-size1em-
--van-switch-node-backgroundvar(--van-white)-
--van-switch-node-shadow0 3px 1px 0 rgba(0, 0, 0, 0.05)-
--van-switch-backgroundrgba(120, 120, 128, 0.16)-
--van-switch-on-backgroundvar(--van-primary-color)-
--van-switch-durationvar(--van-duration-base)-
--van-switch-disabled-opacityvar(--van-disabled-opacity)-

Enterprise-level mobile solution based on Vant