Checkbox06
Draws its tick in one stroke and wobbles like jelly. In Comic it goes PAT!, in Arcade it scores. Use it for choices people confirm with a form, and for picking items from a list.
<Checkbox label="Email me about replies" />Usage
import { Checkbox } from "plonk-ui";
export function Updates() {
return <Checkbox label="Email me about replies" name="replies" />;
}Checkbox renders a real <input type="checkbox">. Leave it uncontrolled with
defaultChecked, or control it with checked and onChange; the new state is
event.target.checked. For a setting that takes effect right away, use a
Switch instead.
Select all
indeterminate shows a dash for "some of these are selected". It's a visual
state only: you decide when it's on, and clicking the box checks it.
import { useState } from "react";
import { Checkbox } from "plonk-ui";
const FILES = ["brief.pdf", "moodboard.png", "invoice-0042.pdf"];
export default function SelectAll() {
const [selected, setSelected] = useState<string[]>(["brief.pdf"]);
const all = selected.length === FILES.length;
const some = selected.length > 0 && !all;
const toggle = (file: string, checked: boolean) =>
setSelected((current) =>
checked ? [...current, file] : current.filter((f) => f !== file),
);
return (
<div className="flex flex-col gap-3">
<Checkbox
label="All files"
checked={all}
indeterminate={some}
onChange={(event) => setSelected(event.target.checked ? FILES : [])}
/>
<div className="flex flex-col gap-3 ps-9">
{FILES.map((file) => (
<Checkbox
key={file}
label={file}
checked={selected.includes(file)}
onChange={(event) => toggle(file, event.target.checked)}
/>
))}
</div>
</div>
);
}In a form
A checked box submits its value (on by default) under its name; an
unchecked box sends nothing. error marks it invalid and shakes it once,
here when the terms aren't accepted.
import { useState, type FormEvent } from "react";
import { Button, Checkbox } from "plonk-ui";
export default function Terms() {
const [error, setError] = useState<string>();
const [sent, setSent] = useState(false);
function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
const data = new FormData(event.currentTarget);
// A checked box sends "on"; an unchecked one sends nothing.
const accepted = data.get("terms") === "on";
setError(accepted ? undefined : "Accept the terms to continue.");
setSent(accepted);
}
return (
<form onSubmit={handleSubmit} noValidate className="flex flex-col gap-4">
<Checkbox
name="terms"
required
label="I accept the terms"
description="We only email you about your account."
error={error}
onChange={() => setError(undefined)}
/>
<Button type="submit" className="self-start">
Create account
</Button>
<p role="status" className="font-bold empty:hidden">
{sent ? "Account created." : ""}
</p>
</form>
);
}Personalities
Sticker squashes the box and springs it back. Retro draws the tick in three
steps. Comic shouts a PAT! next to the box and Arcade throws points into the
air, then flashes the box. The effects play only when the box gets checked, so
clearing a list stays quiet. To turn them off, set
--plonk-press-effect: none on the checkbox or any parent.
Accessibility
- A native checkbox:
Spacetoggles it, and its label toggles it too. indeterminateis announced as "mixed".descriptionanderrorare linked witharia-describedby;erroralso setsaria-invalid.- The focus ring shows for keyboard users only. Effects and jelly are decorative and skip screen readers.
API
All other <input> props are passed through.
| Prop | Type | Default | Description |
|---|---|---|---|
indeterminate | boolean | false | Shows a dash for “some selected”, like a select-all box. Screen readers announce it as mixed. Clicking it checks the box. |
label | ReactNode | — | Visible label, linked to the control. Without one, pass aria-label or aria-labelledby. |
description | ReactNode | — | Help text under the control, read by screen readers after the label. |
error | ReactNode | — | Error message. When set, the control is marked invalid (aria-invalid) and shakes once. Pass true to mark it without a message. |
checked | boolean | — | Controlled checked state. Use with onChange. |
defaultChecked | boolean | — | Checked state on first render, when uncontrolled. |
onChange | ChangeEventHandler<HTMLInputElement> | — | Native change event. Read event.target.checked for the new state. |
name | string | — | Field name in the submitted form data. |
value | string | number | readonly string[] | — | Value sent with the form when checked. Defaults to on. |
required | boolean | — | Marks the checkbox required, with an asterisk in the label. |
disabled | boolean | — | Disables the checkbox. |