Switch

Switch is used for switching between two opposing states.

Basic usage

Bind v-model to a Boolean typed variable. The --el-switch-on-color and --el-switch-off-color CSS variables decides the background color in two states.

Sizes

Open

Open

Open

Text description

You can add active-text and inactive-text attribute to show texts. use inline-prompt attribute to control text is displayed inside dot.

You can add active-text and inactive-text attribute to show texts.

Pay by month

Pay by month

Y
超出省略
完整展示多个内容

Display custom icons

TIP

Use the active-icon and inactive-icon attribute to add icon. You can pass either string for the component name (registered in advance) or the component itself which is a SVG Vue component. Element Plus has provided a set of icon that you can find at icon

You can add active-icon and inactive-icon attribute to show icons. use inline-prompt attribute to control icon is displayed inside dot.


Extended value types

You can set active-value and inactive-value attributes. They both receive a Boolean, String or Number typed value.

Disabled

Adding the disabled attribute disables Switch.

Loading

Setting the loading attribute to true indicates a loading state on the Switch.

prevent switching

set the before-change property, If false is returned or a Promise is returned and then is rejected, will stop switching.

custom action icon 2.3.9

You can add active-action-icon and inactive-active-icon attribute to show icons.

custom action slot 2.4.4

You can use active-action and inactive-action slot to customize action.

T

API

Attributes

NameDescriptionTypeDefault
model-value / v-modelbinding value, it should be equivalent to either active-value or inactive-value, by default it's boolean typeboolean / string / numberfalse
disabledwhether Switch is disabledbooleanfalse
loadingwhether Switch is in loading statebooleanfalse
sizesize of Switchenum''
widthwidth of Switchnumber / string''
inline-promptwhether icon or text is displayed inside dot, only the first character will be rendered for textbooleanfalse
active-iconcomponent of the icon displayed when in on state, overrides active-textstring / Component
inactive-iconcomponent of the icon displayed when in off state, overrides inactive-textstring / Component
active-action-icon 2.3.9component of the icon displayed in action when in on statestring / Component
inactive-action-icon 2.3.9component of the icon displayed in action when in off statestring / Component
active-texttext displayed when in on statestring''
inactive-texttext displayed when in off statestring''
active-valueswitch value when in on stateboolean / string / numbertrue
inactive-valueswitch value when in off stateboolean / string / numberfalse
nameinput name of Switchstring''
validate-eventwhether to trigger form validationbooleantrue
before-changebefore-change hook before the switch state changes. If false is returned or a Promise is returned and then is rejected, will stop switchingboolean / Function
idid for inputstring
tabindextabindex for inputstring / number
aria-label a11y 2.7.2same as aria-label in native inputstring
active-color deprecatedbackground color when in on state ( use CSS var --el-switch-on-color instead )string''
inactive-color deprecatedbackground color when in off state ( use CSS var --el-switch-off-color instead )string''
border-color deprecatedborder color of the switch ( use CSS var --el-switch-border-color instead )string''
label a11y deprecatedsame as aria-label in native inputstring

Events

NameDescriptionType
changetriggers when value changesFunction

Switch Slots

NameDescription
active-action 2.4.4customize active action
inactive-action 2.4.4customize inactive action

Exposes

MethodDescriptionType
focusmanual focus to the switch componentFunction

Source

ComponentStyleDocs

Contributors