react ~3 KB 0 deps v0.4.0 β†— GitHub β†—

@mshafiqyajid/react-checkbox

Headless checkbox hook + styled component. Indeterminate (tri-state) support, three sizes, four tones, label position, full keyboard support (Space toggles), animated check + indeterminate icons.

Playground #

I agree to the terms
Props
TSX
import { CheckboxStyled } from "@mshafiqyajid/react-checkbox/styled";
import "@mshafiqyajid/react-checkbox/styles.css";

<CheckboxStyled
  checked={checked}
  onChange={setChecked}
  label="I agree to the terms"
/>
Notify me about…Choose the updates you want to receive.
Email updates
SMS alerts
Push notifications
Props
TSX
import { CheckboxGroup, CheckboxStyled } from "@mshafiqyajid/react-checkbox/styled";
import "@mshafiqyajid/react-checkbox/styles.css";

<CheckboxGroup
  value={groupValues}
  onChange={setGroupValues}
  label="Notify me about…"
/>

Install #

npm install @mshafiqyajid/react-checkbox

Quick start #

import { CheckboxStyled } from "@mshafiqyajid/react-checkbox/styled";
import "@mshafiqyajid/react-checkbox/styles.css";

<CheckboxStyled label="I agree to the terms" tone="primary" />

Indeterminate #

Pass "indeterminate" to checked or defaultChecked for tri-state. Clicking from indeterminate moves to checked. onChange always emits boolean (never "indeterminate") β€” typically you choose what state to land in based on the action that triggered the indeterminate value.

<CheckboxStyled
  checked="indeterminate"
  onChange={(c) => console.log(c)}
/>

Headless #

import { useCheckbox } from "@mshafiqyajid/react-checkbox";

const { checkboxProps, isChecked, isIndeterminate } = useCheckbox({
  defaultChecked: false,
});

return (
  <button {...checkboxProps}>
    {isIndeterminate ? "β€”" : isChecked ? "βœ“" : ""}
  </button>
);

CheckboxGroup #

Wrap multiple checkboxes in CheckboxGroup to manage them as a set. Each checkbox must have a value prop. The group tracks a string[] of checked values.

import { CheckboxGroup, CheckboxStyled } from "@mshafiqyajid/react-checkbox/styled";

const [values, setValues] = useState<string[]>(["a"]);

<CheckboxGroup value={values} onChange={setValues}>
  <CheckboxStyled value="a" label="Option A" />
  <CheckboxStyled value="b" label="Option B" />
  <CheckboxStyled value="c" label="Option C" />
</CheckboxGroup>

API #

PropTypeDefaultDescription
checkedboolean | "indeterminate"β€”Controlled state
defaultCheckedboolean | "indeterminate"falseUncontrolled initial state
onChange(checked: boolean) => voidβ€”Always emits boolean
valuestringβ€”Value used when inside a CheckboxGroup
size"sm" | "md" | "lg""md"Size
tone"neutral" | "primary" | "success" | "danger""primary"Color when checked
labelReactNodeβ€”Label
descriptionReactNodeβ€”Helper text below the label
errorReactNodeβ€”Error message β€” flips tone to danger and renders below
labelPosition"left" | "right""right"Label side
cardbooleanfalseBordered card; whole row becomes clickable
requiredbooleanfalseAppend a red asterisk to the label
invalidbooleanfalseForce invalid (danger) state without inline error text
disabledbooleanfalseDisable
namestringβ€”Native form name attribute
classNamestringβ€”Extra class on the root element
styleCSSPropertiesβ€”Inline style override
Edit this page on GitHub