Skip to content
Likable Aurora @likable-hair/svelte 5

Switch

On/off toggle with an optional label next to it. It renders a native <input type="checkbox" role="switch"> inside a <label>, so a click on the text toggles it and screen readers announce it as a switch: name, value, required, disabled and events (onchange, ...) work as in HTML. Two sizes: md and lg. Its state is exposed as data-checked, data-size and data-disabled for app CSS.

import { Switch } from '@likable-hair/svelte';

src/lib/components/simple/forms/Switch.svelte · Markdown for LLMs

Basic

bind:checked keeps the state in sync and a click on the label toggles it. Two sizes: md and lg.

Code
<script lang="ts">
	import { Switch } from '@likable-hair/svelte';

	let online = $state(true);
	let holiday = $state(false);
	let large = $state(true);
</script>

<div class="list">
	<Switch label="Online booking" bind:checked={online} />
	<Switch label="Holiday mode" bind:checked={holiday} />
	<Switch label="Large" size="lg" bind:checked={large} />
	<Switch label="Disabled" disabled />
	<Switch label="Disabled and on" checked disabled />
</div>

<style>
	.list {
		display: grid;
		justify-items: start;
		gap: 14px;
	}
</style>

Settings list

onchange receives the native event: event.currentTarget.checked is the new value. labelSnippet adds a description under the title, and a class on the container moves the switch to the right.

Toggle a setting.

Code
<script lang="ts">
	import { Switch } from '@likable-hair/svelte';

	let settings = $state([
		{ key: 'sms', title: 'SMS reminders', description: 'Sent 24 hours before the appointment.', on: true },
		{ key: 'email', title: 'Email receipts', description: 'A copy of every payment.', on: false },
		{ key: 'reviews', title: 'Review requests', description: 'Asked the day after the visit.', on: true }
	]);
	let log = $state('');
</script>

<div class="settings">
	{#each settings as setting (setting.key)}
		<Switch
			class={{ container: 'setting' }}
			name={setting.key}
			bind:checked={setting.on}
			onchange={(event) => (log = `${setting.title}: ${event.currentTarget.checked ? 'on' : 'off'}`)}
		>
			{#snippet labelSnippet()}
				<span class="title">{setting.title}</span>
				<span class="description">{setting.description}</span>
			{/snippet}
		</Switch>
	{/each}
	<p>{log || 'Toggle a setting.'}</p>
</div>

<style>
	.settings {
		display: grid;
		gap: 16px;
		max-width: 380px;
	}

	.settings :global(.setting) {
		display: flex;
		flex-direction: row-reverse;
		justify-content: space-between;
	}

	.title,
	.description {
		display: block;
	}

	.description {
		font-size: 12.5px;
		opacity: 0.7;
	}

	p {
		margin: 0;
		font-size: 13px;
	}
</style>

Props

NameTypeDefaultDescription
checked bindable booleanOn or off.
size 'md' | 'lg''md'Track size: md 36×20px, lg 46×26px.
label stringText next to the switch. A click on it toggles the switch.
disabled booleanfalseNative disabled.
input bindable HTMLInputElementThe native <input> element.
class { container?: string; input?: string; label?: string; }{}Extra classes for each part: the root <label>, the input (which draws the track) and the text.

It also accepts the native attributes and events of <input> (for example id, title, aria-label, onkeydown), which are forwarded to the element.

Snippets

NameParametersDescription
labelSnippet{ label: string | undefined }Replaces the text next to the switch. A click on it still toggles the switch.

CSS variables

For a single instance, pass them as style props: <Switch --switch-background="tomato">. The --switch-default-* defaults apply to the whole app: override them in :root or in a theme.

Per-instance variableDefault
--switch-gap
--switch-default-gap 12px
--switch-label-color
--switch-default-label-color var(--global-color-text)
--switch-label-font-size
--switch-default-label-font-size 14px
--switch-label-font-weight
--switch-default-label-font-weight 400
--switch-label-line-height
--switch-default-label-line-height 1.5
--switch-width
--switch-default-width 36px
--switch-default-lg-width 46px
--switch-height
--switch-default-height 20px
--switch-default-lg-height 26px
--switch-thumb-size
--switch-default-thumb-size 16px
--switch-default-lg-thumb-size 22px
--switch-border-width
--switch-default-border-width var(--global-border-width)
--switch-border-color
--switch-default-border-color var(--global-color-border-strong)
--switch-border-radius
--switch-default-border-radius var(--global-radius-full)
--switch-background
--switch-default-background var(--global-color-surface-3)
--switch-box-shadow
--switch-default-box-shadow 0 0 #0000
--switch-duration
--switch-default-duration var(--global-duration-slow)
--switch-thumb-border-radius
--switch-default-thumb-border-radius var(--global-radius-full)
--switch-thumb-color
--switch-default-thumb-color var(--global-color-on-primary)
--switch-thumb-box-shadow
--switch-default-thumb-box-shadow var(--global-shadow-sm), 0 2px 5px color-mix(in oklab, var(--global-color-overlay) 70%, transparent)
--switch-easing
--switch-default-easing var(--global-ease-spring)
--switch-checked-border-color
--switch-default-checked-border-color transparent
--switch-checked-background
--switch-default-checked-background var(--global-fill-primary)
--switch-checked-box-shadow
--switch-default-checked-box-shadow var(--global-shadow-fill)
--switch-checked-thumb-color
--switch-default-checked-thumb-color var(--global-color-on-primary)
--switch-translate-x
--switch-hover-border-color
--switch-default-hover-border-color var(--global-color-primary)
--switch-focus-ring-width
--switch-default-focus-ring-width var(--global-focus-ring-width)
--switch-focus-ring-color
--switch-default-focus-ring-color var(--global-focus-ring-color)
--switch-focus-ring-offset
--switch-default-focus-ring-offset var(--global-focus-ring-offset)
--switch-disabled-opacity
--switch-default-disabled-opacity 0.4