A checkbox lets people select one or more items from a list, or turn one item on or off in a form that is saved later. It is selected, unselected or indeterminate (a parent whose children are partly selected), and any of them in error. For a setting that takes effect at once, use a switch.
Usage
import { createCheckbox } from 'material';
const checkbox = createCheckbox({ label: 'I accept the terms', name: 'terms' });
checkbox.on('change', ({ checked }) => acceptTerms(checked));
document.body.append(checkbox.element);
<m-checkbox name="terms">I accept the terms</m-checkbox>
<script type="module">
const checkbox = document.querySelector('m-checkbox');
checkbox.addEventListener('change', (event) => acceptTerms(event.detail.checked));
</script>
import { Checkbox } from 'material/react';
import { acceptTerms } from './app';
export function Example() {
return (
<Checkbox onChange={(event) => acceptTerms(event.detail.checked)} name="terms">I accept the terms</Checkbox>
);
}
<script setup lang="ts">
import { MCheckbox } from 'material/vue';
import { acceptTerms } from './app';
</script>
<template>
<MCheckbox @change="acceptTerms($event.detail.checked)" name="terms">I accept the terms</MCheckbox>
</template>
<script lang="ts">
import { Checkbox } from 'material/svelte';
import { acceptTerms } from './app';
</script>
<Checkbox onchange={(event) => acceptTerms(event.detail.checked)} name="terms">I accept the terms</Checkbox>
import { Checkbox } from 'material/solid';
import { acceptTerms } from './app';
export function Example() {
return (
<Checkbox onChange={(event) => acceptTerms(event.detail.checked)} name="terms">I accept the terms</Checkbox>
);
}
Examples
Indeterminate
A parent checkbox is indeterminate while some of its children are selected. Checking it
selects every child, unchecking it clears them; keep it unchecked while indeterminate, so a
click checks everything. Its children are the app's to keep in step: check(), uncheck() and
setValue() clear the indeterminate state, and emit change when the state changes, without
the nativeEvent a user's click carries.
import { createCheckbox } from 'material';
const checkbox = createCheckbox({ label: 'Additions', indeterminate: true });
document.body.append(checkbox.element);
<m-checkbox>Additions</m-checkbox>
<script type="module">
const checkbox = document.querySelector('m-checkbox');
checkbox.indeterminate = true;
</script>
import { Checkbox } from 'material/react';
export function Example() {
return (
<Checkbox indeterminate>Additions</Checkbox>
);
}
<script setup lang="ts">
import { MCheckbox } from 'material/vue';
</script>
<template>
<MCheckbox indeterminate>Additions</MCheckbox>
</template>
<script lang="ts">
import { Checkbox } from 'material/svelte';
</script>
<Checkbox indeterminate>Additions</Checkbox>
import { Checkbox } from 'material/solid';
export function Example() {
return (
<Checkbox indeterminate>Additions</Checkbox>
);
}
Required, in error
error draws the error colors and sets aria-invalid; set it when a required checkbox is left
unselected.
import { createCheckbox } from 'material';
const checkbox = createCheckbox({ label: 'Share usage data', required: true, error: true });
document.body.append(checkbox.element);
function clearError() {
checkbox.setError(false);
}
<m-checkbox required error>Share usage data</m-checkbox>
<script type="module">
const checkbox = document.querySelector('m-checkbox');
function clearError() {
checkbox.toggleAttribute('error', false);
}
</script>
import { useState } from 'react';
import { Checkbox } from 'material/react';
export function Example() {
const [error, setError] = useState(true);
const clearError = () => setError(false);
return (
<Checkbox required error={error}>Share usage data</Checkbox>
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MCheckbox } from 'material/vue';
const error = ref(true);
function clearError() {
error.value = false;
}
</script>
<template>
<MCheckbox required :error="error">Share usage data</MCheckbox>
</template>
<script lang="ts">
import { Checkbox } from 'material/svelte';
let error = $state(true);
function clearError() {
error = false;
}
</script>
<Checkbox required error={error}>Share usage data</Checkbox>
import { createSignal } from 'solid-js';
import { Checkbox } from 'material/solid';
export function Example() {
const [error, setError] = createSignal(true);
const clearError = () => setError(false);
return (
<Checkbox required error={error()}>Share usage data</Checkbox>
);
}
A parent with its children, from the M3 guidelines, is planned for Showcase.
API
Options
labelThe label, which names the checkbox; a click on it toggles too
stringundefinedlabelPositionWhich side of the box the label sits on, in the reading direction
'start' | 'end''end'checkedWhether it starts selected
booleanfalseindeterminateWhether it starts indeterminate
booleanfalseerrorThe error state: error outline, container and state layers, and aria-invalid
booleanfalsedisabledWhether it starts disabled
booleanfalsenameThe input's name, for forms
stringundefinedvalueThe value submitted when selected
string'on'requiredWhether the form requires it selected
booleanfalseariaLabelAccessible name when there is no visible label
stringundefinedclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'Methods
check() / uncheck() / toggle()Changes the state, clears indeterminate, and emits change when the state changes
CheckboxComponentisChecked()Whether it is selected
booleansetIndeterminate(state)Sets or clears the indeterminate state
state: booleanCheckboxComponentsetError(error)Sets or clears the error state
error: booleanCheckboxComponentgetValue()The selected state
booleansetValue(value)Selects or clears it; the strings 'true' and '1' select
value: boolean | stringCheckboxComponentgetValueAttribute() / setValueAttribute(value)The input's value attribute
value: stringstring / CheckboxComponentgetLabel() / setLabel(text)The label
text: stringstring / CheckboxComponentenable() / disable()The disabled state
CheckboxComponenton(event, handler) / off(event, handler)Adds or removes a listener
event: 'change', handler: FunctionCheckboxComponentdestroy()Removes the checkbox
voidEvents
changeThe state changed
{ checked, value, valueAttribute, nativeEvent? }value is the boolean, the same as getValue(). valueAttribute is the input's value attribute, submitted with a form. nativeEvent is there when the user toggled it, not for the methods. The web component's
change carries { checked, value, valueAttribute }.
Styling
.mtrl-checkbox { } /* the root */
.mtrl-checkbox--indeterminate, .mtrl-checkbox--error, .mtrl-checkbox--disabled { }
.mtrl-checkbox--label-start, .mtrl-checkbox--label-end { }
.mtrl-checkbox__input { } /* the native input, over the whole checkbox */
.mtrl-checkbox__icon { } /* the box; ::before is the state layer, ::after the dash */
.mtrl-checkbox__label { }
Measurements
From the m3.material.io checkbox specs, then Compose's CheckboxTokens and material-web.
| Attribute | Value |
|---|---|
| Box | 18dp, 2dp corner, 2dp outline |
| Unselected | No fill, on-surface-variant outline (on-surface on hover, focus and press) |
| Selected and indeterminate | primary container, on-primary check or dash |
| Error | error outline and container, on-error check |
| Disabled | on-surface 38% outline or container, surface check and dash |
| State layer | 40dp circle: on-surface when unselected, primary when selected; a press takes the color of the state it leads to |
| Touch target | 48dp |
| Label | Body Large, on-surface, 12dp from the box |
| Motion | The check draws in on the default spatial spring and leaves at once |