Checkbox
A control that allows the user to toggle between checked and not checked.
Features
- Supports indeterminate state
- Full keyboard navigation
- Can be controlled or uncontrolled
Anatomy
- Root: The root container for the checkbox
- Input: The native html input element that is hidden from view
Usage
To create a checkbox, use the createCheckbox
builder function. Follow the anatomy or the example
above to create your checkbox.
Indeterminate state
To create an indeterminate checkbox, set the checked
argument as indeterminate
.
Disabling the checkbox
To disable the checkbox, set the disabled
argument as true
.
API Reference
createCheckbox
The builder function used to create the checkbox component.
Props
Prop | Default | Type / Description |
disabled | false | boolean Whether or not the checkbox is disabled. |
required | false | boolean Whether or not the checkbox is required. |
name | - | string The name of the checkbox. Submitted with its owning form as part of a name/value pair. |
value | - | string The value given as data when submitted with a |
defaultChecked | false | boolean | 'indeterminate' The default checked state of the checkbox. |
checked | - | Writable<boolean | 'indeterminate'> The controlled checked state store of the checkbox. If provided, this will override the value passed to |
onCheckedChange | - | ChangeFn<boolean | 'indeterminate'> A callback called when the value of the See Change Functions |
Elements
States
State | Description |
checked | A writable store that contains the checked state of the checkbox. |
Helpers
Helper | Description |
isChecked | A derived store that returns whether or not the checkbox is checked. |
isIndeterminate | A derived store that returns whether or not the checkbox is in an indeterminate state. |
Options
Option | Description |
disabled | Whether or not the checkbox is disabled. |
required | Whether or not the checkbox is required. |
name | The name of the checkbox. Submitted with its owning form as part of a name/value pair. |
value | The value given as data when submitted with a |
root
The checkbox element.
Data Attributes
Data Attribute | Value |
[data-disabled] | Present when the element is disabled. |
[data-state] |
|
[data-melt-checkbox] | Present on all checkbox elements. |
Custom Events
Event | Value |
m-keydown | (e: ) => void |
m-click | (e: ) => void |
input
The native input element.
Data Attributes
Data Attribute | Value |
[data-melt-checkbox-hidden-input] | Present on all checkbox input elements. |
Accessibility
Adheres to the tri-state Checkbox WAI-ARIA design pattern
Key | Behavior |
Space | Toggles the checkbox state. |
On This Page