Skip to content
iv-ui v0.3.0

Toggle

since 0.2.0

A pressable button that keeps a checked state.

import { Toggle } from '@elluzion/iv-ui';

Basic

A labelled toggle with a bound state.

pinned yes

Source· svelte Show Hide
<script lang="ts">
	import { Toggle } from '@elluzion/iv-ui';

	let pinned = $state(true);
</script>

<div class="stack">
	<Toggle bind:checked={pinned}>Pin to dashboard</Toggle>
	<p class="readout">pinned {pinned ? 'yes' : 'no'}</p>
</div>

<style>
	.stack {
		display: flex;
		flex-direction: column;
		align-items: start;
		gap: var(--iv_spacing-sm);
	}

	.readout {
		margin: 0;
		font-family: var(--iv_font-mono);
		font-size: var(--iv_text-sm);
		color: var(--iv_foreground-dim);
	}
</style>
  • Use Toggle for formatting and view modes; use Switch for settings that save immediately.
  • Pass visible children or a label for the accessible name.
  • icon renders before the children; the check mark appears when checked.

Icons

Icons inherit the current text color and size step.

Source· svelte Show Hide
<script lang="ts">
	import { IconBold, IconItalic, IconUnderline } from '@tabler/icons-svelte';
	import { Toggle } from '@elluzion/iv-ui';

	let bold = $state(true);
	let italic = $state(false);
	let underline = $state(false);
</script>

<div class="row">
	<Toggle bind:checked={bold} label="Bold"><IconBold size={14} /></Toggle>
	<Toggle bind:checked={italic} label="Italic"><IconItalic size={14} /></Toggle>
	<Toggle bind:checked={underline} label="Underline"><IconUnderline size={14} /></Toggle>
</div>

<style>
	.row {
		display: flex;
		gap: var(--iv_spacing-sm);
	}
</style>

Sizes

sm, md, and lg share the same shape.

Source· svelte Show Hide
<script lang="ts">
	import { Toggle } from '@elluzion/iv-ui';

	let a = $state(true);
	let b = $state(true);
	let c = $state(true);
</script>

<div class="row">
	<Toggle size="sm" bind:checked={a}>Small</Toggle>
	<Toggle bind:checked={b}>Medium</Toggle>
	<Toggle size="lg" bind:checked={c}>Large</Toggle>
</div>

<style>
	.row {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
		gap: var(--iv_spacing-sm);
	}
</style>
import { Toggle } from '@elluzion/iv-ui';

Props

4 props · 2 events · 2 snippets
PropTypeDefaultDescription
checked bindablebooleanfalsePressed state. Bind with bind:checked.
disabled booleanfalseDisables interaction and applies the disabled treatment.
label string''Accessible name. Visible text can be passed as children instead.
size 'sm' | 'md' | 'lg''md'Control size.

All native <button> attributes are forwarded.

Events

EventHandler typeDescription
onclick(e: MouseEvent) => voidCalled when the element is activated.
onchange(e: CustomEvent<{ checked: boolean }>) => voidCalled with { checked } after the state changes.

The legacy on:name form is accepted for every event.

Snippets

SnippetTypeDescription
iconSnippetIcon rendered inside the toggle.
childrenSnippetContent rendered inside the component.
KeysAction
TabMoves focus to the toggle.
Space / EnterToggles the pressed state.
  • The toggle is a native button with aria-pressed.
  • With no visible text, pass label so the control has an accessible name.
  • Focus uses the standard button ring.
Class hooks
.iv-check.iv-checked.iv-disabled.iv-icon.iv-lg.iv-sm.iv-toggle
iv-ui
↑ ↓ navigate ↵ open esc close minisearch · 482 records