Switch
A headless toggle switch component that provides accessible, flexible on/off switching functionality. Supports standalone boolean state or multi-selection within groups, similar to checkboxes but with distinct toggle UI semantics.Features
- Dual-mode operation: Standalone with v-model or group multi-selection
- Visual components: Separate Track and Thumb components for flexible styling
- Tri-state support: On, off, and indeterminate states (in group mode)
- Full accessibility: Uses
role="switch"with proper ARIA attributes - Form integration: Automatic hidden input generation for native form submission
- Select all: Built-in component for bulk toggling in groups
- Flexible rendering: Works with any element via
asprop or renderless mode
Basic Usage
Switch Group
Components
Switch.Root
The main switch component. Supports standalone mode with v-model or group mode withinSwitch.Group.
The on/off state (standalone mode only)
Value associated with this switch (used in group mode and form submission)
Form field name. When provided, automatically renders a hidden input for form submission
Disables the switch, preventing interaction
Sets the indeterminate/mixed state (standalone mode)
Optional display label (passed to slot props)
Unique identifier (auto-generated if not provided)
Associate with a form element by ID
HTML element to render as
Render only slot content without wrapper element
Context namespace for child components
Namespace for connecting to parent group
Slot Props
Whether the switch is on
Whether the switch is in indeterminate state
Whether the switch is disabled
Function to turn the switch on
Function to turn the switch off
Function to toggle switch state
Pre-computed ARIA and data attributes including
role="switch" and aria-checkedSwitch.Group
Container for managing multiple switches with multi-selection support.Array of enabled switch values
Disables all switches in the group
Auto-enable all non-disabled switches on mount
false: No enforcementtrue: Prevents disabling the last enabled switch'force': Auto-enables first non-disabled switch
Accessible name for the group
HTML element to render as
Render only slot content without wrapper
Context namespace for child switches
Slot Props
Whether the group is disabled
Whether no switches are enabled
Whether all selectable switches are enabled
Whether some but not all switches are enabled
Enable all non-disabled switches
Disable all switches
Toggle between all enabled and none enabled
Switch.Track
The background track/rail of the switch. Always visible, provides styling container for the thumb.HTML element to render as
Render only slot content without wrapper
Context namespace to inject from parent
Slot Props
Whether the switch is on
Whether the switch is indeterminate
Contains
data-state for CSS stylingSwitch.Thumb
The sliding knob/button of the switch. Position indicates on/off state.HTML element to render as
Render only slot content without wrapper
Context namespace to inject from parent
Slot Props
Whether the switch is on
Whether the switch is indeterminate
Contains
data-state for CSS stylingUnlike Checkbox.Indicator, the Switch.Thumb is always visible. Use CSS transitions on
data-state to animate its position.Switch.SelectAll
Special switch for enabling/disabling all switches in a group. Must be used withinSwitch.Group.
Accessible label for the select all switch
Disables the select all switch
HTML element to render as
Render only slot content without wrapper
Context namespace for child components
Namespace for connecting to parent group
Slot Props
Whether all switches are enabled
Whether some but not all switches are enabled
Whether the select all is disabled
Enable all switches
Toggle all switches
Switch.HiddenInput
Hidden input for native form submission. Automatically rendered whenname prop is provided to Switch.Root.
Form field name (required)
Value to submit when enabled (overrides context value)
Associate with form by ID (overrides context form)
Context namespace to inject from parent
Advanced Examples
Styled Switch with Animation
Settings Panel
Renderless Mode
Accessibility
- Uses
role="switch"witharia-checkedfor proper screen reader support - Supports keyboard navigation (Space to toggle)
- Manages
tabindexfor proper focus management - Provides
aria-disabledwhen disabled - Group uses
role="group"with optionalaria-label - Announces current state (“on” or “off”) to screen readers
Keyboard Support
- Space: Toggle the switch
- Enter: Toggle the switch (when rendered as button)
Switch vs Checkbox
While both components support similar functionality, they have different semantic meanings:| Aspect | Switch | Checkbox |
|---|---|---|
| Semantic | On/off action | Selection state |
| ARIA role | switch | checkbox |
| Typical use | Settings, features | Form selections, lists |
| Feedback | Immediate effect | Deferred until submission |
| Visual | Track + thumb | Box + checkmark |
- The change takes immediate effect (like toggling dark mode)
- Controlling a single feature or setting
- The action is clearly “on” or “off”
- Selecting items from a list
- Form submission is involved
- Multiple independent selections