Back to Onyx

Checkbox

web/lib/opal/src/components/checkbox/README.md

3.3.02.5 KB
Original Source

Checkbox

Import: import { Checkbox, type CheckboxProps } from "@opal/components";

A dual-element checkbox with custom styling. Uses a hidden native <input> for form state and a visible <div> for the visual surface. Supports controlled, uncontrolled, indeterminate, and disabled modes.

Props

PropTypeDefaultDescription
checkedbooleanControlled checked state
defaultCheckedbooleanfalseInitial state for uncontrolled mode
onCheckedChange(checked: boolean) => voidCalled when the checked state changes
indeterminatebooleanfalseShows a dash instead of a check
disabledbooleanfalsePrevents interaction
classNamestringAdditional classes for the visual surface
idstringPassed to the hidden <input> for label association
namestringForm field name

All remaining props are forwarded to the hidden <input> element.

Styles

Visual states are driven by data-* attributes on the surface element and defined in styles.css:

AttributeValuesDescription
data-state"unchecked" | "checked" | "indeterminate"Current checkbox state
data-disabledpresent / absentDisables interaction and applies muted colors

Color matrix

StateBackgroundBorder
Uncheckedbackground-neutral-00border-02 (hover: border-03)
Checked / Indeterminateaction-link-05 (hover: action-link-04)
Disabled uncheckedbackground-neutral-03border-02
Disabled checkedbackground-neutral-04

CSS classes

ClassElementDescription
.opal-checkboxRoot wrapperinline-flex shrink-0
.opal-checkbox-surfaceVisual surface16x16, rounded, state-driven colors
.opal-checkbox-inputHidden <input>Screen-reader only
.opal-checkbox-iconCheck / dash icon12x12, stroke-text-light-05

Usage Examples

tsx
import { Checkbox } from "@opal/components";

// Uncontrolled
<Checkbox onCheckedChange={(checked) => console.log(checked)} />

// Controlled
<Checkbox checked={isChecked} onCheckedChange={setIsChecked} />

// With label
<div className="flex items-center gap-2">
  <Checkbox id="terms" />
  <label htmlFor="terms">Accept terms</label>
</div>

// Indeterminate (e.g. "select all" with partial selection)
<Checkbox indeterminate />

// Disabled
<Checkbox disabled checked />