Forms
Toggle
A two-state button that stays pressed or unpressed. Ideal for toolbar controls like bold or italic, with full aria-pressed semantics.
Playground
Experiment with different props in real-time.
Basic Usage
Bind the pressed state for two-way control.
<script lang="ts">
import { Toggle } from 'sv5ui';
let pressed = $state(false);
</script>
<Toggle bind:pressed label="Bold" />Icon Only
Pass icon with square. Always add an aria-label.
<Toggle icon="lucide:bold" square aria-label="Bold" />
<Toggle icon="lucide:italic" square aria-label="Italic" />
<Toggle icon="lucide:underline" square aria-label="Underline" />Leading & Trailing Icons
<Toggle leadingIcon="lucide:star" label="Favorite" />
<Toggle trailingIcon="lucide:pin" label="Pin" />Variants
<Toggle variant="ghost" label="Ghost" pressed />
<Toggle variant="outline" label="Outline" pressed />
<Toggle variant="soft" label="Soft" pressed />
<Toggle variant="subtle" label="Subtle" pressed />Colors
The color applies to the pressed state.
<Toggle color="primary" label="Primary" pressed />
<Toggle color="success" label="Success" pressed />
<Toggle color="warning" label="Warning" pressed />
<Toggle color="error" label="Error" pressed />Sizes
<Toggle size="xs" label="XS" pressed />
<Toggle size="sm" label="SM" pressed />
<Toggle size="md" label="MD" pressed />
<Toggle size="lg" label="LG" pressed />
<Toggle size="xl" label="XL" pressed />Block
Stretch to fill the container width.
<Toggle block leadingIcon="lucide:eye" label="Show preview" />Disabled
<Toggle label="Disabled" disabled />
<Toggle label="Disabled on" pressed disabled />Controlled
React to the pressed state to drive other UI.
Sound is on
<script lang="ts">
import { Toggle } from 'sv5ui';
let muted = $state(false);
</script>
<Toggle
bind:pressed={muted}
icon={muted ? 'lucide:volume-x' : 'lucide:volume-2'}
square
color={muted ? 'error' : 'primary'}
aria-label="Toggle sound"
/>
<p>Sound is {muted ? 'muted' : 'on'}</p>UI Slots
Use the ui prop to override classes.
| Slot | Description |
|---|---|
base | The toggle button element |
label | The text label |
leadingIcon | Icon before the label |
trailingIcon | Icon after the label |
Props
| Prop | Type | Default |
|---|---|---|
pressed | boolean | false |
onPressedChange | (pressed: boolean) => void | - |
label | string | - |
icon | string | - |
leadingIcon | string | - |
trailingIcon | string | - |
variant | 'outline' | 'soft' | 'subtle' | 'ghost' | 'ghost' |
color | ColorType | 'primary' |
size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'md' |
block | boolean | false |
square | boolean | false |
disabled | boolean | false |
leadingSlot | Snippet | - |
trailingSlot | Snippet | - |
children | Snippet | - |
ref | HTMLElement | null | null |
class | string | - |
ui | Record<Slot, Class> | - |