Skip to content

Switch

Used to turn options on or off.

Component Types

Basic Usage

v-model is the bound value, default is boolean type.

html
<wd-switch v-model="checked" />
ts
const checked = ref<boolean>(true)

Modify Value

Modify the value when the switch is on via active-value property, and modify the value when the switch is off via inactive-value property.

html
<wd-switch v-model="checked" active-value="Wot" inactive-value="Merchant Backend" />

Component States

Loading State

Setting loading displays the loading state.

html
<wd-switch v-model="checked" loading active-text="Work" inactive-text="Off Work" />

Disabled State

Setting disabled disables the switch.

html
<wd-switch v-model="checked" disabled />

Component Styles

Modify Color

Modify the color when the switch is on via active-color property, and modify the color when the switch is off via inactive-color property.

html
<wd-switch v-model="checked" active-color="#13ce66" inactive-color="#f00" />

Text Description

Set the text inside the switch via active-text and inactive-text.

html
<wd-switch v-model="checked" active-text="Work" inactive-text="Off Work" />

Custom Display Icon

Customize the icon inside the switch via active-icon and inactive-icon.

html
<wd-switch v-model="checked" active-icon="check" inactive-icon="close" />

Custom Action Icon

Customize the button icon via active-action-icon and inactive-action-icon.

html
<wd-switch v-model="checked" active-action-icon="check" inactive-action-icon="close" />

Shape

Set the shape via shape, optional values are round, square.

html
<wd-switch v-model="checked" shape="round" />
<wd-switch v-model="checked" shape="square" />

Custom Size

Set size to modify the switch size.

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

Special Styles

Use with Form

Can be placed in a form item as a right-side control.

html
<wd-form-item title="Use with Form" center>
  <wd-switch v-model="checked" size="20" />
</wd-form-item>

Before Change Hook

Setting before-change property, a hook before modification that receives the target value. Returning false means no modification, supports returning Promise<boolean>.

html
<wd-switch v-model="checked" :before-change="beforeChange" @change="handleChange" />
ts
import { useDialog } from '@/uni_modules/wot-ui'

const message = useDialog()

const beforeChange = (value) => {
  return message.confirm('Switch the toggle?').then(() => true).catch(() => false)
}

Attributes

ParameterDescriptionTypeDefault Value
model-value / v-modelBound valueboolean | string | numberfalse
disabledWhether disabledbooleanfalse
inactive-action-iconInactive state action button iconstring-
active-action-iconActive state action button iconstring-
active-iconActive state icon, ignores active-text when setstring-
inactive-iconInactive state icon, ignores inactive-text when setstring-
active-textActive state textstring''
inactive-textInactive state textstring''
active-valueActive valueboolean | string | numbertrue
inactive-valueInactive valueboolean | string | numberfalse
active-colorActive colorstring-
inactive-colorInactive colorstring-
sizeSwitch sizestring | number-
shapeShape, optional values are round, squareSwitchShape'round'
loadingWhether to show loadingbooleanfalse
loading-propsLoading configurationPartial<LoadingProps>-
before-changeHook before modificationSwitchBeforeChange-
class-prefixIcon class name prefixstring'wd-icon'
custom-classRoot node custom class namestring''
custom-styleRoot node custom stylestring''

Events

Event NameDescriptionParameters
changeValue modification event{ value }

主题定制

CSS 变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 全局配置

名称默认值描述
--wot-switch-color-action-bg$base-white操作块背景色
--wot-switch-color-action-icon$primary-6操作块图标颜色
--wot-switch-color-active-bg$primary-6激活态背景色
--wot-switch-color-inner-active-text$text-white内部文案激活态颜色
--wot-switch-color-inner-inactive-text$text-auxiliary内部文案非激活态颜色
--wot-switch-color-active-icon$icon-white内部图标激活态颜色
--wot-switch-color-inactive-bg$filled-strong非激活态背景色
--wot-switch-color-inactive-icon$icon-auxiliary非激活态图标颜色
--wot-switch-left-right-padding$padding-tight水平内边距
--wot-switch-top-bottom-padding$padding-ultra-tight垂直内边距
--wot-switch-size$n-26组件尺寸
--wot-switch-size-icon0.5em内部图标尺寸
--wot-switch-size-action-height1em操作块高度
--wot-switch-size-action-width1em操作块宽度
--wot-switch-size-action-icon0.5em操作块图标尺寸
--wot-switch-size-inner-heightcalc($switch-size-action-height + $switch-top-bottom-padding * 2)内部容器高度
--wot-switch-size-inner-min-widthcalc(1.8em + $switch-top-bottom-padding * 2)内部容器最小宽度
--wot-switch-inner-font-size$typography-body-size-main内部文案字号
--wot-switch-radius$radius-main方角模式圆角
--wot-switch-radius-full$radius-radius-full圆角模式圆角
--wot-switch-radius-action$radius-small方角模式操作块圆角
--wot-switch-radius-action-full$radius-radius-full圆角模式操作块圆角
--wot-switch-spacing$spacing-super-tight图标与文案间距
--wot-switch-transition-duration0.3s过渡时长
--wot-switch-disabled$opacity-disabled禁用态透明度

Source Code

Documentation
Component

Released under the MIT License.

Released under the MIT License.