Checkbox
A checkbox marks a value as chosen, and the choice is usually sent later, with a form. It renders a native <input type="checkbox"> inside a <label>; CheckboxGroup manages a list of them.
When to use
- Use checkboxes to pick one or more items from a list, or to mark a single choice that is submitted later, with a form.
- If the change applies at once, with no extra Save or Submit step, a switch is the better fit.
- Use
indeterminatewith a group for a "check all" control, and a function binding when the parent has to confirm a change.
Examples
The box is checked no change yet
<script lang="ts">
import { Checkbox } from 'svelte-ant-design';
let remember = $state(true);
let last = $state('no change yet');
</script>
<!-- The text between the tags is the children of the checkbox, and its accessible name. -->
<Checkbox bind:checked={remember} onchange={(checked) => (last = `onchange(${checked})`)}>
Remember me
</Checkbox>
<span>The box is {remember ? 'checked' : 'unchecked'}</span>
<span role="status">{last}</span>
<script lang="ts">
import { Button, Checkbox } from 'svelte-ant-design';
let disabled = $state(true);
</script>
<Checkbox {disabled}>Unchecked</Checkbox>
<Checkbox {disabled} checked>Checked</Checkbox>
<Checkbox {disabled} indeterminate>Indeterminate</Checkbox>
<Button onclick={() => (disabled = !disabled)}>Toggle disabled</Button>
disabled, refused 0 times
<script lang="ts">
import { Checkbox } from 'svelte-ant-design';
let agreed = $state(false);
let enabled = $state(false);
let locked = $state(false);
let refused = $state(0);
</script>
<Checkbox bind:checked={agreed}>I accept the terms</Checkbox>
<!-- A function binding: the setter decides whether a change is accepted. -->
<Checkbox
bind:checked={
() => enabled,
(next) => {
if (next && !agreed) {
refused++;
return;
}
enabled = next;
}
}
>
Enable the feature
</Checkbox>
<span role="status">{enabled ? 'enabled' : 'disabled'}, refused {refused} times</span>
<!-- preventDefault() in a forwarded onclick cancels the toggle and onchange. -->
<Checkbox
bind:checked={locked}
onclick={(event) => {
if (!agreed) event.preventDefault();
}}
>
Lock the setting
</Checkbox>
Selected: Apple
<script lang="ts">
import { Checkbox, CheckboxGroup } from 'svelte-ant-design';
const options = ['Apple', 'Pear', 'Orange'];
let selected = $state<string[]>(['Apple']);
const all = $derived(selected.length === options.length);
const some = $derived(selected.length > 0 && !all);
</script>
<!-- indeterminate only changes the look; checked stays the value that is reported. -->
<Checkbox
checked={all}
indeterminate={some}
onchange={(checked) => (selected = checked ? [...options] : [])}
>
Check all
</Checkbox>
<CheckboxGroup bind:value={selected} {options} aria-label="Fruits" />
<span role="status">Selected: {selected.join(', ') || 'nothing'}</span>
no change yet
<script lang="ts">
import { CheckboxGroup } from 'svelte-ant-design';
let fruits = $state<string[]>(['Apple']);
let sizes = $state<number[]>([2]);
let extras = $state<string[]>([]);
let last = $state('no change yet');
</script>
<!-- Strings and numbers are both the value and the label; the group reports the whole array. -->
<CheckboxGroup
bind:value={fruits}
options={['Apple', 'Pear', 'Orange']}
aria-label="Fruits"
onchange={(value) => (last = `onchange([${value.join(', ')}])`)}
/>
<CheckboxGroup bind:value={sizes} options={[1, 2, 3]} aria-label="Sizes" name="size" />
<!-- Objects add a snippet label, disabled, title and an onchange of the option. -->
{#snippet cherry()}<strong>Cherry</strong>{/snippet}
<CheckboxGroup
bind:value={extras}
aria-label="Extras"
options={[
{ label: cherry, value: 'cherry' },
{
label: 'Pear',
value: 'pear',
title: 'Juicy',
onchange: (checked) => (last = `pear: ${checked}`)
},
{ label: 'Plum', value: 'plum', disabled: true }
]}
/>
<!-- disabled disables the whole group. -->
<CheckboxGroup disabled value={['Apple']} options={['Apple', 'Pear']} aria-label="Disabled fruits" />
<span role="status">{last}</span>
<script lang="ts">
import { Checkbox, CheckboxGroup } from 'svelte-ant-design';
let days = $state<string[]>(['mon', 'wed']);
</script>
<!-- The children of the group are your own markup: here a grid. Each checkbox needs a value. -->
<CheckboxGroup bind:value={days} aria-label="Days" style="display: block; width: 100%">
<div class="grid">
<Checkbox value="mon">Monday</Checkbox>
<Checkbox value="tue">Tuesday</Checkbox>
<Checkbox value="wed">Wednesday</Checkbox>
<Checkbox value="thu">Thursday</Checkbox>
<Checkbox value="fri" disabled>Friday (closed)</Checkbox>
<Checkbox value="sat">Saturday</Checkbox>
</div>
</CheckboxGroup>
<span role="status">Selected: {days.join(', ') || 'nothing'}</span>
<style>
.grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 8px 16px;
}
</style>
<script lang="ts">
import { Checkbox, CheckboxGroup } from 'svelte-ant-design';
</script>
<!-- class goes to the root; classNames adds classes to the root, icon and label parts. -->
<Checkbox checked class="spaced" classNames={{ root: 'halo', label: 'strong' }}>With classes</Checkbox>
<!-- styles are inline styles per part; custom properties work too. -->
<Checkbox
checked
styles={{
root: { '--ads-checkbox-checked-bg': '#237804', '--ads-checkbox-checked-bg-hover': '#135200' },
icon: { 'border-radius': '50%' },
label: { 'font-weight': 600 }
}}
>
With styles
</Checkbox>
<!-- style is the inline style of the root; styles.root is written after it and wins. -->
<Checkbox style="outline: 2px dashed #8c8c8c; outline-offset: 2px" styles={{ root: { opacity: 0.85 } }}>
With style
</Checkbox>
<!-- A group: root, and item, itemIcon and itemLabel for every checkbox inside. -->
<CheckboxGroup
value={['a']}
options={['a', 'b']}
aria-label="Group parts"
class="boxed"
classNames={{ root: 'halo', item: 'item' }}
styles={{ itemIcon: { 'border-radius': '50%' }, itemLabel: { 'font-style': 'italic' } }}
style="padding: 4px 8px"
/>
<style>
:global(.spaced) {
margin-inline-end: 12px;
}
:global(.halo) {
box-shadow: 0 0 0 4px rgb(22 104 220 / 25%);
border-radius: 4px;
}
:global(.strong) {
text-decoration: underline;
}
:global(.boxed) {
border: 1px dashed #8c8c8c;
}
:global(.item) {
padding-inline: 4px;
}
</style>
Bluetooth Needed for headphones
<script lang="ts">
import { Checkbox } from 'svelte-ant-design';
</script>
<!-- The text between the tags names the checkbox, and a click on it toggles the box. -->
<Checkbox>Wi-Fi</Checkbox>
<!-- aria-label for a checkbox that has no visible text. -->
<Checkbox aria-label="Airplane mode" title="Airplane mode" />
<!-- aria-labelledby points at any element that holds the name. -->
<span id="bluetooth-name">Bluetooth</span>
<Checkbox aria-labelledby="bluetooth-name" aria-describedby="bluetooth-hint" />
<small id="bluetooth-hint">Needed for headphones</small>
<!-- An external label: the id of the checkbox goes to its input. -->
<label for="sound">Sound</label>
<Checkbox id="sound" checked />
<script lang="ts">
import { Button, Checkbox, CheckboxGroup } from 'svelte-ant-design';
let terms = $state(false);
let topics = $state<string[]>([]);
let result = $state('');
function submit(event: SubmitEvent) {
event.preventDefault();
const data = new FormData(event.currentTarget as HTMLFormElement);
result = `Submitted: ${JSON.stringify([...data.entries()])}`;
}
// A native reset does not change the state of the components: reset it here.
function reset() {
terms = false;
topics = [];
result = '';
}
</script>
<form onsubmit={submit} onreset={reset}>
<Checkbox bind:checked={terms} name="terms" value="accepted" required>I accept the terms</Checkbox>
<CheckboxGroup bind:value={topics} name="topic" options={['news', 'offers']} aria-label="Topics" />
<Button htmlType="submit" type="primary">Save</Button>
<Button htmlType="reset">Reset</Button>
</form>
<span role="status">{result}</span>
API
| Property | Description | Type | Default |
|---|---|---|---|
| checked | Whether the checkbox is checked. Use bind:checked for two-way binding; a plain value is the initial state. Inside a CheckboxGroup the state comes from the group's value and this prop is ignored. | boolean | false |
| indeterminate | The "partly checked" look of a "check all" control. It changes only the appearance and the announced state (mixed); checked stays the value that is reported and submitted. | boolean | false |
| disabled | Native disabled on the input: not focusable, ignores clicks and keys. A group's disabled applies as well. | boolean | false |
| value | The identity inside a CheckboxGroup and the native value of the input (a number is written as text). Without it the input keeps the native on. | string | number | — |
| name | Native name of the input. Inside a group the group's name wins. | string | — |
| children | The label: any markup. It is the accessible name; without it give aria-label or aria-labelledby, or an external <label for>. | Snippet | — |
| onchange | Called once per user toggle, after the state changed, with the new value and the native change event (event.target is the input). Not called when the parent changes checked, nor while disabled. | (checked: boolean, event: Event) => void | — |
| classNames | Extra classes for the parts root (the label), icon (the box) and label (the text), appended to the component's own. | Partial<Record<'root' | 'icon' | 'label', string>> | — |
| styles | Inline styles for the same parts. Property names are kebab-case or custom properties, numbers are written as they are (no automatic px). They win over the component's styles. | Partial<Record<'root' | 'icon' | 'label', Record<string, string | number>>> | — |
| class | An additional CSS class for the root. | string | — |
| style | Inline style of the root; styles.root is written after it. | string | — |
| title | Native title of the root label, so the tooltip covers the box and the text. | string | — |
| native attributes | Native attributes of the input (id, required, form, tabindex, aria-*, data-*, onfocus, onblur, onclick, other handlers) are forwarded to it. type, checked, indeterminate, value, disabled and onchange belong to the component. | HTMLInputAttributes | — |
Differences from Ant Design:
onchange receives (checked, event) with the native event (React passes an object with target); there is no defaultChecked or defaultValue, because bind:checked and bind:value cover them; a group is a separate CheckboxGroup component whose onchange receives (value, event).CheckboxGroup API
| Property | Description | Type | Default |
|---|---|---|---|
| value | The selected values. Use bind:value for two-way binding; a plain value is the initial selection. The array follows the order of the options (or of the checkboxes in your markup); values that match nothing are kept after the known ones. | CheckboxValue[] | [] |
| options | Renders one checkbox per entry. A string or a number is both the value and the label; an object is { label, value, disabled?, title?, class?, style?, onchange? } with label as text or a snippet. Values must be unique. When it has entries, children is ignored. | ReadonlyArray<CheckboxValue | CheckboxOption> | [] |
| children | Your own markup with Checkbox elements anywhere inside (a grid, columns). Each one needs a value; outside production a missing one throws. | Snippet | — |
| disabled | Disables every checkbox of the group; the own disabled of an option disables that one alone. | boolean | false |
| name | Native name of every input of the group; it wins over the own name of a checkbox. | string | — |
| onchange | Called once per user toggle with a new array of the selected values and the change event of the toggled input. The onchange of the checkbox or option runs first. Not called when the parent changes value. | (value: CheckboxValue[], event: Event) => void | — |
| classNames | Extra classes: root is the group; item, itemIcon and itemLabel are appended to the root, icon and label parts of every checkbox inside. | Partial<Record<'root' | 'item' | 'itemIcon' | 'itemLabel', string>> | — |
| styles | Inline styles for the same parts. The own styles of a checkbox are written after the group's, so they win. | Partial<Record<'root' | 'item' | 'itemIcon' | 'itemLabel', Record<string, string | number>>> | — |
| class | An additional CSS class for the root. | string | — |
| style | Inline style of the root; styles.root is written after it. | string | — |
| native attributes | Native attributes of the <div> (id, aria-label, aria-labelledby, aria-describedby, data-*, handlers) are forwarded to it. role="group" belongs to the component: name the group with aria-label or aria-labelledby. | HTMLAttributes<HTMLDivElement> | — |
Methods
| Method | Description | Type |
|---|---|---|
| focus | Moves focus to the checkbox. | (options?: FocusOptions) => void |
| blur | Removes focus from the checkbox. | () => void |
Design tokens
| Token | Description | Default |
|---|---|---|
| --ads-checkbox-size | Width and height of the box. | 16px |
| --ads-checkbox-radius | Corner radius of the box. | 4px |
| --ads-checkbox-border-width | Thickness of the border of the box. | 1px |
| --ads-checkbox-check-width | Stroke of the check mark. | 2px |
| --ads-checkbox-font-size | Font size of the label. | 14px |
| --ads-checkbox-line-height | Line height of the label; the box is centered on the first line. | 1.5714 |
| --ads-checkbox-label-padding | Space between the box and the text, on both sides of the text. | 8px |
| --ads-checkbox-group-gap | Space between the checkboxes of a group. | 8px |
| --ads-checkbox-bg | Fill of an unchecked box. | #ffffff |
| --ads-checkbox-border | Border of an unchecked box. tokens-accessible.css darkens it to #737373 to keep 3:1 against the page. | #d9d9d9 |
| --ads-checkbox-border-hover | Border of an unchecked or indeterminate box on hover, over the box or the text. | #1677ff |
| --ads-checkbox-checked-bg | Fill and border of a checked box. | #1677ff |
| --ads-checkbox-checked-bg-hover | Fill and border of a checked box on hover. tokens-accessible.css darkens it to #0958d9 (#4096ff is 2.99:1 against the page). | #4096ff |
| --ads-checkbox-check-color | Color of the check mark. | #ffffff |
| --ads-checkbox-indeterminate-color | Color of the bar of an indeterminate box. | #1677ff |
| --ads-checkbox-disabled-bg | Fill of a disabled box. | rgba(0, 0, 0, 0.04) |
| --ads-checkbox-disabled-border | Border of a disabled box. | #d9d9d9 |
| --ads-checkbox-disabled-check-color | Check mark or bar of a disabled box. | rgba(0, 0, 0, 0.25) |
Accessibility
- The checkbox is a native
<input type="checkbox">in a<label>: Space toggles it, a click on the text works, and it takes part in forms (name,value,required). Enter and the arrow keys do nothing. - It needs an accessible name: its text (
children),aria-label,aria-labelledbyor an external<label for>. A group hasrole="group"; name it witharia-labeloraria-labelledby. - An
indeterminatecheckbox is announced as mixed. It changes only the look and the announced state:checkedis still the value that is reported and submitted. - By default the colours are Ant Design's own. With
tokens-accessible.css, and in the dark theme, the border, the check mark, the bar and the focus ring keep a contrast of at least 3:1 in every state, and the text 4.5:1: the unchecked border and the hover fill are darker. - In forced-colors modes such as Windows High Contrast the bar of an indeterminate box is drawn in the text colour, so it still differs from an unchecked box, and a disabled checkbox takes the disabled colour.