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.

Import
import { Checkbox, CheckboxGroup } from 'svelte-ant-design';
Links
src/lib/Checkbox Report an issue Component README

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 indeterminate with 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

Basic

bind:checked keeps a value in sync with the checkbox; onchange receives the new value and the native change event. The text between the tags is the label and the accessible name.

Disabled

A disabled checkbox is natively disabled: it is not focusable, and a click on the box or on the text does nothing. Unchecked, checked and indeterminate are all supported.

disabled, refused 0 times

Control by the parent

A function binding lets the parent accept or refuse a change, and the box goes back to the state of the parent (it needs Svelte 5.9 or newer). preventDefault() in a forwarded onclick cancels the toggle. Tick the first box to allow changes.

Selected: Apple

Check all

indeterminate only changes the look and the announced state (mixed), so a control that checks everything can show a partial selection. checked stays the value that is reported.

no change yet

Checkbox group

CheckboxGroup renders one checkbox per entry of options: a string or a number is both the value and the label, an object adds a snippet label, disabled, title and its own onchange. bind:value holds the selected values in the order of the options; name reaches every input.

Selected: mon, wed

Custom layout

Instead of options the group takes your own markup, here a grid: a Checkbox anywhere inside reads its state from the group and needs a value.

Classes and styles

classNames and styles address the root, icon and label parts of a checkbox, and the root, item, itemIcon and itemLabel parts of a group; class and style go to the root. Inline styles win over the component's own, and custom properties such as --ads-checkbox-checked-bg work.

Bluetooth Needed for headphones

Accessible name

Name a checkbox with its text, aria-label, aria-labelledby or an external <label for>. id, aria-* and tabindex go to the input; title goes to the whole label.

Forms and native attributes

A checkbox is a real input, so name, value and required work in a form: only the checked ones are submitted. A native reset does not change the state of the components, so the form resets it in onreset.

API

PropertyDescriptionTypeDefault
checkedWhether 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.booleanfalse
indeterminateThe "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.booleanfalse
disabledNative disabled on the input: not focusable, ignores clicks and keys. A group's disabled applies as well.booleanfalse
valueThe 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—
nameNative name of the input. Inside a group the group's name wins.string—
childrenThe label: any markup. It is the accessible name; without it give aria-label or aria-labelledby, or an external <label for>.Snippet—
onchangeCalled 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—
classNamesExtra 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>>—
stylesInline 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>>>—
classAn additional CSS class for the root.string—
styleInline style of the root; styles.root is written after it.string—
titleNative title of the root label, so the tooltip covers the box and the text.string—
native attributesNative 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

PropertyDescriptionTypeDefault
valueThe 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[][]
optionsRenders 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>[]
childrenYour own markup with Checkbox elements anywhere inside (a grid, columns). Each one needs a value; outside production a missing one throws.Snippet—
disabledDisables every checkbox of the group; the own disabled of an option disables that one alone.booleanfalse
nameNative name of every input of the group; it wins over the own name of a checkbox.string—
onchangeCalled 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—
classNamesExtra 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>>—
stylesInline 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>>>—
classAn additional CSS class for the root.string—
styleInline style of the root; styles.root is written after it.string—
native attributesNative 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

MethodDescriptionType
focusMoves focus to the checkbox.(options?: FocusOptions) => void
blurRemoves focus from the checkbox.() => void

Design tokens

TokenDescriptionDefault
--ads-checkbox-sizeWidth and height of the box.16px
--ads-checkbox-radiusCorner radius of the box.4px
--ads-checkbox-border-widthThickness of the border of the box.1px
--ads-checkbox-check-widthStroke of the check mark.2px
--ads-checkbox-font-sizeFont size of the label.14px
--ads-checkbox-line-heightLine height of the label; the box is centered on the first line.1.5714
--ads-checkbox-label-paddingSpace between the box and the text, on both sides of the text.8px
--ads-checkbox-group-gapSpace between the checkboxes of a group.8px
--ads-checkbox-bgFill of an unchecked box.#ffffff
--ads-checkbox-borderBorder of an unchecked box. tokens-accessible.css darkens it to #737373 to keep 3:1 against the page.#d9d9d9
--ads-checkbox-border-hoverBorder of an unchecked or indeterminate box on hover, over the box or the text.#1677ff
--ads-checkbox-checked-bgFill and border of a checked box.#1677ff
--ads-checkbox-checked-bg-hoverFill 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-colorColor of the check mark.#ffffff
--ads-checkbox-indeterminate-colorColor of the bar of an indeterminate box.#1677ff
--ads-checkbox-disabled-bgFill of a disabled box.rgba(0, 0, 0, 0.04)
--ads-checkbox-disabled-borderBorder of a disabled box.#d9d9d9
--ads-checkbox-disabled-check-colorCheck 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-labelledby or an external <label for>. A group has role="group"; name it with aria-label or aria-labelledby.
  • An indeterminate checkbox is announced as mixed. It changes only the look and the announced state: checked is 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.