Forms
Checkbox
Chooses any number of options, submitted with a form.
Installation
npx sinaui add checkboxCopy src/lib/ui/Checkbox.svelte, Icon.svelte and tokens.css. The tick and dash come from @hugeicons/core-free-icons.
Examples
With a hint
The hint is read out as the checkbox's description.
Manuscripts must be back within forty days.
<script lang="ts">
import Checkbox from '#lib/ui/Checkbox.svelte';
let agreed = $state(false);
</script>
<Checkbox bind:checked={agreed} hint="Manuscripts must be back within forty days.">
I'll return the manuscript to the House of Wisdom
</Checkbox>Select all
The parent shows a dash, announced as “mixed”, when only some of its children are chosen.
<script lang="ts">
import Checkbox from '#lib/ui/Checkbox.svelte';
const works = $state([
{ title: 'The Canon of Medicine', chosen: true },
{ title: 'The Book of Optics', chosen: false },
{ title: 'Al-Jabr', chosen: false }
]);
const count = $derived(works.filter((w) => w.chosen).length);
</script>
<div class="list">
<Checkbox
checked={count === works.length}
indeterminate={count > 0 && count < works.length}
onchange={(e) => works.forEach((w) => (w.chosen = e.currentTarget.checked))}
>
Borrow all works ({count} of {works.length})
</Checkbox>
<div class="children">
{#each works as work (work.title)}
<Checkbox bind:checked={work.chosen}>{work.title}</Checkbox>
{/each}
</div>
</div>
<style>
.list,
.children {
display: grid;
gap: 0.5rem;
}
.children {
padding-inline-start: 1.75rem;
}
</style>Disabled
<script lang="ts">
import Checkbox from '#lib/ui/Checkbox.svelte';
</script>
<Checkbox disabled>Sent for binding</Checkbox>Disabled and checked
Shows a choice that can't be changed here.
<script lang="ts">
import Checkbox from '#lib/ui/Checkbox.svelte';
</script>
<Checkbox disabled checked>Cataloged by the librarian</Checkbox>Props
| Prop | Type | Default |
|---|---|---|
checked | boolean, bindable | false |
indeterminate | boolean, bindable | false |
hint | string | |
children | Snippet: the visible label | |
| …rest | Any <input> attribute. | |
Accessibility
- A real
<input type="checkbox">: keyboard, forms, reset and label clicks need no JavaScript. - The box outline keeps at least 3:1 contrast even when unchecked, since nothing else shows it's there.
- The hit area is 44px even though the box is 18px, so it's easy to tap.
| Key | Action |
|---|---|
| Space | Toggles the checkbox. |