Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

InputsReact

Checkbox

An easily stylable checkbox.

Rendered by React 19.3.1

Loading React…
import * as React from 'react'
import { Checkbox } from '@hamur/react/checkbox'

export default function CheckboxExample() {
  const [accepted, setAccepted] = React.useState(false)
  return (
    <div className="stack">
      <label className="row">
        <Checkbox.Root className="checkbox" onCheckedChange={setAccepted}>
          <Checkbox.Indicator className="checkbox-indicator">
            ✓
          </Checkbox.Indicator>
        </Checkbox.Root>
        Accept the terms
      </label>
      <label className="row">
        <Checkbox.Root className="checkbox" defaultChecked>
          <Checkbox.Indicator className="checkbox-indicator">
            ✓
          </Checkbox.Indicator>
        </Checkbox.Root>
        Send me updates
      </label>
      <label className="row">
        <Checkbox.Root className="checkbox" disabled>
          <Checkbox.Indicator className="checkbox-indicator">
            ✓
          </Checkbox.Indicator>
        </Checkbox.Root>
        Disabled
      </label>
      <p className="muted">The terms are {accepted ? 'accepted' : 'not accepted'}</p>
    </div>
  )
}
270Tests passing in React · 0 failing
10.4 KiBSize
Not measured yetMemory
Not measured yetSpeed
productdevbook and 18 upstream authors14 commits · last

Installation

npm i @hamur/react

Not on npm yet. The package goes out under the chosen name.

Anatomy

Import the component and assemble its parts:

<Checkbox.Root>
  <Checkbox.Indicator />
</Checkbox.Root>

Examples

The demo at the top is the one example this component has so far.

API reference

16 props across 2 parts, in React syntax. className, style, render are listed once, under Shared props.

IndicatorCheckbox.Indicator

Indicates whether the checkbox is ticked. Renders a <span>.

Indicator props
PropTypeDefault
keepMountedWhether to keep the element in the DOM when the checkbox is not checked.booleanfalse
Indicator data attributes
Data attributeDescription
data-checkedPresent when the checkbox is checked.
data-uncheckedPresent when the checkbox is not checked.
data-indeterminatePresent when the checkbox is in an indeterminate state.
data-disabledPresent when the checkbox is disabled.
data-readonlyPresent when the checkbox is readonly.
data-requiredPresent when the checkbox is required.
data-starting-stylePresent when the checkbox indicator begins animating in.
data-ending-stylePresent when the checkbox indicator is animating out.
data-validPresent when the checkbox is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the checkbox is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the checkbox has been touched (when wrapped in Field.Root).
data-dirtyPresent when the checkbox's value has changed (when wrapped in Field.Root).
data-filledPresent when the checkbox is checked (when wrapped in Field.Root).
data-focusedPresent when the checkbox is focused (when wrapped in Field.Root).

RootCheckbox.Root

Represents the checkbox itself. and a hidden <input> beside. Renders a <span>.

Root props
PropTypeDefault
nameIdentifies the field when a form is submitted.stringundefined
defaultCheckedWhether the checkbox is initially ticked.booleanfalse
checkedWhether the checkbox is currently ticked.booleanundefined
onCheckedChangeEvent handler called when the checkbox is ticked or unticked.((checked: boolean, eventDetails: { reason: "none"; event: Event; cancel: () => void; allowPropagation: () => void; isCanceled: boolean; isPropagationAllowed: boolean; trigger: Element | undefined; }) => void)—
indeterminateWhether the checkbox is in a mixed state: neither ticked, nor unticked.booleanfalse
valueThe checkbox's value.string—
formIdentifies the form that owns the hidden input.string—
nativeButtonWhether the component renders a native <button> element when replacing itbooleanfalse
parentWhether the checkbox controls a group of child checkboxes.booleanfalse
uncheckedValueThe value submitted with the form when the checkbox is unchecked.string—
disabledWhether the component should ignore user interaction.booleanfalse
readOnlyWhether the user should be unable to tick or untick the checkbox.booleanfalse
requiredWhether the user must tick the checkbox before submitting a form.booleanfalse
inputRefA ref to access the hidden <input> element.React.Ref<HTMLInputElement>—
idThe id of the input element.string—
Root data attributes
Data attributeDescription
data-checkedPresent when the checkbox is checked.
data-uncheckedPresent when the checkbox is not checked.
data-indeterminatePresent when the checkbox is in an indeterminate state.
data-disabledPresent when the checkbox is disabled.
data-readonlyPresent when the checkbox is readonly.
data-requiredPresent when the checkbox is required.
data-validPresent when the checkbox is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the checkbox is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the checkbox has been touched (when wrapped in Field.Root).
data-dirtyPresent when the checkbox's value has changed (when wrapped in Field.Root).
data-filledPresent when the checkbox is checked (when wrapped in Field.Root).
data-focusedPresent when the checkbox is focused (when wrapped in Field.Root).

Shared props

Every part that renders an element takes these.

Shared props
PropType
classNameCSS class applied to the element, or a function that returns a class based on the component's state.string | ((state: State) => string | undefined)
styleStyle applied to the element, or a function that returns a style object based on the component's state.React.CSSProperties | ((state: State) => React.CSSProperties | undefined)
renderAllows you to replace the component's HTML element with a different tag, or compose it with another component.React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State>

Keyboard and accessibility

Keyboard

Keyboard
KeysDoesPart
SpaceChecks or unchecks the boxRoot
EnterDoes not toggle; submits the form the checkbox belongs to, as a native checkbox doesRoot

Accessibility

What the parts set
PartSetsSo that
Rootrole="checkbox", aria-checked ("mixed" while indeterminate)a screen reader reads it as a checkbox and its state
Rootaria-readonly, aria-required from readOnly and requiredthe constraints are announced
Rootaria-labelledby to an associated labela <label> or Field.Label names the checkbox
Roota hidden <input type="checkbox"> with aria-hidden="true" and tabindex="-1"the form gets the value without a second control to read or tab to
Indicatorrenders a <span> with your markthe tick is decoration; the state is on Root

Focus order

  1. Root is one tab stop. The hidden input is skipped.