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.

SlotDescription
baseThe toggle button element
labelThe text label
leadingIconIcon before the label
trailingIconIcon after the label

Props

PropTypeDefault
pressedbooleanfalse
onPressedChange(pressed: boolean) => void-
labelstring-
iconstring-
leadingIconstring-
trailingIconstring-
variant'outline' | 'soft' | 'subtle' | 'ghost''ghost'
colorColorType'primary'
size'xs' | 'sm' | 'md' | 'lg' | 'xl''md'
blockbooleanfalse
squarebooleanfalse
disabledbooleanfalse
leadingSlotSnippet-
trailingSlotSnippet-
childrenSnippet-
refHTMLElement | nullnull
classstring-
uiRecord<Slot, Class>-