Skip to content

Switch

The Switch component provides an elegant toggle interface for binary choices with smooth animations, accessibility support, and multiple size and color variants.

Basic Usage

With Label

With Description

Receive email updates about your account activity

Add an extra layer of security to your account

Sizes

Compact size for dense layouts

Default size for most use cases

Larger size for better touch targets

Intent Variants

Required Field

You must accept our terms to continue

Disabled State

This option is currently unavailable

This setting cannot be changed

Form Integration

Receive updates via email

Receive push notifications on your device

Allow others to find and view your profile

Accessibility

The Switch component follows WCAG 2.1 AA guidelines:

  • Keyboard Navigation: Full support for Space and Enter keys
  • Screen Reader Support: Proper role and state announcements
  • Focus Management: Clear focus indicators and logical tab order
  • Touch Targets: Minimum 44×44 pixel touch targets
  • State Indication: Clear visual and programmatic state changes
  • Form Integration: Proper form association and value handling
  • Required Fields: Clear indication of required switches
  • Descriptions: Proper association with helper text

Properties

PropertyTypeDefaultDescription
checkedbooleanfalseWhether the switch is checked
disabledbooleanfalseWhether the switch is disabled
requiredbooleanfalseWhether the switch is required
labelstringundefinedLabel text for the switch
descriptionstringundefinedHelper text below the label
size"small" | "medium" | "large""medium"Size of the switch
intent"default" | "primary" | "success" | "warning" | "danger""default"Color variant
namestringundefinedName attribute for form submission
valuestringundefinedValue attribute for form submission
classstringundefinedAdditional CSS classes
idstringauto-generatedUnique identifier for the switch

Events

The Switch component emits the following events:

change

Standard change event fired when the switch state changes.

switchChange (Custom)

Custom event with detailed information about the switch change:

switchElement.addEventListener('switchChange', (event) => {
console.log('Switch changed:', {
checked: event.detail.checked,
value: event.detail.value,
name: event.detail.name,
element: event.detail.element
})
})

Features

Smooth Animations

The switch features smooth transitions for state changes with easing functions that feel natural and responsive.

Touch-Friendly

All switch sizes meet or exceed the minimum 44×44 pixel touch target size for mobile accessibility.

Form Integration

The switch properly integrates with forms using standard input attributes like name, value, and required.

Keyboard Support

Enhanced keyboard support beyond the standard checkbox behavior, including Space and Enter key handling.

Custom Events

Emits custom events for advanced use cases like state management and analytics tracking.

Examples

Settings Panel

---
import Switch from '@uicg/cli/components/Switch/switch.astro'
---
<div class="space-y-6">
<h3 class="text-lg font-semibold">Notification Settings</h3>
<Switch
name="email_notifications"
label="Email notifications"
description="Receive email updates about your account activity"
checked
/>
<Switch
name="push_notifications"
label="Push notifications"
description="Get notified on your device for important updates"
/>
<Switch
name="marketing_emails"
label="Marketing emails"
description="Receive promotional content and product updates"
/>
</div>

Feature Toggles

<div class="space-y-4">
<Switch
intent="success"
label="Enable dark mode"
description="Switch to a darker color scheme"
/>
<Switch
intent="primary"
label="Enable beta features"
description="Try out new experimental features"
/>
<Switch
intent="warning"
label="Developer mode"
description="Enable advanced debugging tools"
/>
</div>

Privacy Settings

<form>
<div class="space-y-6">
<Switch
name="profile_visibility"
value="public"
label="Public profile"
description="Make your profile visible to other users"
required
/>
<Switch
name="search_indexing"
value="enabled"
label="Search engine indexing"
description="Allow search engines to index your profile"
/>
<Switch
name="data_sharing"
value="analytics"
label="Analytics data sharing"
description="Help improve our service by sharing usage data"
intent="warning"
/>
</div>
</form>