Skip to content

Switch

Overview

The Switch component provides an interface for a toggleable option, often used to represent on/off or active/inactive states. This component is built on top of the @headlessui/vue library, and it's designed for easy customization and a wide range of configurations.

Storybook

Props

PropTypeDefaultDescription
modelValueBooleanfalseThe value representing the switch's state (on/off).
labelString''Label text for the switch.
colorColors (from ./colors)'primary'Color theme for the switch.
inactiveClassString''Class to be applied when the switch is in the inactive state.
activeClassString''Class to be applied when the switch is in the active state.
buttonClassString''Class to be applied to the switch's button.
wrapperClassString''Class to be applied to the component wrapper.
switchClassString''Class to be applied to the main switch component.
switchGroupClassString''Class to be applied to the switch group wrapper.
labelClassString''Class to be applied to the switch label.
nameString''Name attribute for the switch.
rulesString''Validation rules for the switch.
errorClassString''Class to be applied when there is an error related to the switch.
hintString''Hint or help text displayed below the switch.
size'sm' | 'md' | 'lg' | string'md'Size of the switch. Can be 'sm', 'md', 'lg', or any custom string.
errorMessageString''Error message displayed when there is a validation error related to the switch.

Events

EventPayload TypeDescription
update:modelValueBooleanEmits the current state (on/off) of the switch.

Slots

SlotPropsDescription
hint{}Custom hint content. If not provided, the hint prop will be used.
error{}Custom error content. If not provided, the errorMessage prop will be used.

Usage

To use the Switch component:

  1. Import the component.
  2. Bind the necessary props.
  3. Optionally, use the slots for custom content.
vue
<script setup lang="ts">
import {ref} from 'vue';
import {Switch} from '@morpheme/switch';

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

<template>
  <Switch v-model="isActive" label="Activate Feature" color="green" size="lg" />
</template>
<script setup lang="ts">
import {ref} from 'vue';
import {Switch} from '@morpheme/switch';

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

<template>
  <Switch v-model="isActive" label="Activate Feature" color="green" size="lg" />
</template>

This will render a large green switch labeled "Activate Feature". The switch's state (on or off) will be bound to the isActive ref.