Skip to content

Switch Component ​

Switch Component

Switch component is used to represent two mutually opposing states, and users can toggle between states by clicking.

Basic Usage ​

Basic switch usage, bind the switch value through v-model.

Current status: On
Show CodeArrow
CopyGitHub

Disabled State ​

Set the disabled attribute to disable the switch.

Disabled On State:
Disabled Off State:
Show CodeArrow
CopyGitHub

Loading State ​

Set the loading attribute to display loading state.

Loading On State:
Loading Off State:
Show CodeArrow
CopyGitHub

Different Styles ​

Set the radius attribute to change the switch style, supporting default (circular) and square (square).

Default Round Style:
Square Style:
Show CodeArrow
CopyGitHub

Custom Colors ​

Customize the switch background colors through onBgColor and offBgColor attributes.

Custom On Color:
Custom Off Color:
Custom Both On and Off Colors:
Show CodeArrow
CopyGitHub

Different Sizes ​

Switch supports three sizes: small, default, and large.

Small Size:
Default Size:
Large Size:
Show CodeArrow
CopyGitHub

Custom Content ​

Use onContent and offContent slots to customize the switch content.

Show CodeArrow
CopyGitHub

Event Handling ​

Switch provides a change event that is triggered when the state changes.

Listen to change event:
Event Log:
Current value: Off
Show CodeArrow
CopyGitHub

Switch API ​

Attributes ​

AttributeDescriptionTypeAccepted ValuesDefault
v-modelBinding valueboolean——
sizeSizestringsmall / default / largedefault
disabledWhether disabledboolean—false
loadingWhether in loading stateboolean—false
radiusButton border radius typestringdefault / squaredefault
onBgColorBackground color when onstring——
offBgColorBackground color when offstring——

Events ​

Event NameDescriptionParameters
changeTriggered when state changes(value: boolean)

Slots ​

Slot NameDescription
onContentContent when on
offContentContent when off

Style Variables ​

The switch component uses the following style variables, which can be customized through SCSS variables:

Variable NameDescription
$theme-color-primaryTheme color - Primary
$theme-color-dangerTheme color - Danger
$color-whiteWhite color
$color-blackBlack color
$bg-color-defaultDefault background color
$opacity-disabledDisabled state opacity
$font-size-smallSmall font size
$font-size-defaultDefault font size
$font-size-largeLarge font size

Released under the MIT License