📚 MultiSwitch Component

A multi-step switch (3+ options) with Svelte 5 runes, snippets, and generic typing.

Import
 
Basic Usage
 

🔧 Properties

PropertyTypeDefaultDescriptionVersion
selectedIndexnumber0Currently selected index (bindable via bind:selectedIndex)1.0+
itemsreadonly T[] | nullnullArray of options. Generic over T — type flows through to all snippets and callbacks.2.0+ generic
itemsCountnumber0Number of steps (when not deriving from items)1.0+
isDisabledbooleanfalseDisables interaction when true1.0+
orientation"horizontal" | "vertical""horizontal"Layout direction1.0+
size"xs" | "sm" | "md" | "lg" | "xl" | number"md"Named size or numeric pixels. Default changed from 50 to "md" in 2.0.2.0+ named
itemStylesStepStyle | StepStyle[]undefinedPer-step styles. Object applies to every step; array applies per-step (length = items.length).1.0+
shouldDisplayLabelsbooleanfalseShow or hide option labels1.3+
labelPosition"top" | "bottom" | "left" | "right""bottom"Position of labels relative to the switch1.3+
labelRenderMode"absolute" | "block""absolute""block" reserves layout space; "absolute" overlays (needs manual padding)1.4+
labelMemberkeyof T | stringundefinedProperty name to read from each item (e.g., "name" reads item.name)1.4+
labelCallback(item: T | undefined, index: number) => stringundefinedCustom function returning the label text for each item1.4+
onItemChange(index: number) => voidundefinedCallback fired when the selection changes1.0+

❌ Removed in v2.0

  • disableThumbRender — no longer needed; just don't pass the thumb snippet.
  • update() instance method — Svelte 5 props are reactive; mutate the $state object passed to mount().

🎭 Snippets

SnippetContextRoleVersion
thumb{ index, item }Renders once inside the moving thumb. index = active index.2.0+ split
segment{ index, item, isSelected }Renders once per step background. Use for static per-step content.2.0+ split
label{ index, item, isSelected }Custom per-step label content. Renamed from labelTemplate in 2.0.2.0+ renamed
v2.0 snippet rewrite. 1.x had one ambiguous children snippet that ran in BOTH the moving thumb and each step background. 2.0 split it into thumb (one render) + segment (one render per step) so each snippet has one job. labelTemplate was also renamed to label. Snippet context shape unified: currentIndexindex, currentItemitem.

📋 Type Definitions

StepStyle interface
 
Generic items signature
 

💡 Usage Examples

Basic Multi-Option
 
Per-Step Styling
 
label snippet (rich content)
 
thumb + segment snippets
 
labelMember (object property)
 
labelCallback (computed string)
 

🏷️ Label Positioning & Rendering

💡 Recommended: Use labelRenderMode="block" in most cases — it reserves layout space automatically. "absolute" is the legacy default and requires manual padding.
PositionRequired (absolute mode only)Works With
toppadding-top: 3remHorizontal only
bottom (default)padding-bottom: 2.5remHorizontal only
leftpadding-left: 5remVertical only
rightpadding-right: 6–8remVertical only
 

🔤 Label Content (priority system)

Label text resolves through this priority chain (highest to lowest):

  1. label snippet — full custom rendering, takes precedence over everything
  2. labelMember — read text from object property
  3. labelCallback — compute text from item + index
  4. Default"Option 1", "Option 2", ...
💡 Clickable per-step labels: In vertical mode with "left"/"right" label positions, labels render as real <button> elements with keyboard activation. This activates only when no thumb snippet is provided.

🎨 Theming

v2.0 introduced a full theming layer (--base-* cascade + --sw-* per-instance overrides). See the dedicated Theming page for the live preset gallery.

MultiSwitch-specific variables added in 2.0:

  • --sw-step-bg / --sw-step-bg-active — step segment surfaces
  • --sw-label-color / --sw-label-active-color — label colours
  • --sw-label-hover-bg / --sw-label-hover-bg-active — clickable label hover

♿ Accessibility

Built-in Features
  • ✅ Keyboard navigation (Arrow keys, Home, End)
  • ✅ Per-step labels are real <button>s
  • ✅ Focus ring with theme-aware colour
  • ✅ ARIA role + aria-selected
  • ✅ Disabled state handling
Best Practices
  • 🎯 Provide a label or accessible name nearby
  • 🎯 Ensure sufficient colour contrast on themed surfaces
  • 🎯 Test keyboard navigation
  • 🎯 Consider reduced motion preferences

🎯 Common Usage Patterns

Settings Selection
 
Navigation Tabs