📚 Switch Component
A binary switch component for on/off states with Svelte 5 support.
Import
Basic Usage
🔧 Properties
| Property | Type | Default | Description | Version |
|---|---|---|---|---|
checked | boolean | false | Current state of the switch (bindable) | 1.0.0+ |
isDisabled | boolean | false | Disables interaction when true | 1.0.0+ |
orientation | "horizontal" | "vertical" | "horizontal" | Layout direction of the switch | 1.0.0+ |
size | number | 50 | Height in pixels (width for vertical) | 1.0.0+ |
items | any[] | null | null | Array of exactly 2 items for custom content | 1.0.0+ |
itemStyles | StepStyle | StepStyle[] | [] | Custom styling for track and thumb | 1.0.0+ |
onToggle | (checked: boolean) => void | undefined | Callback fired when state changes | 1.0.0+ |
disableThumbRender | boolean | false | Disable default thumb rendering | 1.0.0+ |
🎭 Template Snippets
| Snippet | Parameters | Description | Version |
|---|---|---|---|
children | currentIndex: number | Custom content snippet for switch areas | 1.0.0+ |
thumbTemplate | currentIndex: number | Custom thumb content snippet | 1.2.0+ |
📋 Type Definitions
StepStyle Interface
💡 Usage Examples
Two-way Binding
Custom Styling
Custom Thumb Template
Event Handling
🎨 SCSS Variables
Override these variables before importing the component styles:
♿ Accessibility
Built-in Features
- ✅ Keyboard navigation (Space, Enter)
- ✅ Focus management with visible indicators
- ✅ ARIA attributes (role, aria-checked)
- ✅ Screen reader support
- ✅ Disabled state handling
Best Practices
- 🎯 Provide clear labels or context
- 🎯 Ensure sufficient color contrast
- 🎯 Test with keyboard navigation
- 🎯 Consider reduced motion preferences
- 🎯 Use semantic HTML structure
⚙️ Component Methods
| Method | Signature | Description | Version |
|---|---|---|---|
update | (props: Partial<SwitchProps>) => void | Programmatically update component properties | 1.0.0+ |