Skip to contents

Checkbox

An easily stylable checkbox component.

Usage guidelines

  • Form controls must have an accessible name: It can be created using a <label> element or the Field component. See Labeling a checkbox and the forms guide.

Anatomy

Import the component and assemble its parts:

Examples

Labeling a checkbox

An enclosing <label> is the simplest labeling pattern:

Rendering as a native button

By default, <Checkbox.Root> renders a <span> element to support enclosing labels. Prefer rendering the checkbox as a native button when using sibling labels (for/id). Set nativeButton and render a <button> with the render snippet.

Native buttons with wrapping labels are supported by using the render snippet to avoid invalid HTML, so the hidden input is placed outside the label:

Form integration

Use Field to handle label associations and form integration:

API reference

Root

Represents the checkbox itself. Renders a <span> and a hidden checkbox beside it.

checked boolean false
Name
Description

Whether the checkbox is currently ticked. Use bind:checked to share it with the parent. Inside a group, the group value wins.

Type
boolean
Default
false
Binding
bind:checked
defaultChecked boolean false
Description

The state an empty checked starts from, as on an HTML checkbox.

Type
boolean
Default
false
disabled boolean | null false
Description

Whether the component should ignore user interaction.

Type
boolean | null
Default
false
readonly boolean | null false
Description

Whether the user should be unable to tick or untick the checkbox.

Type
boolean | null
Default
false
required boolean | null false
Description

Whether the user must tick the checkbox before submitting a form.

Type
boolean | null
Default
false
indeterminate boolean false
Description

Whether the checkbox is in a mixed state: neither ticked nor unticked.

Type
boolean
Default
false
parent boolean false
Name
Description

Whether this checkbox controls every value in a parent CheckboxGroup. It sets data-parent and is left out of form submission.

Type
boolean
Default
false
nativeButton boolean false
Description

Whether the host is a native <button>. Set true and render a <button>. The id is then applied to that button instead of the hidden input.

Type
boolean
Default
false
name string | null —
Name
Description

Identifies the field when a form is submitted.

Type
string | null
value string —
Name
Description

The value submitted with the form when the checkbox is ticked. By default, the checkbox submits "on", matching a native checkbox.

Type
string
uncheckedValue string —
Description

The value submitted with the form when the checkbox is unticked. By default, an unticked checkbox does not submit a value.

Type
string
form string | null —
Name
Description

Identifies the form that owns the hidden input. Useful when the checkbox is rendered outside the form.

Type
string | null
id string | null —
Name
Description

The id of the hidden input. When nativeButton is true, the id is applied to the root element instead.

Type
string | null
onCheckedChange function —
Description

Called before the checked state changes. Call eventDetails.cancel() to veto it.

Type
(checked: boolean, eventDetails: CheckboxRootChangeEventDetails) => void
onclick MouseEventHandler<HTMLElement> | null —
Name
Description

Runs before the checkbox's click handler. Call event.preventDefault() to skip it.

Type
MouseEventHandler<HTMLElement> | null
render Snippet<[props: CheckboxHostProps, state: CheckboxRootState, children: Snippet | undefined]> —
Name
Description

Replace the default <span>. Spread props onto the host element.

Type
Snippet<[props: CheckboxHostProps, state: CheckboxRootState, children: Snippet | undefined]>
checked boolean
Name
checked
Description

Whether the checkbox is currently ticked.

Type
boolean
disabled boolean
Name
disabled
Description

Whether the component should ignore user interaction.

Type
boolean
readOnly boolean
Name
readOnly
Description

Whether the user should be unable to tick or untick the checkbox.

Type
boolean
required boolean
Name
required
Description

Whether the user must tick the checkbox before submitting a form.

Type
boolean
indeterminate boolean
Name
indeterminate
Description

Whether the checkbox is in a mixed state. While mixed, aria-checked is mixed and the checked style hooks are omitted.

Type
boolean
touched boolean
Name
touched
Description

Whether the field has been touched.

Type
boolean
dirty boolean
Name
dirty
Description

Whether the field value has changed from its initial value.

Type
boolean
valid boolean | null
Name
valid
Description

Whether the field is valid. null means validity is not known yet.

Type
boolean | null
filled boolean
Name
filled
Description

Whether the field has a value.

Type
boolean
focused boolean
Name
focused
Description

Whether the field is focused.

Type
boolean
data-checked
Present when the checkbox is checked.
data-unchecked
Present when the checkbox is not checked.
data-disabled
Present when the checkbox is disabled.
data-readonly
Present when the checkbox is readonly.
data-required
Present when the checkbox is required.
data-valid
Present when the checkbox is in a valid state (when wrapped in Field.Root).
data-invalid
Present when the checkbox is in an invalid state (when wrapped in Field.Root).
data-dirty
Present when the checkbox's value has changed (when wrapped in Field.Root).
data-touched
Present when the checkbox has been touched (when wrapped in Field.Root).
data-filled
Present when the checkbox is checked (when wrapped in Field.Root).
data-focused
Present when the checkbox is focused (when wrapped in Field.Root).
data-indeterminate
Present when the checkbox is in an indeterminate state.
data-parent
Present when the checkbox is a parent checkbox.

Indicator

Indicates whether the checkbox is ticked. Renders a <span> while ticked, mixed, or kept mounted.

keepMounted boolean false
Description

Whether to keep the indicator in the DOM when the checkbox is unticked and not mixed.

Type
boolean
Default
false
render Snippet<[props: HTMLAttributes<HTMLSpanElement>, state: CheckboxIndicatorState, children: Snippet | undefined]> —
Name
Description

Replace the default <span>. Spread props onto the host element.

Type
Snippet<[props: HTMLAttributes<HTMLSpanElement>, state: CheckboxIndicatorState, children: Snippet | undefined]>
transitionStatus CheckboxIndicatorPhase
Name
transitionStatus
Description

Indicator motion phase.

Type
CheckboxIndicatorPhase
checked boolean
Name
checked
Description

Whether the checkbox is currently ticked.

Type
boolean
disabled boolean
Name
disabled
Description

Whether the component should ignore user interaction.

Type
boolean
readOnly boolean
Name
readOnly
Description

Whether the user should be unable to tick or untick the checkbox.

Type
boolean
required boolean
Name
required
Description

Whether the user must tick the checkbox before submitting a form.

Type
boolean
indeterminate boolean
Name
indeterminate
Description

Whether the checkbox is in a mixed state. While mixed, aria-checked is mixed and the checked style hooks are omitted.

Type
boolean
touched boolean
Name
touched
Description

Whether the field has been touched.

Type
boolean
dirty boolean
Name
dirty
Description

Whether the field value has changed from its initial value.

Type
boolean
valid boolean | null
Name
valid
Description

Whether the field is valid. null means validity is not known yet.

Type
boolean | null
filled boolean
Name
filled
Description

Whether the field has a value.

Type
boolean
focused boolean
Name
focused
Description

Whether the field is focused.

Type
boolean
data-checked
Present when the checkbox is checked.
data-unchecked
Present when the checkbox is not checked.
data-disabled
Present when the checkbox is disabled.
data-readonly
Present when the checkbox is readonly.
data-required
Present when the checkbox is required.
data-valid
Present when the checkbox is in a valid state (when wrapped in Field.Root).
data-invalid
Present when the checkbox is in an invalid state (when wrapped in Field.Root).
data-dirty
Present when the checkbox's value has changed (when wrapped in Field.Root).
data-touched
Present when the checkbox has been touched (when wrapped in Field.Root).
data-filled
Present when the checkbox is checked (when wrapped in Field.Root).
data-focused
Present when the checkbox is focused (when wrapped in Field.Root).
data-indeterminate
Present when the checkbox is in an indeterminate state.
data-starting-style
Present when the checkbox indicator begins animating in.
data-ending-style
Present when the checkbox indicator is animating out.