@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"
/>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 #
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | boolean | "indeterminate" | β | Controlled state |
| defaultChecked | boolean | "indeterminate" | false | Uncontrolled initial state |
| onChange | (checked: boolean) => void | β | Always emits boolean |
| value | string | β | Value used when inside a CheckboxGroup |
| size | "sm" | "md" | "lg" | "md" | Size |
| tone | "neutral" | "primary" | "success" | "danger" | "primary" | Color when checked |
| label | ReactNode | β | Label |
| description | ReactNode | β | Helper text below the label |
| error | ReactNode | β | Error message β flips tone to danger and renders below |
| labelPosition | "left" | "right" | "right" | Label side |
| card | boolean | false | Bordered card; whole row becomes clickable |
| required | boolean | false | Append a red asterisk to the label |
| invalid | boolean | false | Force invalid (danger) state without inline error text |
| disabled | boolean | false | Disable |
| name | string | β | Native form name attribute |
| className | string | β | Extra class on the root element |
| style | CSSProperties | β | Inline style override |