Ant Design Svelte

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

checked: false

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.

Unavailable state

disabled disables interaction, maintaining a cleared, intermediate, or selected state.

There have been no events yet

Controllable flag

checked and disabled can be changed externally. onChange receives a CheckboxChangeEvent with target.checked and target.value.

["Яблоко"]
["email"]

Option groups

options accepts simple values ​​or objects with label and value. disabled works for an individual option and the entire group.


Selected: 2 from 3

Select all

checked and indeterminate are calculated from the selected array. An intermediate state means that only some of the options have been selected.

["A"]

Grid placement

CheckboxGroup with child Checkboxes allows its own markup. Row and Col arrange options in three columns.

Semantic styles

classNames and styles are styled root, icon and label. The function from { props } allows you to take checked into account.

[0]

Typed values

string, number and boolean are stored in the array. The values ​​0 and false remain valid options.

{"accepted":false,"topics":["news"]} Form has not been submitted yet

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.

Form has not been submitted yet

Native form and focus

name adds the marked values ​​to the FormData. reset restores defaultChecked and defaultValue; bind:this gives focus() and blur().

docs
General: suite; local: project-checkbox

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.

API

Below are the main parameters for examples. Precise definitions are available in public types CheckboxProps, CheckboxGroupProps and CheckboxGroupOption package @woodger/antd.

Main Parameters Checkbox
PropertyDescriptionTypeDefault
prefixClsBasic CSS prefix without the -group/-button suffix. The empty string and undefined inherit ConfigProvider. The group applies to options; children choose it independently.stringConfigProvider.prefixCls + "-checkbox"
checkedSelected state; supports bind:checked outside of CheckboxGroup.booleanfalse
defaultCheckedInitial state without binding. Native reset restores it without callbacks.booleanfalse
indeterminateIntermediate state with aria-checked="mixed"; supports bind:indeterminate. Custom switching resets it.booleanfalse
disabledDisables interaction. Inherited from Group, Form or ConfigProvider; disabled group disables all child checkboxes.booleanFrom context
valueValue for group or FormData. Does not set the selected state of a single Checkbox.CheckboxValueon
skipGroupExcludes Checkbox from the selected group array; disabled external fieldset remains in effect.booleanfalse
onCheckedChangeGets a boolean after user modification. Convenient for model shape.(checked: boolean) => void—
onChangeGets CheckboxChangeEvent: target.checked, target.value, nativeEvent and event control methods. Native onchange receives a separate DOM event.(event: CheckboxChangeEvent) => void—
onFocusCalled when the native input receives focus.(event: FocusEvent) => void—
onBlurCalled when the native input loses focus.(event: FocusEvent) => void—
autoFocusFocuses native input when mount. Regular autofocus is also supported.booleanfalse
classNamesClasses root, icon and label; object or function from { props }.CheckboxSemantic—
stylesCSS lines of the same semantic slots; object or function from { props }.CheckboxSemantic—
inputElementbind:inputElement gets native input; the link is cleared when unmounted.HTMLInputElement—
nativeElementbind:nativeElement gets the icon's inner span, not the outer label.HTMLSpanElement—
refbind:ref receives input, nativeElement and focus() / blur() methods. focus() and blur() are also available through bind:this.CheckboxRef—
Main parameters of CheckboxGroup
PropertyDescriptionTypeDefault
prefixClsBasic CSS prefix without the -group/-button suffix. The empty string and undefined inherit ConfigProvider. The group applies to options; children choose it independently.stringConfigProvider.prefixCls + "-checkbox"
valueArray of selected values; supports bind:value. The value type is set via generic CheckboxGroupProps<value>.Value[][]
defaultValueInitial array without binding. Native reset restores it without callbacks.readonly Value[][]
optionsSimple string / number / boolean or CheckboxGroupOption<value> objects. Non-empty options takes precedence over children.readonly (Value | CheckboxGroupOption<Value> | null | undefined)[][]
childrenSnippet with child Checkboxes and its own markup, for example Row / Col.Snippet—
disabledLocks the entire group via a native fieldset. Local disabled=false for a variant does not remove the lock.booleanFrom context
nameName of the native checkbox group. FormData.getAll(name) returns rows of only the checked available options.stringFrom FormItem
onChangeGets the selected array with the original value types, not the DOM event.(value: Value[]) => void—
onValueChangeCompatible callback of the selected array; called after onChange.(value: Value[]) => void—
nativeElementbind: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

classstyleclassNamesstyles

The 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().

Methods and DOM references Checkbox
NameTypePurpose
focus(options?: FocusOptions) => voidFocuses the native input.
blur() => voidRemoves focus from input.
nativeElementHTMLSpanElementspan 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.

root: External label: combines icon and label, switchable by pressing.

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.

Common tokens of the current topic
TokenPurposeCurrent value
colorPrimaryColor of the marked and intermediate states.#2f54eb
colorPrimaryBorderLight outline of the main color.#adc6ff
colorPrimaryHoverInteractive hover color.#597ef7
colorBorderUnchecked checkbox frame.#d9d9d9
colorBgContainerIcon background.#ffffff
colorBgContainerDisabledBackground of the inaccessible icon.rgba(0,0,0,0.04)
colorTextLabel color.rgba(0,0,0,0.88)
colorTextDisabledColor of unavailable label.rgba(0,0,0,0.25)
colorWhiteThe color of the selected checkbox.#fff
controlInteractiveSizeCheckbox icon size.16
fontSizeLabel text size.14
fontSizeLGLarge text size.16
fontFamilyFont 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'
lineHeightLabel line height.1.5714285714285714
lineWidthFrame thickness.1
lineWidthBoldReinforced line thickness.2
lineWidthFocusThe thickness of the keyboard focus stroke.3
lineTypeFrame line type.solid
borderRadiusSMIcon rounding.4
paddingXSIndentation of the label from the icon.8
marginXSSmall external indent.8
motionDurationFastDuration of fast transitions.0.1s
motionDurationMidDuration of main transitions.0.2s
motionDurationSlowDuration of slow transitions.0.3s
motionEaseInBackTransition curve with reverse movement at the beginning.cubic-bezier(0.71, -0.46, 0.88, 0.6)
motionEaseOutBackTransition 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.

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.