Skip to content

Switch Toggle Selector Component ​

The switch selector is used for toggling between the on and off states.

📌 Platform Compatibility ​

APP(vue)H5WeChat Mini ProgramAlipay Mini Program
✔✔✔✔

🏯 Basic Usage Examples ​

html
<!-- Global usage -->
<hy-switch v-model="value" @change="change"></hy-switch>
ts
import { ref } from 'vue';

const value = ref(false);
const change = (e) => {
    console.log('change', e);
};

Loading ​

html
<template>
    <hy-switch v-model="value" loading></hy-switch>
</template>
<script setup>
    import { ref } from 'vue';

    const value = ref(false);
</script>

Loading ​

Set the loading attribute (default is true) to put the switch into a loading state; in this state the switch cannot be operated. You can dynamically set the loading state via :loading='loading'

html
<template>
    <hy-switch v-model="value" loading></hy-switch>
</template>
<script setup>
    import { ref } from 'vue';

    const value = ref(true);
</script>

Disable Switch ​

Set the disabled attribute (default is true) to disable the component so that users cannot click it. There are two disabled states:

  • First, disabled while off: in this case only a white area is displayed.
  • Second, disabled after being turned on: in this case an opacity transparency is applied over the original color, but the switch still cannot be operated.
html
<template>
    <hy-switch v-model="value" disabled></hy-switch>
</template>
<script setup>
    import { ref } from 'vue';

    const value = ref(false);
</script>

Custom Size ​

  • Set the switch size by setting size to a number or to small, medium, or large
    • small: small switch
    • medium: medium switch
    • large: large switch
    • number: number
html
<template>
    <hy-switch v-model="value" size="small"></hy-switch>
    <hy-switch v-model="value" size="medium"></hy-switch>
    <hy-switch v-model="value" size="large"></hy-switch>
    <hy-switch v-model="value" :size="28"></hy-switch>
</template>
<script setup>
    import { ref } from 'vue';

    const value = ref(true);
</script>

Custom Color ​

html
<template>
    <hy-switch v-model="value" activeColor="#f56c6c"></hy-switch>
    <hy-switch v-model="value" activeColor="red"></hy-switch>
    <hy-switch v-model="value" activeColor="#rgb(0,0,0)"></hy-switch>
</template>
<script setup>
    import { ref } from 'vue';

    const value = ref(true);
</script>

Custom Icon ​

html
<template>
    <hy-switch
        v-model="value"
        icon-color="red"
        :active-icon="IconConfig.SUCCESS"
        :inactive-icon="IconConfig.CLOSE"
    ></hy-switch>
    <!-- Use your own vector icon library -->
    <hy-switch
        v-model="value"
        icon-color="red"
        :active-icon="open"
        :inactive-icon="close"
        :icon="{ customPrefix: 'icon' }"
    ></hy-switch>
</template>
<script setup>
    import { ref } from 'vue';
    import { IconConfig } from '@hy-app/ui';

    const value = ref(true);
</script>

Custom Slot ​

html
<template>
    <hy-switch v-model="value">
        <view style="font-size: 16rpx">Disabled</view>
    </hy-switch>
</template>
<script setup>
    import { ref } from 'vue';

    const value = ref(true);
</script>

Asynchronous Control ​

Asynchronous control scenarios typically occur when the user turns the selector on or off, and a local or backend request is needed to determine whether the user is allowed to turn it on or off. You can also use them in combination, for example adding the disabled or loading attributes based on the API result

Note

Please add the asyncChange attribute to support asynchronous control; otherwise, the value bound to v-model will be updated first and you will lose control

html
<template>
    <hy-switch v-model="value" asyncChange @change="asyncChange"></hy-switch>
</template>
<script setup>
    import { ref } from 'vue';

    const value = ref(false);
    const asyncChange = (e) => {
        uni.showModal({
            content: e ? 'Are you sure you want to turn it on?' : 'Are you sure you want to turn it off?',
            success: (res) => {
                if (res.confirm) {
                    value.value = e;
                }
            },
        });
    };
</script>

API ​

Switch Props ​

ParameterDescriptionTypeDefault
v-modelValue bound via v-model (two-way)boolean|string|numberfalse
loadingWhether it is in a loading statebooleanfalse
disabledWhether to disablebooleanfalse
sizeSwitch size, in rpxsmall|medium|large| string|numbermedium
activeColorBackground color when onstring-
inactiveColorBackground color when offstring-
activeValueValue of the switch when onboolean|string|numbertrue
inactiveValueValue of the switch when offboolean|string|numberfalse
activeIconIcon shown when the selector is onstring-
inactiveIconIcon shown when the selector is offstring-
iconIcon API collection, see Icon API for detailsHyIconProps-
asyncChangeWhether to enable asynchronous change; when enabled, the input value must be controlled manuallybooleanfalse
spaceDistance between the dot and the outer borderstring|number0
customStyleCustom styleCSSProperties-

Events ​

Event NameDescriptionCallback Parameters
changeTriggered when the switch is turned on or offvalue: the activeValue when on, the inactiveValue when off

Slots ​

Slot NameDescriptionAccepted Value
defaultDefault slot-

CSS Variables ​

Variable NameDescriptionDefault
--hy-switch-border-colorBorder colorrgba(0, 0, 0, 0.12)
--hy-switch-node-box-shadowDot shadow1px 1px 1px 0 rgba(0, 0, 0, 0.25)
--hy-switch-disabled-opacityDisabled opacity0.6
--hy-switch-radiusOverall border radius of the switchFollows --hy-radius-semicircle (100px)
12:33
battery