📚 MultiSwitch Component
A multi-option switch component for selecting between multiple choices with Svelte 5 support.
Import
Basic Usage
🔧 Properties
| Property | Type | Default | Description | Version |
|---|---|---|---|---|
selectedIndex | number | 0 | Currently selected index (bindable) | 1.0.0+ |
items * | any[] | required | Array of options to display | 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+ |
itemStyles | StepStyle | StepStyle[] | [] | Custom styling for each option | 1.0.0+ |
shouldDisplayLabels | boolean | false | Show/hide option labels | 1.3.0+ |
labelPosition | "top" | "bottom" | "left" | "right" | "bottom" | Position of labels relative to switch | 1.3.0+ |
labelRenderMode | "absolute" | "block" | "absolute" | Label rendering mode - absolute (may overlap) or block (takes space) | 1.4.0+ |
labelMember | string | undefined | Property name to extract label text from items (e.g., "name" reads item.name) | 1.4.0+ |
labelCallback | (item: any, index: number) => string | undefined | Custom function to generate label text with item and index access | 1.4.0+ |
onItemChange | (index: number) => void | undefined | Callback fired when selection changes | 1.0.0+ |
disableThumbRender | boolean | false | Disable default thumb rendering | 1.0.0+ |
* Required property
🎭 Template Snippets
| Snippet | Parameters | Description | Version |
|---|---|---|---|
children | currentIndex: number | Custom content snippet for option areas | 1.0.0+ |
thumbTemplate | currentIndex: number | Custom thumb content snippet | 1.2.0+ |
labelTemplate | currentIndex: number | Custom label template snippet | 1.3.0+ |
📋 Type Definitions
StepStyle Interface
💡 Usage Examples
Basic Multi-Option
Custom Styling per Option
Custom Label Template
Vertical with Custom Content
🏷️ Label Positioning & Rendering
💡 New in v1.4.0: Use
labelRenderMode="block" for automatic spacing,
or stick with labelRenderMode="absolute" (default) for precise control with manual padding.⚠️ Absolute Mode: Labels are absolutely positioned and don't reserve space in the layout.
Add appropriate padding to prevent overlap:
| Position | Required Padding | Works With |
|---|---|---|
top | padding-top: 3rem | Horizontal & Vertical |
bottom | padding-bottom: 2.5rem | Horizontal & Vertical |
left | padding-left: 5rem | Vertical only |
right | padding-right: 6-8rem | Vertical only |
🔤 Label Content Features (v1.4.0+)
Advanced label text handling with automatic extraction from object properties or custom functions.
Label Priority System
- labelMember - Extract text from object property
- labelCallback - Custom function with item and index access
- labelTemplate - Full custom rendering control
- Default - "Option 1", "Option 2", etc.
Object Property Extraction
Custom Label Function
💡 Clickable Labels: In vertical mode with left/right label positions,
labels become clickable when no
thumbTemplate is defined. Click any label to jump directly to that option!🎨 SCSS Variables
Override these variables before importing the component styles:
♿ Accessibility
Built-in Features
- ✅ Keyboard navigation (Arrow keys, Home, End)
- ✅ Focus management with visible indicators
- ✅ ARIA attributes (role, aria-selected)
- ✅ Screen reader support
- ✅ Disabled state handling
- ✅ Proper tab order
Best Practices
- 🎯 Provide clear labels or context
- 🎯 Ensure sufficient color contrast
- 🎯 Test with keyboard navigation
- 🎯 Consider reduced motion preferences
- 🎯 Use semantic option descriptions
- 🎯 Test with screen readers
⚙️ Component Methods
| Method | Signature | Description |
|---|---|---|
update | (props: Partial<MultiSwitchProps>) => void | Programmatically update component properties |