Checkbox
An easily stylable checkbox component.
Usage guidelines
- Form controls must have an accessible name: It can be created using a
<label>element or theFieldcomponent. 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:checkedto share it with the parent. Inside a group, the group value wins.- Type
boolean- Default
false- Binding
bind:checked
defaultChecked boolean false
- Name
- Description
The state an empty
checkedstarts from, as on an HTML checkbox.- Type
boolean- Default
false
disabled boolean | null false
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean | null- Default
false
readonly boolean | null false
- Name
- Description
Whether the user should be unable to tick or untick the checkbox.
- Type
boolean | null- Default
false
required boolean | null false
- Name
- Description
Whether the user must tick the checkbox before submitting a form.
- Type
boolean | null- Default
false
indeterminate boolean false
- Name
- 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 setsdata-parentand is left out of form submission.- Type
boolean- Default
false
nativeButton boolean false
- Name
- Description
Whether the host is a native
<button>. Settrueand render a<button>. Theidis 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 —
- Name
- 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
nativeButtonis true, the id is applied to the root element instead.- Type
string | null
onCheckedChange function —
- Name
- 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>. Spreadpropsonto 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-checkedismixedand 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.
nullmeans 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
data-unchecked
data-disabled
data-readonly
data-required
data-valid
data-invalid
data-dirty
data-touched
data-filled
data-focused
data-indeterminate
data-parent
Indicator
Indicates whether the checkbox is ticked. Renders a <span> while ticked, mixed, or kept mounted.
keepMounted boolean false
- Name
- 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>. Spreadpropsonto 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-checkedismixedand 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.
nullmeans 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