Checkbox
Selects any number of options, with checked, unchecked and indeterminate states.
import { Checkbox } from '@windmorph/react';Props
Checkbox 37 props
Controlled checked state.
Initial checked state when uncontrolled.
Shows a dash, announced as mixed: some, but not all, of a group is checked. The checked state underneath doesn't change.
Turns the checkbox off: it can't be changed or focused.
The checkbox can be focused but not changed.
The checkbox must be checked (
aria-required; withvalidationBehavior="native", the form can't submit until it is).Shows the error state.
Web density,
0to-3: each step takes 4px off the round state layer (40px at 0, 28px at -3); the box stays 18px and the touch target 48px. Left out, the checkbox takes the density of the nearestThemeProviderorThemeScope. M3 asks for density to be the reader's choice rather than a default, so prefer setting it there, from a setting.Where the box sits beside its label:
start(before it) orend(after it, the label growing to fill the row, as a settings row's). AsListItem'scontrolPlacement. Withendthe root carriesdata-control-placement="end", for styling.The root
<label>. UseinputReffor the checkbox input.Ref to the visually hidden checkbox
<input>.
Events
Called when the user checks or unchecks the checkbox.
Text
Defines a string value that labels the current element.
Identifies the element (or elements) that labels the current element.
Classes for the root
<label>, merged withclassNames.root; consumer classes win.Classes for each part, by slot.
classNames.rootis merged withclassName.The 18px box that draws the check mark or dash.
The check or dash drawn in the box (an
<svg>stroked withcurrentColor).The root
<label>that holds the control and its label text.The 40px control that carries the state layer and holds the indicator.
The label text beside the control.
Inline styles for the root
<label>.
The value of the input element, used when submitting an HTML form. See MDN.
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.
A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if
validationBehavior="native". For realtime validation, use theinvalidprop instead.Whether the element should receive focus on render.
Handler that is called when the element receives focus.
Handler that is called when the element loses focus.
Handler that is called when the element's focus status changes.
Handler that is called when a key is pressed.
Handler that is called when a key is released.
The name of the input element, used when submitting an HTML form. See MDN.
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN.The element's unique identifier. See MDN.
Identifies the element (or elements) that describes the object.
Identifies the element (or elements) that provide a detailed, extended description for the object.
Identifies the element that provides an error message for the object.
Handler that is called when the press is released over the target.
Handler that is called when a press interaction starts.
Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target.
Handler that is called when the press state changes.
Handler that is called when a press is released over the target, regardless of whether it started on the target or not.
Also accepts the root element's HTML attributes (id, data-*, event handlers).
Tokens
M3's component tokens as CSS variables. Each is unset until you set it, on :root, a theme or any ancestor, and falls back to the default shown. The root carries data-variant (and data-size where it has sizes), which picks the variables that apply. See Component tokens in the Customisation guide.
| Variable | Default | Paints | Compose |
|---|---|---|---|
--md-comp-checkbox-selected-container-color | var(--md-sys-color-primary) | The box, checked or indeterminate. | CheckboxTokens.SelectedContainerColor |
--md-comp-checkbox-selected-icon-color | var(--md-sys-color-on-primary) | The check mark and the dash. | CheckboxTokens.SelectedIconColor |
--md-comp-checkbox-unselected-outline-color | var(--md-sys-color-on-surface-variant) | The 2px outline of an unchecked box. | CheckboxTokens.UnselectedOutlineColor |
Spec status
Built to the M3 Expressive checkbox spec. Each item of its variants and configurations links to a live example; where the M3 Expressive column of the spec differs from the original M3 one, it wins.
| Item | Status | Details |
|---|---|---|
| Unselected and selected | Built | Configuration · Basic |
| Indeterminate | Built | Configuration · Indeterminate |
| Error | Built | Configuration · States |
| Disabled | Built | Configuration · States |
| Select several items, with a parent | Built | Use case · Indeterminate |
| Density (web, chosen by the reader) | Built | Use case · Density |
| Check to dash | Differs | The indeterminate dash draws on its own instead of morphing from the check, because Safari can't animate a path's shape in CSS. |
Unselected and selected
BuiltConfiguration · Basic
Indeterminate
BuiltConfiguration · Indeterminate
Error
BuiltConfiguration · States
Disabled
BuiltConfiguration · States
Select several items, with a parent
BuiltUse case · Indeterminate
Density (web, chosen by the reader)
BuiltUse case · Density
Check to dash
DiffersThe indeterminate dash draws on its own instead of morphing from the check, because Safari can't animate a path's shape in CSS.
Bundle size
Gzipped, as published: the component and the library code it imports. React, React Aria, Motion and the other dependencies aren't counted, and your bundler's minifier makes it smaller still. CI fails a change that grows it past its budget.
Checkbox: 13.4 kB
Differences from Compose
- Uses Compose's M3 styling (
isCheckboxStylingFixEnabled = true), which Compose itself defaults to off (keeping M2 styling): an 18px box with 2px corners and a 2px outline. The outline ison-surface-variant, darkening toon-surfaceon hover, focus and press; a checked or indeterminate box has no outline, so an invalid one is a plainerrorfill. - Motion: the check draws on the default spatial spring and snaps away 100ms after unchecking; the box fills on default effects and empties on fast effects.
- Deviation: Compose morphs the check into the indeterminate dash, but CSS can't animate a path shape in Safari, so the dash draws on its own.
- Error uses the
error/on-errorroles; disabled-checked ison-surfaceat 38% with asurfacecheck. controlPlacement="end"puts the box after its label, the label filling the row, as a settings row's; the whole row stays the label, so a press anywhere on it toggles the box.- No CheckboxGroup yet (Compose has none); a fieldset of checkboxes with a shared
namecovers forms.