Skip to content

ACV Switch

Switch is a component used to toggle between two states, such as on and off. It is a visual representation of a checkbox that allows the user to switch between two states.

  • It used for actions that occur immediately after the user switches the toggle switch to one of the positions.
  • Switch has two basic states - On / Off.
  • Like checkboxes, a switch also has several states: focused, disabled, hover and active.

Basic usage

Bind v-model to a Boolean typed variable.

Source code
vue
<script setup>
  import { ref } from 'vue';
  import AcvSwitch from '@/components/switch/switch.vue';

  const values = ref([true, true, false, false]);
</script>

<template>
  <AcvSwitch v-model="values[0]">
    On
  </AcvSwitch>
  <AcvSwitch
    v-model="values[1]"
    disabled
  >
    On and disabled
  </AcvSwitch>
  <AcvSwitch v-model="values[2]">
    On
  </AcvSwitch>
  <AcvSwitch
    v-model="values[3]"
    disabled
  >
    Off and disabled
  </AcvSwitch>
</template>

Label

You can add a label to the switch component using the label prop.

TIP

You can also use default slot to render the label.

Array

You can use an array to bind multiple switches to a single variable.

Enabled Notifications: []

Source code
vue
<script setup>
  import { ref } from 'vue';
  import AcvSwitch from '@/components/switch/switch.vue';

  const enabledNotifications = ref([]);
</script>

<template>
  <div class="grid-row place-items-stretch">
    <AcvSwitch
      v-model="enabledNotifications"
      value="accountActivity"
      label="accountActivity"
    />
    <AcvSwitch
      v-model="enabledNotifications"
      value="comment"
      label="comment"
    />
    <AcvSwitch
      v-model="enabledNotifications"
      value="like"
      label="like"
    />
    <AcvSwitch
      v-model="enabledNotifications"
      value="mention"
      label="mention"
    />
    <AcvSwitch
      v-model="enabledNotifications"
      value="follow"
      label="follow"
    />

    <p>Enabled Notifications: {{ enabledNotifications }}</p>
  </div>
</template>

Sizes

You can switch between different sizes using the size prop.

See also

Props

Prop nameDescriptionTypeValuesDefault
modelValueBinding value of the Switchboolean-
colorCustom color of the SwitchColorProp-primary
labelSwitch descriptionstring-
sizeSwitch sizeComponentSize-large
disabledWhether the Switch is disabledboolean-

Events

Event namePropertiesDescription
update:modelValueeventName string - The name of the event
value string - Checked state of the Switch
Triggered when the modelValue is updated

Slots

NameDescriptionBindings
defaultThe default slot content

MIT Licensed