Switch

Material Design 3 switch component for toggling between on and off states

Switch

Switches toggle the state of a single item on or off. @duskmoon-dev/core provides a Material Design 3 switch component with smooth animations and multiple variants.

Basic Usage

Simply add the .switch class to an <input type="checkbox">:

Basic Switch

With Label

Wrap the switch in a label for better UX:

Switch with Label

Checked State

Checked Switch

Sizes

Five sizes are available:

Switch Sizes

Color Variants

Primary (Default)

Primary Switch

Secondary

Secondary Switch

Tertiary

Tertiary Switch

Success

Success Switch

Warning

Warning Switch

Error

Error Switch

Info

Info Switch

All Colors Comparison

All Color Variants

States

Disabled

Disabled Switches

Switch Groups

Vertical Group

Vertical Switch Group

Horizontal Group

Horizontal Switch Group

Form Integration

Switch in Form

Privacy Settings

Best Practices

When to Use

Switches are best for:

  • Instant changes: Settings that take effect immediately
  • On/off states: Binary choices like enable/disable features
  • System settings: Preferences and configurations

When Not to Use

Avoid switches for:

  • Form submission: Use checkboxes instead when changes require user confirmation
  • Multiple options: Use radio buttons or select dropdowns
  • Actions with delay: If the change requires processing time, consider a button

Accessibility

  • Use semantic <label> elements to associate labels with switches
  • Provide descriptive label text that explains what the switch controls
  • Support keyboard navigation (built-in with native inputs)
  • Ensure sufficient color contrast in both states

Touch Targets

The switch provides adequate touch targets. For mobile-first designs, use larger sizes:

Mobile-Friendly Switch

API Reference

Class Names

ClassDescription
.switchBase switch styling (required, on input element)
.switch-xsExtra small size (2rem width)
.switch-smSmall size (2.5rem width)
.switch-mdMedium size (3.25rem width, default)
.switch-lgLarge size (4rem width)
.switch-xlExtra large size (4.75rem width)
.switch-primaryPrimary color variant (default)
.switch-secondarySecondary color variant
.switch-tertiaryTertiary color variant
.switch-successSuccess color variant
.switch-warningWarning color variant
.switch-errorError color variant
.switch-infoInfo color variant
.switch-labelLabel wrapper for switch + text
.switch-groupContainer for vertical switch group
.switch-group-horizontalHorizontal layout modifier

HTML Structure

Simple switch:

<input type="checkbox" class="switch" />

With label:

<label class="switch-label">
  <input type="checkbox" class="switch" />
  <span>Label text</span>
</label>

Switch group:

<div class="switch-group">
  <label class="switch-label">
    <input type="checkbox" class="switch" name="option1" />
    <span>Option 1</span>
  </label>
  <label class="switch-label">
    <input type="checkbox" class="switch" name="option2" />
    <span>Option 2</span>
  </label>
</div>

States

StateHow to Apply
Checkedchecked attribute
Disableddisabled attribute
FocusAutomatic via :focus-visible
  • Checkbox - Multi-select form control
  • Radio - Single selection input
  • Button - For action triggers