Checkbox
A tri-state checkable control.
Anatomy
<script>
import { Checkbox } from '@shardsui/svelte/checkbox'
</script>
<Checkbox.Root>
<Checkbox.Indicator />
</Checkbox.Root>Usage guidelines
- Form controls must have an accessible name: give the checkbox one by wrapping it in a
<label>, or witharia-label/aria-labelledby.
Examples
Labeling a checkbox
The simplest way to name a checkbox is to wrap it in a <label>:
<label>
<Checkbox.Root>
<Checkbox.Indicator>
<!-- checkmark icon -->
</Checkbox.Indicator>
</Checkbox.Root>
Accept terms and conditions
</label>Checkbox.Root renders a <span> by default so the wrapping <label> toggles the hidden <input type="checkbox"> natively.
Rendering as a native button
When you point a separate label at the checkbox with for/id instead of wrapping it, render it as a native button with as="button":
<div>
<label for="notifications-checkbox">Enable notifications</label>
<Checkbox.Root id="notifications-checkbox" as="button">
<Checkbox.Indicator />
</Checkbox.Root>
</div>Form integration
Field wires the label and form association:
<Form>
<Field.Root name="stayLoggedIn">
<Field.Label>
<Checkbox.Root />
Stay logged in for 7 days
</Field.Label>
</Field.Root>
</Form>API reference
Root
Represents the checkbox itself.
Renders a <span> element and a hidden <input> beside.
Indicator
Indicates whether the checkbox is ticked.
Renders a <span> element.
Inherits the same data attributes as Root, plus: