An accessible, themeable checkbox control that supports checked, indeterminate, disabled, read-only, and required states. Fires cancelable request events before state changes, allowing host code to intercept.
<x-checkbox></x-checkbox>| Attribute | Type | Default | Description |
|---|---|---|---|
checked |
boolean | false |
Whether the checkbox is checked. Setting checked="false" is treated as absent (unchecked). |
indeterminate |
boolean | false |
Indeterminate (mixed) state — overrides checked visually |
disabled |
boolean | false |
Disables interaction |
readonly |
boolean | false |
Prevents user toggling (still focusable) |
required |
boolean | false |
Marks the field as required; the element is invalid when unchecked |
name |
string | — | Form field name |
value |
string | "on" |
Value submitted with form data when checked |
aria-label |
string | — | Accessible label |
aria-describedby |
string | — | References a describing element |
aria-labelledby |
string | — | References a labelling element |
| Property | Type | Reflects attribute |
|---|---|---|
checked |
boolean | checked |
indeterminate |
boolean | indeterminate |
disabled |
boolean | disabled |
readOnly |
boolean | readonly |
required |
boolean | required |
name |
string | name |
value |
string | value |
| Event | Cancelable | Detail |
|---|---|---|
x-checkbox-change-request |
yes | { value, previousChecked, nextChecked } — call preventDefault() to block the toggle |
x-checkbox-change |
no | { value, checked } — fired after state has changed |
Both events bubble and are composed.
- When
indeterminateis set, clicking always transitions tochecked=true, indeterminate=false. - When checked, clicking transitions to
checked=false, indeterminate=false. - When unchecked, clicking transitions to
checked=true, indeterminate=false.
formAssociatedistrue; the element participates in form submission.- The submitted value is the
valueattribute (default"on") when checked; nothing is submitted when unchecked. - On form reset,
checkedandindeterminateattributes are removed. Thevalueattribute is not restored by reset (matching native<input type="checkbox">behaviour).
Like a native form control, x-checkbox exposes read-only validity, validationMessage, willValidate, form and labels, plus checkValidity() and reportValidity(). All seven delegate to the element's ElementInternals, so el.checkValidity() answers the same question a submit attempt would.
- The internal
<button part="control">carriesrole="checkbox"andaria-checkedset to"true","false", or"mixed". tabindex="0"on the control when enabled;tabindex="-1"when disabled.aria-disabled="true"on the control when disabled.aria-required="true"on the control when required.aria-readonly="true"on the control when readonly.aria-label,aria-labelledby, andaria-describedbyare forwarded from the host attribute to the control.- Keyboard:
Spacetoggles;Entertoggles (unless readonly/disabled).
<x-checkbox></x-checkbox>
<x-checkbox checked></x-checkbox>
<x-checkbox indeterminate></x-checkbox>
<x-checkbox disabled></x-checkbox>
<x-checkbox checked disabled></x-checkbox><label>
<x-checkbox name="agree" value="yes" required></x-checkbox>
I agree to the terms
</label>document.querySelector('x-checkbox').addEventListener('x-checkbox-change', e => {
console.log('checked:', e.detail.checked, 'value:', e.detail.value);
});checkbox.addEventListener('x-checkbox-change-request', e => {
if (!canToggle) e.preventDefault();
});[:x-checkbox {:checked "" :name "agree" :value "yes"
:on-x-checkbox-change
(fn [e]
(swap! state assoc :agreed (.. e -detail -checked)))}]