Checkbox
Native checkbox. Children are the visible label.
On this page
Controlled when checked is defined. Uncontrolled when you pass defaultChecked or omit both. useControlledState does not fire onCheckedChange. The component does.
import { Checkbox, ErrorMessage, Field } from '@tounsoo/todae';
import { useId, useState } from 'react';
export default function CheckboxStates() {
const [accepted, setAccepted] = useState(false);
const errorId = useId();
const invalid = !accepted;
return (
<>
<Checkbox.Root defaultChecked>Product updates</Checkbox.Root>
<Checkbox.Root disabled>Archived list</Checkbox.Root>
<Field.Group>
<Checkbox.Root
checked={accepted}
onCheckedChange={setAccepted}
invalid={invalid}
aria-describedby={invalid ? errorId : undefined}
>
I accept the terms
</Checkbox.Root>
{invalid ? <ErrorMessage.Root id={errorId}>Accept the terms to continue.</ErrorMessage.Root> : null}
</Field.Group>
</>
);
}
When it is checked, the box fades to its checked color and the check grows in over selection.motion.duration. When the user prefers reduced motion, the check only fades.
Do not put Checkbox inside Field.Root. The shell is for text and chip inputs. Wire aria-describedby on the checkbox when a Description or ErrorMessage sits under it.
For a permanent exclusive choice, use Radio. For an overlay list, use OptionList.
Indeterminate
indeterminate shows the mixed state, such as a select-all box over a list where only some items are checked. It sets the input's indeterminate property and draws the indeterminate glyph in place of the check. It does not change checked: a click toggles checked as usual, and you decide what the box shows next. Todae sets the property again after every render, so a click does not leave it cleared while your state says mixed.
import { Checkbox, Field } from '@tounsoo/todae';
import { useState } from 'react';
export default function CheckboxIndeterminate() {
const [email, setEmail] = useState(true);
const [push, setPush] = useState(false);
const all = email && push;
return (
<Field.Group>
<Checkbox.Root
checked={all}
indeterminate={(email || push) && !all}
onCheckedChange={(checked) => {
setEmail(checked);
setPush(checked);
}}
>
All notifications
</Checkbox.Root>
<Checkbox.Root checked={email} onCheckedChange={setEmail}>
Email
</Checkbox.Root>
<Checkbox.Root checked={push} onCheckedChange={setPush}>
Push
</Checkbox.Root>
</Field.Group>
);
}
useSelection wires a select-all checkbox this way, from its getSelectAllProps().
Icons
The check is the check glyph: Todae's check mark, unless a GlyphsProvider sets one. It fills the box and shows while the checkbox is checked. While indeterminate is set, the box is filled the same way and shows the indeterminate glyph instead, a horizontal bar by default.
import { Check } from 'lucide-react';
<GlyphsProvider glyphs={{ check: <Check strokeWidth={3} /> }}>
<Checkbox.Root defaultChecked>Product updates</Checkbox.Root>
</GlyphsProvider>
Accessible composition
Todae handles: Checkbox is a native <input type="checkbox"> inside a <label> with your children, so the children are its name and a click on the text toggles it. Space toggles it, as natively. disabled is native and invalid sets aria-invalid; both also come from a surrounding Fieldset. The drawn box is aria-hidden and shows the focus ring while the input is focus-visible. indeterminate sets the input's indeterminate property, which a screen reader reads as a mixed state. Other props, aria-*, id and ref included, go to the input; className goes to the label.
You supply: Visible text as children, which the type requires. The ids of any Description or ErrorMessage in aria-describedby. A Fieldset with a legend when several checkboxes answer one question.
Watch for: Wrapping it in another <label> or pointing a Label at it: it is already labelled. An aria-label that differs from the children replaces the visible name. invalid marks the checkbox invalid but does not say why, so show an ErrorMessage with it.
When to use
When: Independent on/off that stays on the page. Children = visible label. A parent box over a partly checked group → indeterminate.
When not: Exclusive one-of → Radio. Instant setting toggle look → Switch. Overlay multi-select → OptionList selectionMode="multiple". Inside Field.Root.
Props
| Name | Type | Description |
|---|---|---|
children Required | React.ReactNode | Visible label. Clicking the text toggles the input. |
checked | boolean | Controlled checked state. Defined means controlled. |
defaultChecked | boolean | Initial checked state when uncontrolled. |
onCheckedChange | (checked: boolean) => void | Fires after the checked state changes. |
disabled | boolean | Disables the input. |
invalid | boolean | Marks the control invalid and sets aria-invalid. |
indeterminate | boolean | The mixed state, such as a select-all box over a partly selected list. Sets the input's indeterminate and shows the indeterminate glyph in place of the check. A click still toggles checked. |
customValidity | string | A message for a rule the browser cannot check, set with setCustomValidity on the checkbox. An empty string or undefined means the rule holds. |
name | string | Native input name. |
value | string | Native input value. |
className | string | Optional class on the wrapping label. |