Skip to content

Toggle Component

This is a toggle button component that allows users to switch between two states, similar to a switch but with labels instead of a sliding knob.


How to Use the Toggle Component

To use this component, bind v-model to a reactive boolean variable:

vue
<Toggle v-model="isActive" />

Props

Prop NameTypeDefault ValueDescription
modelValuebooleanfalseThe current state of the toggle (on/off).
onTextstring"On"Text to display when the toggle is active.
offTextstring"Off"Text to display when the toggle is inactive.
widthstring"60px"Width of the toggle button.
disablebooleanfalseIf true, the toggle is disabled and cannot be clicked.

Example Usage

Basic Example

vue
<template>
  <div>
    <h2>Toggle Button</h2>
    <Toggle v-model="isToggled" onText="Subscribed" offText="Not Subscribed" width="250px"/>
    <p>Toggle is: {{ isToggled ? 'Active' : 'Inactive' }}</p>
  </div>
</template>

<script setup>
import { ref } from 'vue';

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

Screenshot

toggle image


Troubleshooting

Toggle does not update when clicked

  • Ensure v-model is bound to a ref variable.
  • Make sure disable is not set to true.
  • Check for any console errors.