Forms

Switch

A toggle control for binary settings. Use switches for immediate state changes like enabling features, toggling preferences, or activating modes.

Playground

Experiment with different props in real-time.

Basic Usage

Import and use the Switch component with bind:checked for two-way binding.

<script lang="ts">
  import { Switch } from 'sv5ui';

  let enabled = $state(false);
</script>

<Switch bind:checked={enabled} />

With Label & Description

Add a label and description to provide context for the setting.

Receive alerts when someone mentions you.

Use dark theme across the application.

Help us improve by sending anonymous usage data.

<Switch
  bind:checked={notifications}
  label="Push Notifications"
  description="Receive alerts when someone mentions you."
/>

<Switch
  bind:checked={darkMode}
  label="Dark Mode"
  description="Use dark theme across the application."
/>

<Switch
  bind:checked={analytics}
  label="Usage Analytics"
  description="Help us improve by sending anonymous usage data."
/>

Sizes

5 sizes from extra small to extra large.

<Switch size="xs" label="Extra Small" bind:checked={xs} />
<Switch size="sm" label="Small" bind:checked={sm} />
<Switch size="md" label="Medium" bind:checked={md} />
<Switch size="lg" label="Large" bind:checked={lg} />
<Switch size="xl" label="Extra Large" bind:checked={xl} />

Colors

8 semantic color schemes, visible when the switch is checked.

<Switch color="primary" label="Primary" checked />
<Switch color="secondary" label="Secondary" checked />
<Switch color="tertiary" label="Tertiary" checked />
<Switch color="success" label="Success" checked />
<Switch color="warning" label="Warning" checked />
<Switch color="error" label="Error" checked />
<Switch color="info" label="Info" checked />
<Switch color="surface" label="Surface" checked />

With Icons

Display icons on the thumb for checked and unchecked states using Iconify names.

<Switch
  bind:checked={wifi}
  label="Wi-Fi"
  checkedIcon="lucide:wifi"
  uncheckedIcon="lucide:wifi-off"
/>

<Switch
  bind:checked={sound}
  label="Sound"
  checkedIcon="lucide:volume-2"
  uncheckedIcon="lucide:volume-x"
/>

<Switch
  bind:checked={visibility}
  label="Visibility"
  checkedIcon="lucide:eye"
  uncheckedIcon="lucide:eye-off"
/>

Loading

Show a loading spinner on the thumb while an async operation is in progress.

<Switch label="Syncing settings..." loading checked />
<Switch label="Connecting..." loading />

Disabled

Prevent interaction while preserving the current state.

<Switch label="Enabled (disabled)" checked disabled />
<Switch label="Disabled (disabled)" disabled />

UI Slots

Use the ui prop to override classes on specific internal elements of the Switch.

SlotDescription
rootOutermost wrapper — controls overall layout and spacing
baseSwitch track element — controls track shape, background, border
containerInner container wrapping the track and thumb
thumbThe sliding circle — controls thumb size, shadow, position
wrapperWrapper around label and description text
labelLabel text element — controls font, weight, color
descriptionDescription text element — controls helper text styles
iconIcon inside the thumb — controls icon size and color

With overridden slot classes

<Switch
  label="Custom Styled"
  description="With overridden slot classes"
  checked
  ui={{
    base: 'bg-success',
    thumb: 'bg-white shadow-lg',
    label: 'font-bold tracking-wide',
    description: 'italic'
  }}
/>

Snippets

Use snippets to customize label and description rendering.

SnippetArgsDescription
labelSlot{ label }Custom label rendering
descriptionSlot{ description }Custom description rendering

Props

PropTypeDefault
checkedbooleanfalse
onCheckedChange(checked: boolean) => void-
colorColor'primary'
size'xs' | 'sm' | 'md' | 'lg' | 'xl''md'
loadingbooleanfalse
loadingIconstringicons.loading
checkedIconstring-
uncheckedIconstring-
labelstring-
descriptionstring-
idstring-
namestring-
valuestring-
disabledbooleanfalse
requiredbooleanfalse
classstring-
uiRecord<Slot, Class>-