Checkbox
Checkboxes for independent selection and group selection.
When to use
A checkbox marks an independent choice: consent, additional service, or multiple subscription topics. Typically, the user first changes the marks, then confirms them by submitting the form. For a setting that is applied immediately, use Switch; for one of several options - Radio.
The signature should explain the meaning of the on state. Give the group a common accessible name, and each option its own signature. CheckboxGroup does not turn the options into one common radio button: each remains a native checkbox.
Checkbox CSS Prefixes show global ConfigProvider.prefixCls and local prefixCls without resetting state. The Group prefix is the basic Checkbox prefix without -group. It applies to options; arbitrary children choose their prefix separately. Row and namespace themes are independent.
Status and value of
A single checkbox has a different state and send value:
| Problem | Property | Example |
|---|---|---|
| Check single checkbox | checked, bind:checked |
Basic checkbox |
| Link multiple selections | CheckboxGroup.value, bind:value |
Group |
| Show partial selection | indeterminate |
Select all |
| Submit native field value | name, value |
Native form |
defaultChecked and defaultValue groups define the initial state. For subsequent changes, use binding or managed props. A single onCheckedChange receives a boolean, and a single onChange receives an event with target.checked and nativeEvent. The group onChange and onValueChange receive an array of values.
CheckboxGroup supports string, number and boolean. Store these types in options and models: the number 0, the string '0', and the boolean false denote different options. Typed Example does not convert them to strings within the model.
Group and selection of all options
For a standard list, use options. Child Checkboxes are needed for their own markup, for example mesh with Row and Col. Non-empty options takes precedence over children. skipGroup excludes an individual checkbox from group selection, preserving the disabled effect of the outer fieldset.
indeterminate is a separate visual and accessible partial selection attribute, rather than a third value of the boolean model. In "Select all" example checked is calculated when all options are selected, indeterminate - when the part is non-empty. Update the variant array and calculated characteristics together. If some options are not available, the application determines which ones are included in the overall selection.
Tab moves focus between available checkboxes, Space changes the mark. Arrows do not select the adjacent Checkbox, as in RadioGroup. Do not replace a native input element without its keyboard semantics when designing a label or icon.
Form and native submission
In Form pass checked from the model and update it via onCheckedChange; for group use value and onValueChange. FormItem binds the signature and errors, but does not expose the state to the child snippet. Checkbox form stores a boolean of agreements and an array of topics.
The required rule does not consider false to be an empty model value. For mandatory consent, set a validator that accepts only true; a visual mandatory marker by itself does not perform such a check.
Native FormData contains rows of only checked available checkboxes with name. An unchecked Checkbox does not send false. For a group, use FormData.getAll(name) to get duplicate values. Native form shows dispatch, reset, and program focus separately from the typed model.
Checkbox uses design tokens from the current theme for its appearance.
Examples
Basic flag
bind:checked binds a boolean to the Svelte model. The text of the checkbox also toggles the state; from the keyboard use Space.
<script lang="ts">
import { Checkbox } from '@woodger/antd';
let checked = $state(false);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<Checkbox bind:checked>{("Receive project news")}</Checkbox>
<output
aria-label={("Checkbox status")}
>checked: {(String(checked))}</output>
</div>
Unavailable state
disabled disables interaction, maintaining a cleared, intermediate, or selected state.
<script lang="ts">
import { Checkbox } from '@woodger/antd';
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
</style>
<div class="example">
<Checkbox disabled>{("Unavailable option")}</Checkbox>
<Checkbox disabled indeterminate>{("Intermediate unavailable option")}</Checkbox>
<Checkbox disabled defaultChecked>{("Selected unavailable option")}</Checkbox>
</div>
Controllable flag
checked and disabled can be changed externally. onChange receives a CheckboxChangeEvent with target.checked and target.value.
<script lang="ts">
import { Button, Checkbox, type CheckboxProps } from '@woodger/antd';
let checked = $state(true);
let disabled = $state(false);
let eventResult = $state(("There have been no events yet"));
const handleChange: CheckboxProps['onChange'] = event => {
checked = event.target.checked;
eventResult = ("onChange: " + String((String(event.target.checked))) + "; value: " + String((String(
event.target.value
))) + "");
};
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
output {
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<div class="example">
<Checkbox {checked} {disabled} value="updates" onChange={handleChange}>
{("Controllable flag")}
</Checkbox>
<div class="actions">
<Button onclick={() => { checked = !checked; }}>{(checked ? "Uncheck" : "Mark")}</Button>
<Button onclick={() => { disabled = !disabled; }}>{(disabled ? "Allow input" : "Disable input")}</Button>
</div>
<output aria-label={("Event driven Checkbox")}>{eventResult}</output>
</div>
Option groups
options accepts simple values or objects with label and value. disabled works for an individual option and the entire group.
<script lang="ts">
import { CheckboxGroup, type CheckboxGroupOption } from '@woodger/antd';
let fruit = $state<string[]>(["Apple"]);
let delivery = $state<string[]>(['email']);
const options = [
{ value: 'email', label: "Mail", title: "Sending by email" },
{ value: 'push', label: 'Push' },
{ value: 'sms', label: 'SMS', disabled: true }
] satisfies CheckboxGroupOption[];
</script>
<style>
.example, .field {
display: grid;
justify-items: start;
gap: 20px;
}
.example { gap: 28px; }
output {
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<div class="example">
<div class="field">
<CheckboxGroup aria-label={("Fruit")} options={([
"Apple",
"Pear",
"Orange"
])} bind:value={fruit} />
<output aria-label={("Selected fruits")}>{JSON.stringify(fruit)}</output>
</div>
<div class="field">
<CheckboxGroup
aria-label={("Delivery channels")}
options={(options)}
bind:value={delivery}
/>
<output aria-label={("Selected channels")}>{JSON.stringify(delivery)}</output>
</div>
<CheckboxGroup
aria-label={("Inaccessible group")}
options={(options)}
disabled
defaultValue={['push']}
/>
</div>
Select all
checked and indeterminate are calculated from the selected array. An intermediate state means that only some of the options have been selected.
<script lang="ts">
import { Checkbox, CheckboxGroup } from '@woodger/antd';
const options = ["Apple", "Pear", "Orange"];
let selected = $state<string[]>(["Apple", "Orange"]);
const checked = $derived(selected.length === options.length);
const indeterminate = $derived(selected.length > 0 && selected.length < options.length);
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
hr {
width: 100%;
margin: 0;
border: 0;
border-top: 1px solid var(--ant-color-border-secondary);
}
output { color: var(--ant-color-text-secondary); }
</style>
<div class="example">
<Checkbox {checked} {indeterminate}
onCheckedChange={value => { selected = value ? [...options] : []; }}>
{("Select all fruits")}
</Checkbox>
<hr />
<CheckboxGroup aria-label={("Whole fruit selection")} options={(options)} bind:value={selected} />
<output
aria-label={("Number of selected fruits")}
>{("Selected: ")}{(selected.length)}{(" from ")}{(options.length)}</output>
</div>
Grid placement
CheckboxGroup with child Checkboxes allows its own markup. Row and Col arrange options in three columns.
<script lang="ts">
import { Checkbox, CheckboxGroup, Col, Row } from '@woodger/antd';
let selected = $state<string[]>(['A']);
</script>
<style>
.example {
display: grid;
gap: 20px;
}
output {
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<div class="example">
<CheckboxGroup
aria-label={("Options in grid")}
bind:value={selected}
style="width:100%"
>
<Row gutter={[12, 16]} style="width:100%">
{#each ['A', 'B', 'C', 'D', 'E'] as value (value)}
<Col
span={8}
><Checkbox
{value}
>{("Option ")}{(value)}</Checkbox></Col>
{/each}
</Row>
</CheckboxGroup>
<output aria-label={("Selected mesh options")}>{JSON.stringify(selected)}</output>
</div>
Semantic styles
classNames and styles are styled root, icon and label. The function from { props } allows you to take checked into account.
<script lang="ts">
import { Checkbox, type CheckboxProps } from '@woodger/antd';
const styles = {
root: `
padding: 8px 12px;
border-radius: 6px;
background: var(--ant-color-fill-alter);
`,
icon: 'border-radius:6px',
label: 'font-weight:600'
} satisfies CheckboxProps['styles'];
const dynamicClasses: CheckboxProps['classNames'] = ({ props }) => ({
label: props.checked ? 'selected-label' : 'idle-label'
});
const dynamicStyles: CheckboxProps['styles'] = ({ props }) => ({
root: `
padding: 8px 12px;
border-radius: 6px;
border: 1px solid var(--ant-color-border);
`,
label: props.checked ? 'color:var(--ant-color-success)' : undefined
});
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
.example :global(.selected-label) { font-weight: 700; }
.example :global(.idle-label) { font-weight: 400; }
</style>
<div class="example">
<Checkbox {styles}>{("Styles object")}</Checkbox>
<Checkbox defaultChecked classNames={dynamicClasses} styles={dynamicStyles}>
{("Registration according to condition")}
</Checkbox>
</div>
Typed values
string, number and boolean are stored in the array. The values 0 and false remain valid options.
<script lang="ts">
import { CheckboxGroup, type CheckboxGroupOption, type CheckboxGroupValue } from '@woodger/antd';
let selected = $state<CheckboxGroupValue[]>([0]);
const options = [
{ value: 0, label: "Number 0" },
{ value: false, label: 'Boolean false' },
{ value: 'draft', label: "String draft" }
] satisfies CheckboxGroupOption<CheckboxGroupValue>[];
</script>
<style>
.example {
display: grid;
justify-items: start;
gap: 20px;
}
output {
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<div class="example">
<CheckboxGroup aria-label={("Typed values")} options={(options)} bind:value={selected} />
<output aria-label={("Typed array CheckboxGroup")}>{JSON
.stringify(selected)}</output>
</div>
Confirmation and group in Form
A single checkbox is connected via checked, a group – via value. The example shows consent verification, form submission, and form reset.
<script lang="ts">
import { Button, Checkbox, CheckboxGroup, Form, FormItem, createForm } from '@woodger/antd';
let result = $state('');
const form = createForm<{ accepted: boolean; topics: string[] }>({
initialValues: { accepted: false, topics: ['news'] },
rules: ({
accepted: [{
required: true,
validator: value => value === true ? undefined : "Confirm your agreement."
}],
topics: [{ required: true, message: "Select at least one topic." }]
})
});
function setAccepted(value: boolean) {
form.setFieldValue('accepted', value);
result = '';
}
function setTopics(value: string[]) {
form.setFieldValue('topics', value);
result = '';
}
</script>
<style>
.actions {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
output {
display: block;
margin-top: 20px;
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<Form {form} onFinish={values => { result = JSON.stringify(values); }}>
<FormItem name="accepted" label={("Consent")}>
<Checkbox checked={form.values.accepted} onCheckedChange={setAccepted}
aria-label={("I accept the terms")}>{("I accept the terms")}</Checkbox>
</FormItem>
<FormItem name="topics" label={("Subscription themes")}>
<CheckboxGroup value={form.values.topics} onValueChange={setTopics} options={([
{ value: 'news', label: "News" },
{ value: 'releases', label: "Releases" }
])} />
</FormItem>
<div class="actions">
<Button type="primary" htmlType="submit" loading={form.isSubmitting}>
{("Save subscription")}
</Button>
<Button
htmlType="reset"
onclick={() => { result = ''; }}
>{("Reset subscription")}</Button>
</div>
</Form>
<output aria-label={("Checkbox form model")}>{JSON.stringify(form.values)}</output>
<output aria-label={("Checkbox form result")}>{(result
|| ("Form has not been submitted yet"))}</output>
Native form and focus
name adds the marked values to the FormData. reset restores defaultChecked and defaultValue; bind:this gives focus() and blur().
<script lang="ts">
import { Button, Checkbox, CheckboxGroup } from '@woodger/antd';
let control = $state<Checkbox>();
let result = $state('');
</script>
<style>
form {
display: grid;
justify-items: start;
gap: 20px;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
output {
display: block;
margin-top: 20px;
color: var(--ant-color-text-secondary);
overflow-wrap: anywhere;
}
</style>
<form onsubmit={event => {
event.preventDefault();
const values = new FormData(event.currentTarget);
result = JSON.stringify({ notify: values.get('notify'), channels: values.getAll('channels') });
}}>
<Checkbox bind:this={control} name="notify" value="yes" defaultChecked>
{("Notify about events")}
</Checkbox>
<CheckboxGroup name="channels" aria-label={("Native shaped channels")}
options={(['Email', 'Push'])} defaultValue={['Email']} />
<div class="actions">
<Button htmlType="submit">{("Read FormData")}</Button>
<Button htmlType="reset" onclick={() => { result = ''; }}>
{("Reset native form")}
</Button>
<Button onclick={() => control?.focus()}>{("Focus on checkbox")}</Button>
</div>
</form>
<output aria-label={("FormData Checkbox")}>{(result
|| ("Form has not been submitted yet"))}</output>
Group Prefix - Basic Checkbox prefix without -group. It applies to options; arbitrary children choose their prefix separately. Row and namespace themes are independent.
CSS prefixes Checkbox
Common namespace ConfigProvider and local prefixCls; the state of the component is preserved when the name is changed.
<script lang="ts">
import { Checkbox, ConfigProvider, CheckboxGroup } from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
const prefix = $derived(custom ? 'suite' : 'ant');
const localPrefix = $derived(local ? 'project-checkbox' : '');
let value = $state<string[]>(['docs']);
</script>
<style>
.example,
.prefix-stage {
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 16px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
p { margin: 0; }
output { overflow-wrap: anywhere; }
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("Common prefix Checkbox suite")}</Checkbox>
<Checkbox bind:checked={local}>{("Local prefix Checkbox")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} theme={{ cssVar: { prefix: 'checkbox-theme' } }}>
<div class="prefix-stage">
<CheckboxGroup prefixCls={localPrefix} bind:value aria-label={("Checkbox with prefix")} options={([
{ label: "Documentation", value: 'docs' }, { label: "Examples", value: 'examples' }
])} />
<output
aria-label={("Select Checkbox with prefix")}
>{(value.join(', ') || '—')}</output>
</div>
</ConfigProvider>
<output
aria-label={("CSS prefix Checkbox")}
>{("General: ")}{prefix}{("; local: ")}{(localPrefix
|| ("from the provider"))}</output>
<p>
{("Group Prefix - Basic Checkbox prefix without -group. It applies to options; arbitrary children choose their prefix separately. Row and namespace themes are independent.")}
</p>
</div>
API
Below are the main parameters for examples. Precise definitions are available in public types CheckboxProps, CheckboxGroupProps and CheckboxGroupOption package @woodger/antd.
| Property | Description | Type | Default |
|---|---|---|---|
prefixCls | Basic CSS prefix without the -group/-button suffix. The empty string and undefined inherit ConfigProvider. The group applies to options; children choose it independently. | string | ConfigProvider.prefixCls + "-checkbox" |
checked | Selected state; supports bind:checked outside of CheckboxGroup. | boolean | false |
defaultChecked | Initial state without binding. Native reset restores it without callbacks. | boolean | false |
indeterminate | Intermediate state with aria-checked="mixed"; supports bind:indeterminate. Custom switching resets it. | boolean | false |
disabled | Disables interaction. Inherited from Group, Form or ConfigProvider; disabled group disables all child checkboxes. | boolean | From context |
value | Value for group or FormData. Does not set the selected state of a single Checkbox. | CheckboxValue | on |
skipGroup | Excludes Checkbox from the selected group array; disabled external fieldset remains in effect. | boolean | false |
onCheckedChange | Gets a boolean after user modification. Convenient for model shape. | (checked: boolean) => void | — |
onChange | Gets CheckboxChangeEvent: target.checked, target.value, nativeEvent and event control methods. Native onchange receives a separate DOM event. | (event: CheckboxChangeEvent) => void | — |
onFocus | Called when the native input receives focus. | (event: FocusEvent) => void | — |
onBlur | Called when the native input loses focus. | (event: FocusEvent) => void | — |
autoFocus | Focuses native input when mount. Regular autofocus is also supported. | boolean | false |
classNames | Classes root, icon and label; object or function from { props }. | CheckboxSemantic | — |
styles | CSS lines of the same semantic slots; object or function from { props }. | CheckboxSemantic | — |
inputElement | bind:inputElement gets native input; the link is cleared when unmounted. | HTMLInputElement | — |
nativeElement | bind:nativeElement gets the icon's inner span, not the outer label. | HTMLSpanElement | — |
ref | bind:ref receives input, nativeElement and focus() / blur() methods. focus() and blur() are also available through bind:this. | CheckboxRef | — |
| Property | Description | Type | Default |
|---|---|---|---|
prefixCls | Basic CSS prefix without the -group/-button suffix. The empty string and undefined inherit ConfigProvider. The group applies to options; children choose it independently. | string | ConfigProvider.prefixCls + "-checkbox" |
value | Array of selected values; supports bind:value. The value type is set via generic CheckboxGroupProps<value>. | Value[] | [] |
defaultValue | Initial array without binding. Native reset restores it without callbacks. | readonly Value[] | [] |
options | Simple string / number / boolean or CheckboxGroupOption<value> objects. Non-empty options takes precedence over children. | readonly (Value | CheckboxGroupOption<Value> | null | undefined)[] | [] |
children | Snippet with child Checkboxes and its own markup, for example Row / Col. | Snippet | — |
disabled | Locks the entire group via a native fieldset. Local disabled=false for a variant does not remove the lock. | boolean | From context |
name | Name of the native checkbox group. FormData.getAll(name) returns rows of only the checked available options. | string | From FormItem |
onChange | Gets the selected array with the original value types, not the DOM event. | (value: Value[]) => void | — |
onValueChange | Compatible callback of the selected array; called after onChange. | (value: Value[]) => void | — |
nativeElement | bind:nativeElement gets the group's fieldset; For semantic design of options, use ConfigProvider.checkbox. | HTMLFieldSetElement | — |
In objects options can be set label text or snippet, value, disabled, title, className, CSS string style and onChange separate option.
ConfigProvider.checkbox
classstyleclassNamesstylesThe settings also apply to CheckboxGroup options. Local classes and styles complement the provider settings; componentDisabled sets general availability.
Native events
onchange, onfocus, onblur and onkeydown save Svelte events. Compatible names onChange, onFocus, onBlur and onKeyDown are also supported.
Methods
Via bind:this or bind:ref native checkbox methods available. Example of a native form shows call focus().
| Name | Type | Purpose |
|---|---|---|
focus | (options?: FocusOptions) => void | Focuses the native input. |
blur | () => void | Removes focus from input. |
nativeElement | HTMLSpanElement | span of the Checkbox icon. CheckboxGroup has a link pointing to a fieldset. |
Semantic DOM
Checkbox
Select an element to see its border on the running Checkbox. These names are used in classNames and styles.
styles accepts CSS strings. Object and function from { props } shows example of semantic design.
CheckboxGroup
Semantic classNames and styles groups introduced in Ant Design 6.7.0 are not yet supported by our package. The container is issued through class and CSS string style, options - via ConfigProvider.checkbox or options[].className / options[].style.
Registration tokens
Checkbox does not have separate component tokens. Color, border, icon size and label are customizable via shared tokens ConfigProvider.theme.token.
<script lang="ts">
import { Checkbox, ConfigProvider } from '@woodger/antd';
</script>
<ConfigProvider theme={{
cssVar: {},
token: {
colorPrimary: '#389e0d',
borderRadiusSM: 4,
lineWidth: 2
}
}}>
<Checkbox defaultChecked>{("Custom Checkbox theme")}</Checkbox>
</ConfigProvider>
Common tokens of the current topic
Values obtained via theme.useToken() and change along with the settings in the Ant Design header.
| Token | Purpose | Current value |
|---|---|---|
colorPrimary | Color of the marked and intermediate states. | #2f54eb |
colorPrimaryBorder | Light outline of the main color. | #adc6ff |
colorPrimaryHover | Interactive hover color. | #597ef7 |
colorBorder | Unchecked checkbox frame. | #d9d9d9 |
colorBgContainer | Icon background. | #ffffff |
colorBgContainerDisabled | Background of the inaccessible icon. | rgba(0,0,0,0.04) |
colorText | Label color. | rgba(0,0,0,0.88) |
colorTextDisabled | Color of unavailable label. | rgba(0,0,0,0.25) |
colorWhite | The color of the selected checkbox. | #fff |
controlInteractiveSize | Checkbox icon size. | 16 |
fontSize | Label text size. | 14 |
fontSizeLG | Large text size. | 16 |
fontFamily | Font family. | -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji' |
lineHeight | Label line height. | 1.5714285714285714 |
lineWidth | Frame thickness. | 1 |
lineWidthBold | Reinforced line thickness. | 2 |
lineWidthFocus | The thickness of the keyboard focus stroke. | 3 |
lineType | Frame line type. | solid |
borderRadiusSM | Icon rounding. | 4 |
paddingXS | Indentation of the label from the icon. | 8 |
marginXS | Small external indent. | 8 |
motionDurationFast | Duration of fast transitions. | 0.1s |
motionDurationMid | Duration of main transitions. | 0.2s |
motionDurationSlow | Duration of slow transitions. | 0.3s |
motionEaseInBack | Transition curve with reverse movement at the beginning. | cubic-bezier(0.71, -0.46, 0.88, 0.6) |
motionEaseOutBack | Transition curve with reverse movement at the end. | cubic-bezier(0.12, 0.4, 0.29, 1.46) |
FAQ
Why doesn't value mark a single Checkbox?
value sets the value of the variant to be sent, and the state is set to checked. In Svelte use bind:checked. CheckboxGroup bind:value associates an array of selected values.
How to connect Checkbox with Form?
Pass checked from form.values and update the field via onCheckedChange. For group use value and onValueChange. Example form checks agreement and topics, then submits or resets the model.
How to require explicit consent?
Add a validator that only accepts true. false is acceptable as a boolean value and is not considered empty for the required rule. FormItem.required controls the token; it does not replace the model rule.
How does indeterminate work?
This is a separate intermediate state available to assistive technologies as mixed. It doesn't choose options on its own. Calculate it from the selected array, as in general choice.
Why is FormData different from a model?
The model stores boolean and typed array. The native form sends lines only of the marked available checkboxes with name; an unchecked checkbox does not send false. For the group, read duplicate values via FormData.getAll(name).
Is it possible to place variants with your own markup?
Yes, pass child Checkboxes instead of options. Grid example is placed between the group and the Row and Col checkboxes. Non-empty options shows its own options instead of children.
How to switch Checkbox from the keyboard and design its parts?
Tab changes focus, Space changes state. The focus and blur methods use real input. Semantic DOM shows root, icon and label; styles accepts CSS strings, classNames accepts class names.