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

Checkbox

Checkbox with an optional label next to it. It renders a native <input type="checkbox"> inside a <label>, so a click on the text toggles it: name, value, required, disabled and events (onchange, onclick, ...) work as in HTML. indeterminate shows the partial state of a "select all"; a click clears it, as in HTML. To know whether Shift was held (range selection), read event.shiftKey in onclick: it works for a click on the box, and in Chromium also for the Space key and a click on the text (Firefox reports Shift+Space without Shift and does not toggle the box on a Shift+click on the text). Its state is exposed as data-checked, data-indeterminate and data-disabled for app CSS.

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

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

Basic

bind:checked keeps the state in sync. A click on the label toggles the box; labelSnippet takes any content, such as a link.

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

	let reminders = $state(true);
	let newsletter = $state(false);
	let terms = $state(false);
</script>

<div class="list">
	<Checkbox label="Send SMS reminders" bind:checked={reminders} />
	<Checkbox label="Monthly newsletter" bind:checked={newsletter} />
	<Checkbox bind:checked={terms} name="terms" required>
		{#snippet labelSnippet()}
			I accept the <a href="#terms">terms of service</a>
		{/snippet}
	</Checkbox>
	<Checkbox label="Disabled" disabled />
	<Checkbox label="Disabled and checked" checked disabled />
</div>

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

Select all and Shift range

indeterminate shows a partial "select all". In onclick, event.shiftKey tells whether Shift was held while clicking (in Chromium also with the Space key): here Shift selects the whole range from the last clicked row.

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

	const clients = ['Luca Rossi', 'Giulia Marini', 'Sara Bianchi', 'Marco Conti', 'Elena Ferri'];

	let selected = $state<string[]>(['Giulia Marini']);
	let last = $state<number>();

	let all = $derived(selected.length === clients.length);
	let some = $derived(selected.length > 0 && !all);

	function toggleAll() {
		selected = all ? [] : [...clients];
	}

	function toggle(index: number, event: MouseEvent) {
		const name = clients[index];
		const on = !selected.includes(name);
		const range =
			event.shiftKey && last !== undefined
				? clients.slice(Math.min(last, index), Math.max(last, index) + 1)
				: [name];
		selected = on
			? [...new Set([...selected, ...range])]
			: selected.filter((c) => !range.includes(c));
		last = index;
	}
</script>

<div class="list">
	<Checkbox label="All clients" checked={all} indeterminate={some} onchange={toggleAll} />
	<div class="rows">
		{#each clients as client, i (client)}
			<Checkbox
				label={client}
				checked={selected.includes(client)}
				onclick={(event) => toggle(i, event)}
			/>
		{/each}
	</div>
</div>

<style>
	.list {
		display: grid;
		gap: 12px;
	}

	.rows {
		display: grid;
		justify-items: start;
		gap: 10px;
		padding-inline-start: 28px;
	}
</style>

Props

NameTypeDefaultDescription
checked bindable booleanChecked state.
indeterminate bindable booleanPartial state, for example a "select all" with only some rows selected. A click clears it and toggles checked.
label stringText next to the box. A click on it toggles the checkbox.
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 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 box (for example to add a link). A click on it still toggles the checkbox.

CSS variables

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

Per-instance variableDefault
--checkbox-size
--checkbox-default-size 18px
--checkbox-gap
--checkbox-default-gap 10px
--checkbox-label-color
--checkbox-default-label-color var(--global-color-text)
--checkbox-label-font-size
--checkbox-default-label-font-size 14px
--checkbox-label-font-weight
--checkbox-default-label-font-weight 400
--checkbox-label-line-height
--checkbox-default-label-line-height 1.5
--checkbox-border-width
--checkbox-default-border-width 1.5px
--checkbox-border-color
--checkbox-default-border-color var(--global-color-border-strong)
--checkbox-border-radius
--checkbox-default-border-radius var(--global-radius-xs)
--checkbox-background
--checkbox-default-background var(--global-input-background)
--checkbox-box-shadow
--checkbox-default-box-shadow 0 0 #0000
--checkbox-duration
--checkbox-default-duration var(--global-duration)
--checkbox-mark-thickness
--checkbox-default-mark-thickness 2px
--checkbox-mark-color
--checkbox-default-mark-color var(--global-color-on-primary)
--checkbox-easing
--checkbox-default-easing var(--global-ease-spring)
--checkbox-checked-border-color
--checkbox-default-checked-border-color transparent
--checkbox-checked-background
--checkbox-default-checked-background var(--global-fill-primary)
--checkbox-checked-box-shadow
--checkbox-default-checked-box-shadow var(--global-shadow-fill)
--checkbox-hover-border-color
--checkbox-default-hover-border-color var(--global-color-primary)
--checkbox-focus-ring-width
--checkbox-default-focus-ring-width var(--global-focus-ring-width)
--checkbox-focus-ring-color
--checkbox-default-focus-ring-color var(--global-focus-ring-color)
--checkbox-focus-ring-offset
--checkbox-default-focus-ring-offset var(--global-focus-ring-offset)
--checkbox-disabled-opacity
--checkbox-default-disabled-opacity 0.4