Skip to content
iv-ui v0.3.0

Switch

since 0.1.0

An on/off control that applies immediately.

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

Basic

A labelled switch with a bound state.

Send anonymous telemetry

telemetry on

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

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

<div class="stack">
	<Switch label="Send anonymous telemetry" bind:checked={telemetry} />
	<p class="readout">telemetry {telemetry ? 'on' : 'off'}</p>
</div>

<style>
	.stack {
		display: flex;
		flex-direction: column;
		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>
  • Bind checked; changes apply without a submit step.
  • Use label for the visible and accessible name.
  • Prefer Checkbox when the choice is part of a form that is submitted later.

Sizes

sm and md keep the same hit area.

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

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

<div class="stack">
	<Switch label="Small" size="sm" bind:checked={a} />
	<Switch label="Medium" bind:checked={b} />
</div>

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

Disabled

The switch is removed from interaction but keeps its value.

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

<div class="stack">
	<Switch label="Locked by policy" checked disabled />
	<Switch label="Available" />
</div>

<style>
	.stack {
		display: flex;
		flex-direction: column;
		gap: var(--iv_spacing-sm);
	}
</style>
import { Switch } from '@elluzion/iv-ui';

Props

4 props · 3 events · 0 snippets
PropTypeDefaultDescription
checked bindablebooleanfalseOn/off state. Bind with bind:checked.
disabled booleanfalseDisables interaction and applies the disabled treatment.
label string''Label rendered next to the switch.
size 'sm' | 'md''md'Control size.

class, id, style, aria-*, and data-* are forwarded through rest props.

Events

EventHandler typeDescription
onchange(e: CustomEvent<{ checked: boolean }>) => voidCalled with { checked } after the state changes.
onfocus(e: FocusEvent) => voidCalled when the control receives focus.
onblur(e: FocusEvent) => voidCalled when the control loses focus.

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

KeysAction
TabMoves focus to the switch.
Space / EnterToggles the switch.
  • The control exposes role="switch" with aria-checked.
  • The label is linked through aria-labelledby.
  • Focus is drawn on the track and is never removed.
Class hooks
.iv-checked.iv-disabled.iv-label.iv-md.iv-root.iv-sm.iv-switch.iv-thumb
iv-ui
↑ ↓ navigate ↵ open esc close minisearch · 482 records