Checkbox
Data inputA checkbox with bind:checked, and bind:indeterminate for a partly checked group. With name it submits with a form. Inside a Field it takes its id, label, description, error and disabled state from the Field; use orientation="horizontal" to put the label beside it.
import { Checkbox } from '@limerdev/zest'; Use for
An on/off choice that takes effect when a form is submitted, or one of several independent options.
Don't
A setting that applies at once: use
Switch. Choosing one of several options: use RadioGroup. A Checkbox without a label.Examples
Basic
Put a Checkbox and its label in a horizontal Field; clicking the label toggles it.
<script lang="ts">
import { Checkbox, Field } from '@limerdev/zest';
let billable = $state(true);
</script>
<Field.Root orientation="horizontal" class="w-auto">
<Checkbox bind:checked={billable} name="billable" />
<Field.Label>Billable</Field.Label>
</Field.Root> With a description
Field.Content stacks the label and its description beside the checkbox.
They get the invoice as a PDF the day it is sent.
<script lang="ts">
import { Checkbox, Field } from '@limerdev/zest';
</script>
<Field.Root orientation="horizontal" class="max-w-sm">
<Checkbox />
<Field.Content>
<Field.Label>Send a copy to the customer</Field.Label>
<Field.Description>They get the invoice as a PDF the day it is sent.</Field.Description>
</Field.Content>
</Field.Root> Select all
indeterminate shows that only some of a group are checked.
<script lang="ts">
import { Checkbox, Field } from '@limerdev/zest';
type Day = { name: string; checked: boolean };
let days: Day[] = $state([
{ name: 'Monday', checked: true },
{ name: 'Tuesday', checked: false },
{ name: 'Wednesday', checked: true }
]);
const all = $derived(days.every((day) => day.checked));
const some = $derived(!all && days.some((day) => day.checked));
function toggleAll(checked: boolean): void {
for (const day of days) day.checked = checked;
}
</script>
<Field.Set>
<Field.Legend variant="label">Submit hours for</Field.Legend>
<Field.Root orientation="horizontal" class="w-auto">
<Checkbox checked={all} indeterminate={some} onCheckedChange={toggleAll} />
<Field.Label>All days</Field.Label>
</Field.Root>
<div class="ms-6 grid gap-3">
{#each days as day (day.name)}
<Field.Root orientation="horizontal" class="w-auto">
<Checkbox bind:checked={day.checked} />
<Field.Label>{day.name}</Field.Label>
</Field.Root>
{/each}
</div>
</Field.Set> Props
Every part also takes class (merged over its defaults), ref (bindable: the element), children, and child (a snippet that
renders your own element, with props to spread on it).
Checkbox
| Prop | Type | Default | Description |
|---|---|---|---|
| checked bindable | boolean | false | The checked state of the checkbox. It can be one of: - true for checked - false for unchecked |
| disabled | null | false | true | false | Whether the checkbox is disabled. |
| form | string | undefined | The id of the form to link the checkbox button and hidden input to. |
| indeterminate bindable | boolean | false | Whether the checkbox is in an indeterminate state or not. |
| name | any | undefined | The name of the checkbox used in form submission. If not provided, the hidden input will not be rendered. |
| onCheckedChange | OnChangeFn<boolean> | – | A callback function called when the checked state changes. |
| onIndeterminateChange | OnChangeFn<boolean> | – | A callback function called when the indeterminate state changes. |
| readonly | null | false | true | false | Whether the checkbox is read only. If readonly, the checkbox will be focusable by the user, but will not be able to be checked/unchecked. |
| required | boolean | false | Whether the checkbox is required (for form validation). |
| value | string | undefined | The value of the checkbox used in form submission and to identify the checkbox when in a Checkbox.Group. If not provided while in a Checkbox.Group, the checkbox will use a random identifier. |