Checkbox
Checkboxes let users toggle an option on or off, or select multiple items from a list. They also support an indeterminate state for partial selections in groups.
This component works with standard <form> elements. See form controls for form submission and client-side validation.
Examples
Initial Value
Use the checked attribute to activate the checkbox.
checked sets the initial value, not the current state.
Consistent with native checkboxes, it doesn't reflect later changes. To toggle the checked state with JavaScript, use the checked property instead. To target checked checkboxes with CSS, use the :state(checked) selector.
Hint
Add a descriptive hint with the hint attribute. For hints that contain HTML, use the hint slot instead.
Indeterminate
Use the indeterminate attribute to make the checkbox indeterminate. This is typically used for a "select all" control when its associated checkboxes have a mix of checked and unchecked states.
Disabled
Use the disabled attribute to disable the checkbox.
Size
Use the size attribute to change a checkbox's size.
Custom Validity
Use the setCustomValidity() method to set a custom validation message. This will prevent the form from submitting and make the browser display the error message you provide. To clear the error, call this function with an empty string.
API
Importing
If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.
Import this component directly from the CDN:
import 'https://ka-f.webawesome.com/[email protected]/components/checkbox/checkbox.js';
After installing Web Awesome via npm, import this component:
import '@awesome.me/webawesome/dist/components/checkbox/checkbox.js';
If you're self-hosting Web Awesome, import this component from your server:
import './webawesome/dist/components/checkbox/checkbox.js';
To import this component for React 18 or below, use the following code:
import WaCheckbox from '@awesome.me/webawesome/dist/react/checkbox/index.js';
Slots
Learn more about using slots.
| Name | Description |
|---|---|
| (default) | The checkbox's label. |
hint
|
Text that describes how to use the checkbox. Alternatively, you can use the hint attribute. |
Attributes & Properties
Learn more about attributes and properties.
| Name | Description | Reflects |
|---|---|---|
checked |
Draws the checkbox in a checked state.
|
|
defaultCheckedchecked |
The default value of the form control. Primarily used for resetting the form control.
Type
boolean
|
|
disableddisabled |
Disables the checkbox.
Type
boolean
Default
false
|
|
form |
By default, form controls are associated with the nearest containing
<form> element. This attribute allows you
to place the form control outside of a form and associate it with the form that has this id. The form must be in
the same document or shadow root for this to work.
Type
HTMLFormElement | null
|
|
hinthint |
The checkbox's hint. If you need to display HTML, use the
hint slot instead.
Type
string
Default
''
|
|
indeterminateindeterminate |
Draws the checkbox in an indeterminate state. This is usually applied to checkboxes that represents a "select
all/none" behavior when associated checkboxes have a mix of checked and unchecked states.
Type
boolean
Default
false
|
|
namename |
The name of the input, submitted as a name/value pair with form data.
Type
string | null
Default
null
|
|
requiredrequired |
Makes the checkbox a required field.
Type
boolean
Default
false
|
|
sizesize |
The checkbox's size.
Type
'xs' | 's' | 'm' | 'l' | 'xl'
Default
'm'
|
|
valuevalue |
The value of the checkbox, submitted as a name/value pair with form data.
Type
string | null
|
|
Methods
Learn more about methods.
| Name | Description | Arguments |
|---|---|---|
blur() |
Removes focus from the checkbox. | |
click() |
Simulates a click on the checkbox. | |
focus() |
Sets focus on the checkbox. |
options: FocusOptions
|
setCustomValidity() |
Sets a custom validation message. While the message is set, the form control is invalid and the message is shown when validity is reported. Pass an empty string to clear it. |
message: string
|
Events
Learn more about events.
| Name | Description |
|---|---|
blur |
Emitted when the checkbox loses focus. |
change |
Emitted when the checked state changes. |
focus |
Emitted when the checkbox gains focus. |
input |
Emitted when the checkbox receives input. |
wa-invalid |
Emitted when the form control has been checked for validity and its constraints aren't satisfied. |
CSS Custom Properties
Learn more about CSS custom properties.
| Name | Description |
|---|---|
--checked-icon-color |
The color of the checked and indeterminate icons.
|
--checked-icon-scale |
The size of the checked and indeterminate icons relative to the checkbox.
|
Custom States
Learn more about custom states.
| Name | Description | CSS selector |
|---|---|---|
checked |
Applied when the checkbox is checked. |
:state(checked)
|
disabled |
Applied when the checkbox is disabled. |
:state(disabled)
|
indeterminate |
Applied when the checkbox is in an indeterminate state. |
:state(indeterminate)
|
invalid |
Applied when the form control fails validation. |
:state(invalid)
|
optional |
Applied when the form control is not required. |
:state(optional)
|
required |
Applied when the form control is required. |
:state(required)
|
user-invalid |
Applied when the form control fails validation after the user has interacted with it. |
:state(user-invalid)
|
user-valid |
Applied when the form control passes validation after the user has interacted with it. |
:state(user-valid)
|
valid |
Applied when the form control passes validation. |
:state(valid)
|
CSS Parts
Learn more about CSS parts.
| Name | Description | CSS selector |
|---|---|---|
checkbox |
The component's outer wrapper. |
::part(checkbox)
|
checked-icon |
The checked icon, a <wa-icon> element. |
::part(checked-icon)
|
control |
The square container that wraps the checkbox's checked state. |
::part(control)
|
hint |
The hint's wrapper. |
::part(hint)
|
indeterminate-icon |
The indeterminate icon, a <wa-icon> element. |
::part(indeterminate-icon)
|
label |
The container that wraps the checkbox's label. |
::part(label)
|
base |
Deprecated. Use the checkbox part instead. |
::part(base)
|
Dependencies
This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.