Search
MaterialComponentsDocs
Components overviewApp barBadgeButtonsButton groupButtonExtended FABFAB menuFABIcon buttonSplit buttonCardCarouselCheckboxChipsDate & time pickersDate pickerTime pickerDialogDividerListLoading & progressLoading indicatorProgressMenuNavigationNavigation barNavigation drawerNavigation railRadio buttonsSearchSelectSheetsBottom sheetSide sheetSliderSnackbarSwitchTabsText fieldToolbarTooltip

Checkbox

Make a choice, or represent a partial selection. Explore checkbox states, labels, and form behavior.

Additions
PicklesTomato
FactoryWeb ComponentsReactVueSvelteSolidJS
PublishedUpdated

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

Vanilla
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);
Web Components
<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>
React
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>
  );
}
Vue
<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>
Svelte
<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>
SolidJS
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.

Vanilla
import { createCheckbox } from 'material';

const checkbox = createCheckbox({ label: 'Additions', indeterminate: true });
document.body.append(checkbox.element);
Web Components
<m-checkbox>Additions</m-checkbox>

<script type="module">
  const checkbox = document.querySelector('m-checkbox');
  checkbox.indeterminate = true;
</script>
React
import { Checkbox } from 'material/react';

export function Example() {
  return (
    <Checkbox indeterminate>Additions</Checkbox>
  );
}
Vue
<script setup lang="ts">
import { MCheckbox } from 'material/vue';
</script>

<template>
  <MCheckbox indeterminate>Additions</MCheckbox>
</template>
Svelte
<script lang="ts">
  import { Checkbox } from 'material/svelte';
</script>

<Checkbox indeterminate>Additions</Checkbox>
SolidJS
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.

Vanilla
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);
}
Web Components
<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>
React
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>
  );
}
Vue
<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>
Svelte
<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>
SolidJS
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

label

The label, which names the checkbox; a click on it toggles too

Typestring
Defaultundefined
labelPosition

Which side of the box the label sits on, in the reading direction

Type'start' | 'end'
Default'end'
checked

Whether it starts selected

Typeboolean
Defaultfalse
indeterminate

Whether it starts indeterminate

Typeboolean
Defaultfalse
error

The error state: error outline, container and state layers, and aria-invalid

Typeboolean
Defaultfalse
disabled

Whether it starts disabled

Typeboolean
Defaultfalse
name

The input's name, for forms

Typestring
Defaultundefined
value

The value submitted when selected

Typestring
Default'on'
required

Whether the form requires it selected

Typeboolean
Defaultfalse
ariaLabel

Accessible name when there is no visible label

Typestring
Defaultundefined
class

Additional CSS classes

Typestring
Defaultundefined
prefix

Prefix for CSS class names

Typestring
Default'mtrl'

Methods

check() / uncheck() / toggle()

Changes the state, clears indeterminate, and emits change when the state changes

Parametersnone
ReturnsCheckboxComponent
isChecked()

Whether it is selected

Parametersnone
Returnsboolean
setIndeterminate(state)

Sets or clears the indeterminate state

Parametersstate: boolean
ReturnsCheckboxComponent
setError(error)

Sets or clears the error state

Parameterserror: boolean
ReturnsCheckboxComponent
getValue()

The selected state

Parametersnone
Returnsboolean
setValue(value)

Selects or clears it; the strings 'true' and '1' select

Parametersvalue: boolean | string
ReturnsCheckboxComponent
getValueAttribute() / setValueAttribute(value)

The input's value attribute

Parametersvalue: string
Returnsstring / CheckboxComponent
getLabel() / setLabel(text)

The label

Parameterstext: string
Returnsstring / CheckboxComponent
enable() / disable()

The disabled state

Parametersnone
ReturnsCheckboxComponent
on(event, handler) / off(event, handler)

Adds or removes a listener

Parametersevent: 'change', handler: Function
ReturnsCheckboxComponent
destroy()

Removes the checkbox

Parametersnone
Returnsvoid

Events

change

The state changed

Payload{ 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