Switch
Learn how to use the Switch component in your application.
Overview
The Switch component is a toggleable UI element that provides a visual way to turn options on or off. It's commonly used for boolean settings and preferences in forms and settings panels.
Features
- Multiple sizes (xs to xl)
- Color variants (primary and neutral)
- Support for custom icons in checked/unchecked states
- Loading state with spinner
- Optional label and description
- Required field indicator
- RTL support
- Accessible through keyboard navigation
Usage
Basic Switch
The simplest way to use the switch is with v-model binding:
<template>
<div class="flex items-center gap-4">
<MSwitch v-model="value1" />
<MSwitch v-model="value2" />
</div>
</template>
<script setup>
const value1 = ref(false)
const value2 = ref(true)
</script>
With Label and Description
Add context to your switch with labels and descriptions:
Receive notifications about important updates
Get the latest news and offers
<template>
<div class="space-y-4">
<MSwitch
v-model="notifications"
label="Notifications"
description="Receive notifications about important updates"
/>
<MSwitch
v-model="marketing"
label="Marketing emails"
description="Get the latest news and offers"
/>
</div>
</template>
Sizes
Choose from five different sizes:
<template>
<div class="flex flex-col gap-4">
<MSwitch size="xs" label="Extra Small" />
<MSwitch size="sm" label="Small" />
<MSwitch size="md" label="Medium" />
<MSwitch size="lg" label="Large" />
<MSwitch size="xl" label="Extra Large" />
</div>
</template>
Colors
Switch between primary and neutral colors:
<template>
<div class="flex flex-col gap-4">
<MSwitch color="primary" label="Primary Color" />
<MSwitch color="neutral" label="Neutral Color" />
</div>
</template>
States
Switches support various states including loading, disabled, and required:
<template>
<div class="flex flex-col gap-4">
<MSwitch loading label="Loading" />
<MSwitch disabled label="Disabled" />
<MSwitch required label="Required Field" />
</div>
</template>
Custom Icons
Add custom icons for checked and unchecked states:
<template>
<MSwitch
v-model="darkMode"
label="Dark Mode"
checkedIcon="i-lucide-moon"
uncheckedIcon="i-lucide-sun"
/>
</template>
Props
The current state of the switch.
Text label displayed next to the switch.
Additional text displayed below the label.
The color scheme of the switch.
The size of the switch.
When true, prevents the switch from being toggled.
When true, displays a required field indicator (*).
When true, displays a loading spinner.
Custom icon to display in loading state.
Icon to display when the switch is checked.
Icon to display when the switch is unchecked.
Additional CSS classes to apply to the root element.
Additional CSS classes to apply to the label.
Additional CSS classes to apply to the icons.
Slots
Custom content for the label. Receives { label }
prop.
Custom content for the description. Receives { description }
prop.
Events
Emitted when the switch state changes.
Emitted when the switch value changes. Required for v-model binding.
Best Practices
- Use clear and concise labels:
- Make it obvious what the switch controls
- Use positive statements (e.g., "Show notifications" instead of "Don't show notifications")
- Choose appropriate sizes:
- Use
md
size by default - Use larger sizes (
lg
,xl
) for touch interfaces - Use smaller sizes (
xs
,sm
) for dense UIs
- Use
- Provide immediate feedback:
- Use loading state for async operations
- Add descriptions for complex options
- Consider using icons to reinforce meaning
- Accessibility considerations:
- Always provide labels for screen readers
- Use required prop when the field is mandatory
- Ensure sufficient color contrast
- Layout guidelines:
- Group related switches together
- Maintain consistent spacing
- Align labels for better readability