Switch

Elsio SanchezAbout 3 minComponentAtomSwitchAtomComponent

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.

Viwer Source

Code

```vue
<template>
  <field-switch v-model="value1" />
  <field-switch
    v-model="value2"
    class="ml-2"
    style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
  />
</template>

<script lang="ts" setup>
import { ref } from 'vue'

const value1 = ref(true)
const value2 = ref(true)
</script>

```

:::

Sizes

Open

Open

Open

Viwer Source

Code

```vue
<template>
  <field-switch
    v-model="value"
    size="large"
    active-text="Open"
    inactive-text="Close"
  />
  <br />
  <field-switch v-model="value" active-text="Open" inactive-text="Close" />
  <br />
  <field-switch
    v-model="value"
    size="small"
    active-text="Open"
    inactive-text="Close"
  />
</template>

<script lang="ts" setup>
import { ref } from 'vue'

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

:::

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

Viwer Source

Code

```vue
<template>
  <field-switch
    v-model="value1"
    class="mb-2"
    active-text="Pay by month"
    inactive-text="Pay by year"
  />
  <br />
  <field-switch
    v-model="value3"
    class="mb-2"
    style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
    active-text="Pay by month"
    inactive-text="Pay by year"
  />
  <br />
  <field-switch
    v-model="value4"
    class="ml-2"
    inline-prompt
    style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
    active-text="Y"
    inactive-text="N"
  />
</template>

<script lang="ts" setup>
import { ref } from 'vue'

const value1 = ref(true)
const value2 = ref(true)
const value3 = ref(true)
</script>

```

:::

Display custom icons

Tips

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 iconopen in new window


Viwer Source

Code

```vue
<template>
  <el-switch v-model="value1" :active-icon="Check" :inactive-icon="Close" />
  <br />
  <field-switch
    v-model="value2"
    class="mt-2"
    style="margin-left: 24px"
    inline-prompt
    :active-icon="Check"
    :inactive-icon="Close"
  />
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Check, Close } from '@element-plus/icons-vue'
const value1 = ref(true)
const value2 = ref(true)
</script>
```

:::

Disabled

Adding the disabled attribute disables Switch.

Viwer Source

Code

```vue
<template>
  <field-switch v-model="value1" disabled />
  <br />
  <field-switch v-model="value1" />
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Check, Close } from '@element-plus/icons-vue'
const value1 = ref(true)
const value2 = ref(true)
</script>
```

:::

Loading

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

Viwer Source

Code

```vue
<template>
  <field-switch v-model="value1" loading />
  <field-switch v-model="value2" loading class="ml-2" />
</template>

<script lang="ts" setup>
import { ref } from 'vue'

const value1 = ref(true)
const value2 = ref(false)
</script>

```

:::

Props

NameDescriptionTypeMandatory
valuebinding value, it should be equivalent to either active-value or inactive-value, by default it's boolean typeboolean / string / numberTrue

Attributes

NameDescriptionTypeDefault
active-texttext displayed when in on stateStringβ€”β€”β€”β€”β€”β€”
inactive-texttext displayed when in off stateStringβ€”β€”β€”β€”β€”β€”
inline-promptwhether icon or text is displayed inside dot, only the first character will be rendered for textBooleanFalse
disabledwhether Switch is disabledBooleanFalse
loadingwhether Switch is in loading stateBooleanFalse
sizesize of Switch large / default / smallStringDefault

Example for Developer

Open in StackBlitzopen in new window
Open in StackBlitz

Directory

  └─ src                                            # Main source code.
      └── Components                                # Global components
              └── Atoms                             # Atom components
                  └── FieldSwitch                   # Field Switch specific components.