A checkbox (WAI-ARIA Checkbox pattern, two-state and mixed). It renders a real <input type="checkbox"> and behaves like Switch: optimistic checked in the DOM, data-pending while checkedChangeAction runs, native forms. It adds a controlled indeterminate state for "select all" parents.
"use client";
import { startTransition, useOptimistic, useRef, useState } from "react";
import { Checkbox } from "@funstack/ui/checkbox";
import { fakeSave } from "./fake-api.js";
const rows = [
{ id: "1001", title: "Invoice #1001" },
{ id: "1002", title: "Invoice #1002" },
{ id: "1003", title: "Invoice #1003" },
];
/** One selection change: a row toggled, or "select all" set. */
type Change = { readonly id: string; readonly next: boolean } | { readonly all: boolean };
function apply(selection: ReadonlySet<string>, change: Change): ReadonlySet<string> {
if ("all" in change) return new Set(change.all ? rows.map((r) => r.id) : []);
const s = new Set(selection);
if (change.next) s.add(change.id);
else s.delete(change.id);
return s;
}
// A tri-state "select all" built from plain Checkboxes. `indeterminate` is controlled-only: the
// parent derives it (and `checked`) from the rows. Both come from the rows' optimistic selection,
// so the parent box agrees with what the rows show while a save is in flight. Clicking the mixed
// box selects every row. While its own save is pending the dash is hidden (the library renders
// `indeterminate && !pending`); when a save fails the selection rolls back and the dash returns.
export function CheckboxSelectAll() {
const [selected, setSelected] = useState<ReadonlySet<string>>(new Set(["1002"]));
const [optimistic, addChange] = useOptimistic(selected, apply);
// The "fail" switch is read from a ref: state set inside a pending transition would not be
// visible to a second click until the first save settles.
const failNext = useRef(false);
const [failNextShown, setFailNextShown] = useState(false);
const [error, setError] = useState<string | null>(null);
const all = optimistic.size === rows.length;
const some = optimistic.size > 0 && !all;
// Runs inside the Checkbox's transition. "Update on success": adopt the change only after the
// save resolves, with an updater so overlapping saves never close over a stale set.
async function save(change: Change) {
addChange(change);
setError(null);
const fail = failNext.current;
failNext.current = false;
if (fail) setFailNextShown(false);
try {
await fakeSave("the selection", { fail });
startTransition(() => setSelected((prev) => apply(prev, change)));
} catch (e) {
startTransition(() => setError(e instanceof Error ? e.message : String(e)));
}
}
return (
<>
<div className="demo-stack">
<label className="demo-field">
<Checkbox
className="demo-checkbox"
checked={all}
indeterminate={some}
checkedChangeAction={(next) => save({ all: next })}
/>
Select all
</label>
{rows.map((row) => (
<label key={row.id} className="demo-field demo-nested">
<Checkbox
className="demo-checkbox"
checked={optimistic.has(row.id)}
checkedChangeAction={(next) => save({ id: row.id, next })}
/>
{row.title}
</label>
))}
</div>
<div className="demo-row demo-options">
<label className="demo-field">
<input
type="checkbox"
checked={failNextShown}
onChange={(e) => {
failNext.current = e.target.checked;
setFailNextShown(e.target.checked);
}}
/>
Make the next save fail
</label>
</div>
<output className="demo-readout">
saved selection: <code>{[...selected].join(", ") || "none"}</code>
{error && <span className="demo-inline-error"> {error}</span>}
</output>
</>
);
}/* Checkbox: the input is the box; ::before draws the check (clip-path) or the dash.
Selectors use [data-checked] rather than :checked because Chromium does not match :checked on
a box that is both checked and indeterminate. [data-indeterminate] also works before
hydration, where the DOM property (and :indeterminate) cannot be set yet. */
.demo-checkbox {
appearance: none;
flex: none;
margin: 0;
inline-size: 1.25em;
block-size: 1.25em;
border: 2px solid var(--color-text-secondary);
border-radius: 0.25em;
display: inline-grid;
place-content: center;
color: var(--color-on-primary);
cursor: inherit;
transition:
background-color 120ms,
border-color 120ms;
}
.demo-checkbox::before {
content: "";
inline-size: 0.7em;
block-size: 0.7em;
background: currentColor;
scale: 0;
transition: scale 100ms;
clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
}
.demo-checkbox:is([data-checked], [data-indeterminate]) {
background: var(--color-primary);
border-color: var(--color-primary);
}
.demo-checkbox[data-checked]::before {
scale: 1;
}
.demo-checkbox[data-indeterminate]::before {
scale: 1;
clip-path: none;
block-size: 0.18em;
}
.demo-checkbox[data-pending] {
opacity: 0.6;
}
.demo-checkbox:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
.demo-checkbox:disabled {
opacity: 0.5;
}
.demo-checkbox:user-invalid {
border-color: #dc2626;
}
.demo-nested {
margin-inline-start: 1.6em;
}
.demo-options {
margin-top: 16px;
font-size: 0.9rem;
}
@media (prefers-reduced-motion: reduce) {
.demo-checkbox,
.demo-checkbox::before {
transition: none;
}
}import { Checkbox } from "@funstack/ui/checkbox";
<label>
<Checkbox defaultChecked={task.done} checkedChangeAction={setDone} />
Done
</label>One plain component rendering one <input>. No Indicator part: draw the check and the dash on the input with appearance: none and ::before (any SVG works through mask-image), use accent-color to recolour the native box, or hide the input with a clip and style a sibling through label:has(:checked). The label is your own <label>.
Mixed is derived state: a "select all" parent computes it from its rows, which is why it is controlled-only and has no action. Clicking a mixed box dispatches !checked, so the usual unchecked + mixed box becomes checked (the APG's mixed to checked), exactly what the browser does to the DOM.
indeterminate property. The Checkbox dispatches true and, while pending, renders indeterminate && !pending: the attribute, the property and :indeterminate all agree that the dash is gone.checked is true and indeterminate false.In the example the parent box derives from the rows' optimistic selection (useOptimistic), so it never claims "all" while a row's uncheck is still saving. Anything else that reads a selection set from checkedChangeAction (a count, a bulk action) should read the optimistic selection too, or it lags while any action is pending (State that other UI reads at once). For a group with a select-all parent, use CheckboxGroup, which packages this pattern.
Before hydration the DOM property cannot be set, and aria-checked="mixed" is forbidden on native inputs, so a mixed box is announced as unchecked until hydration. data-indeterminate is in the server HTML, so CSS can draw the dash from the start.
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | – | Controlled checked state. undefined means uncontrolled. |
defaultChecked | boolean | false | Initial checked state in uncontrolled mode. Also what a form reset returns to: the latest value is mirrored into the input's defaultChecked. |
indeterminate | boolean | false | Controlled-only mixed state, derived by the parent from other state. No defaultIndeterminate, no action. Rendered as the DOM property, :indeterminate and data-indeterminate, except while an action is pending. Does not change what the form submits. |
checkedChangeAction | ChangeAction<boolean, CheckboxCheckedChangeDetails> | – | Called with the next checked state on every change, inside the Checkbox's transition. Sync, async or a Server Function; rejections reach the nearest Error Boundary. details.reason: "activate" (the native click) or "platform" (a form reset with the reset event, or a change found at hydration with no event). details.valid: the control would be valid with the requested value (native constraints and the Field's validate); information only, the change is dispatched anyway. |
disabled | boolean | false | Native disabled, or the enclosing Field.Root's; never dispatches. A disabled checkbox is not submitted. |
readOnly | boolean | false | The library's read-only, or the enclosing Field.Root's: the checkbox stays focusable and submitted, and a click, Space or a label click changes nothing (the click is cancelled, so no input or change fires) and dispatches nothing. Rendered as aria-readonly="true" and data-readonly, never the HTML attribute (which does not apply to a checkbox input). Native required is not rendered while read-only, so a locked box never blocks a submission. A click made before hydration is undone at mount. disabled wins when both are set. |
value | string | "on" | Submitted with the form when checked (platform default). |
className | string | ((state: CheckboxState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: CheckboxState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
render | ReactElement | ((props, state: CheckboxState) => ReactNode) | – | Replace the default <input> (element form) or take over rendering (function form). It must keep an <input type="checkbox">; a custom component must forward ref to it (the indeterminate property is set through the ref). |
CheckboxState is { checked, indeterminate, pending, disabled, readOnly, invalid }; checked is optimistic, indeterminate is already indeterminate && !pending, and invalid is true while the enclosing Field.Root shows an error (data-invalid), false outside a Field.type, checked, defaultChecked, indeterminate (the property is set), disabled, aria-readonly (while read-only), data-checked, data-indeterminate, data-pending, data-disabled, data-readonly; aria-checked and readOnly are removed, and required while read-only; onChange is merged (a render element's own handler runs first). Inside a Field.Root also id, name, required, aria-invalid, aria-describedby and data-invalid, and disabled / name / required default from the Field.Other <input> props pass through, including role. readOnly is the library's prop (above), not the HTML attribute. There is no transition prop: animate the check with CSS on :checked. In development a checkbox with no accessible name (no <label>, aria-label or aria-labelledby) logs a warning.
| Attribute | On | Pseudo-class | Meaning |
|---|---|---|---|
data-checked | Checkbox | :checked | Optimistic checked state. Identical, with one engine exception (below). |
data-indeterminate | Checkbox | :indeterminate | Mixed state, suspended while pending. Only the attribute works before hydration; :indeterminate also matches radios in an empty group. |
data-pending | Checkbox | – | checkedChangeAction is in flight. |
data-disabled | Checkbox | :disabled | The disabled prop (or the Field's). Only :disabled also sees <fieldset disabled>. |
data-readonly | Checkbox | – | The readOnly prop (or the Field's). :read-only always matches a checkbox input, so it tells nothing. |
data-invalid | Checkbox | :user-invalid | Inside a Field.Root: the Field shows an error for the checkbox. |
| – | Checkbox | :required, :focus-visible | Platform only; no attribute. |
| Key | Behaviour |
|---|---|
| Space | Toggles; a mixed box becomes checked (native). |
| Click (the box or its label) | Toggles (native). |
| Enter | Does not toggle; may submit the form. |
| Tab | Focusable unless disabled (read-only included). |
| Read-only | Space, a click and a label click change nothing and fire no input / change; focus stays. |
role and never sets aria-checked. The mixed state reaches assistive technology through the DOM property, after hydration.required applies to one checkbox; there is no native "at least one of these" constraint.Concept: Transitions & ViewTransition
fsui-checkbox plus fsui-checkbox-check or fsui-checkbox-uncheck. There is no transition prop: a consumer's own <ViewTransition> keys a type map on them.
Concept: Pending & optimistic state
The native input renders the optimistic state, so :checked and data-checked change at the click and assistive technology hears it at once. data-pending is on the Checkbox while checkedChangeAction is in flight. Animate the check with CSS on :checked; your own <ViewTransition> around content next to the Checkbox can animate the commit that ends the action.
Exactly as Switch: the DOM shows the optimistic value (Native inputs), a rejection writes the old value back and reaches the Error Boundary, the latest of overlapping clicks wins, the box is never disabled while pending, and details.event.preventDefault() is not a veto (use controlled mode).
// Autosave with a Server Function, from a Server Component
<Checkbox
defaultChecked={task.done}
checkedChangeAction={setDone.bind(null, task.id)}
aria-label={`Done: ${task.title}`}
/>Concept: Suspense
No content region; nothing the Checkbox renders suspends.
Concept: Browser-owned state
A native <input>: the browser flips it before any JavaScript runs. A form reset and clicks before hydration are dispatched with reason: "platform". indeterminate is consumer-owned and mirrored: the library writes the DOM property after every commit and stops while an action is pending, because the browser clears it on the click.
Concept: Server Components
A native checkbox: clicking, Space, labels, required and form submission work. On hydration a changed value is reconciled with one checkedChangeAction call (reason: "platform"). An indeterminate box is drawn from data-indeterminate but announced as unchecked until hydration.
If a checkbox only posts with its form, a Server Component can render the native input itself, with no library code for it:
// A Server Component: no import from @funstack/ui.
<label>
<input type="checkbox" name="terms" value="accepted" required />
I accept the terms
</label>Compared with <Checkbox>, it has no checkedChangeAction, so no transition, data-pending or rollback on rejection; no data-checked / data-disabled (use :checked and :disabled); no React state that tracks it (clicks before hydration and form resets live only in the DOM); and no Field integration. It cannot be mixed: indeterminate is a DOM property with no HTML attribute, so only script can set it.
"use client";
import { startTransition, useState } from "react";
import { useFormStatus } from "react-dom";
import { Checkbox } from "@funstack/ui/checkbox";
import { fakeSave } from "./fake-api.js";
function SubmitButton() {
const { pending } = useFormStatus();
return (
<button type="submit" className="demo-button demo-primary">
{pending && <span className="demo-spinner" aria-hidden />}
Sign up
</button>
);
}
// No change actions: native form participation. `required` blocks the submit until the terms
// box is checked (and :user-invalid styles it after the attempt). After the form action succeeds
// React resets the form to the `defaultChecked` values; the Reset button does the same.
export function CheckboxForm() {
const [submitted, setSubmitted] = useState("(nothing yet)");
return (
<form
className="demo-stack"
action={async (formData) => {
await fakeSave("the form");
startTransition(() => setSubmitted(JSON.stringify([...formData])));
}}
>
<label className="demo-field">
<Checkbox className="demo-checkbox" name="updates" value="yes" defaultChecked />
Send me product updates
</label>
<label className="demo-field">
<Checkbox className="demo-checkbox" name="terms" required />I accept the terms (required)
</label>
<div className="demo-row">
<SubmitButton />
<button type="reset" className="demo-button">
Reset
</button>
</div>
<output className="demo-readout">
last submitted: <code>{submitted}</code>
</output>
</form>
);
}/* Checkbox: the input is the box; ::before draws the check (clip-path) or the dash.
Selectors use [data-checked] rather than :checked because Chromium does not match :checked on
a box that is both checked and indeterminate. [data-indeterminate] also works before
hydration, where the DOM property (and :indeterminate) cannot be set yet. */
.demo-checkbox {
appearance: none;
flex: none;
margin: 0;
inline-size: 1.25em;
block-size: 1.25em;
border: 2px solid var(--color-text-secondary);
border-radius: 0.25em;
display: inline-grid;
place-content: center;
color: var(--color-on-primary);
cursor: inherit;
transition:
background-color 120ms,
border-color 120ms;
}
.demo-checkbox::before {
content: "";
inline-size: 0.7em;
block-size: 0.7em;
background: currentColor;
scale: 0;
transition: scale 100ms;
clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
}
.demo-checkbox:is([data-checked], [data-indeterminate]) {
background: var(--color-primary);
border-color: var(--color-primary);
}
.demo-checkbox[data-checked]::before {
scale: 1;
}
.demo-checkbox[data-indeterminate]::before {
scale: 1;
clip-path: none;
block-size: 0.18em;
}
.demo-checkbox[data-pending] {
opacity: 0.6;
}
.demo-checkbox:focus-visible {
outline: 2px solid var(--color-primary);
outline-offset: 2px;
}
.demo-checkbox:disabled {
opacity: 0.5;
}
.demo-checkbox:user-invalid {
border-color: #dc2626;
}
.demo-nested {
margin-inline-start: 1.6em;
}
.demo-options {
margin-top: 16px;
font-size: 0.9rem;
}
@media (prefers-reduced-motion: reduce) {
.demo-checkbox,
.demo-checkbox::before {
transition: none;
}
}Without change actions the boxes are native form controls: name, value and required pass through, :user-invalid marks the unchecked required box after a submit attempt, and a reset (the Reset button, or React's automatic reset after the action succeeds) restores defaultChecked.
checkedChangeAction with reason: "platform"; a controlled parent is offered the reset value and decides. Details on the Switch page.checkedChangeAction={serverFn} and <form action={serverFn}> are independent channels. A mixed box still posts its value when checked.