# 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.

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

Category: simple. Source: `src/lib/components/simple/forms/Checkbox.svelte`.

## Examples

### Basic

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

```svelte
<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.

```svelte
<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

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `checked` (bindable) | `boolean` |  | Checked state. |
| `indeterminate` (bindable) | `boolean` |  | Partial state, for example a "select all" with only some rows selected. A click clears it and toggles `checked`. |
| `label` | `string` |  | Text next to the box. A click on it toggles the checkbox. |
| `disabled` | `boolean` | `false` | Native `disabled`. |
| `input` (bindable) | `HTMLInputElement` |  | The 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

| Name | Parameters | Description |
| --- | --- | --- |
| `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 variable | Default |
| --- | --- |
| `--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` |
