Skip to content

Checkbox

Selects any number of options, with checked, unchecked and indeterminate states.

import { Checkbox } from '@windmorph/react';

Props

By name or description, across the 37 props below

Checkbox 37 props

  • checkedtype boolean

    Controlled checked state.

  • defaultCheckedtype boolean

    Initial checked state when uncontrolled.

  • indeterminatetype booleandefault false

    Shows a dash, announced as mixed: some, but not all, of a group is checked. The checked state underneath doesn't change.

  • disabledtype booleandefault false

    Turns the checkbox off: it can't be changed or focused.

  • readOnlytype booleandefault false

    The checkbox can be focused but not changed.

  • requiredtype booleandefault false

    The checkbox must be checked (aria-required; with validationBehavior="native", the form can't submit until it is).

  • invalidtype booleandefault false

    Shows the error state.

  • densitytype 0 | -1 | -2 | -3default 0

    Web density, 0 to -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 nearest ThemeProvider or ThemeScope. M3 asks for density to be the reader's choice rather than a default, so prefer setting it there, from a setting.

  • controlPlacementtype "start" | "end"default "start"

    Where the box sits beside its label: start (before it) or end (after it, the label growing to fill the row, as a settings row's). As ListItem's controlPlacement. With end the root carries data-control-placement="end", for styling.

  • reftype Ref<HTMLLabelElement>

    The root <label>. Use inputRef for the checkbox input.

  • inputReftype Ref<HTMLInputElement>

    Ref to the visually hidden checkbox <input>.

Events

  • onCheckedChangetype ((checked: boolean) => void)

    Called when the user checks or unchecks the checkbox.

Text

  • aria-labeltype string

    Defines a string value that labels the current element.

  • aria-labelledbytype string

    Identifies the element (or elements) that labels the current element.

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.

Checkbox tokens (md-comp-checkbox)
VariableDefaultPaintsCompose
--md-comp-checkbox-selected-container-colorvar(--md-sys-color-primary)The box, checked or indeterminate.CheckboxTokens.SelectedContainerColor
--md-comp-checkbox-selected-icon-colorvar(--md-sys-color-on-primary)The check mark and the dash.CheckboxTokens.SelectedIconColor
--md-comp-checkbox-unselected-outline-colorvar(--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.

  • 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.

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 is on-surface-variant, darkening to on-surface on hover, focus and press; a checked or indeterminate box has no outline, so an invalid one is a plain error fill.
  • 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-error roles; disabled-checked is on-surface at 38% with a surface check.
  • 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 name covers forms.