Checkbox
Checkbox with an optional label row and an indeterminate (mixed) state.
Example
import { useState } from "react";
import { Checkbox } from "@porcelana/ui";
export default function Example() {
const [days, setDays] = useState({ mon: true, sat: false });
const all = days.mon && days.sat ? true : days.mon || days.sat ? "indeterminate" : false;
return (
<div style={{ display: "grid", gap: 4 }}>
<Checkbox
label="Every day"
checked={all}
onCheckedChange={(v) => setDays({ mon: v === true, sat: v === true })}
/>
<Checkbox label="Monday" checked={days.mon} onCheckedChange={(v) => setDays({ ...days, mon: v === true })} />
<Checkbox label="Saturday" checked={days.sat} onCheckedChange={(v) => setDays({ ...days, sat: v === true })} />
</div>
);
}Import
import { Checkbox } from "@porcelana/ui";
import "@porcelana/ui/styles.css"; // once, at your app rootProps
Generated from the TypeScript types. Standard HTML attributes are passed through and not listed.
Checkbox
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label | string | – | Defines a string value that labels the current element. @see aria-labelledby. |
asChild | boolean | – | Render the single child element instead, merging props and behaviour onto it. |
checked | CheckedState | – | Controlled checked state. Use with onCheckedChange. |
defaultChecked | CheckedState | – | Initial checked state when uncontrolled. |
disabled | boolean | – | Prevents interaction and dims the control. |
id | string | – | Element id. Generated automatically when omitted. |
label | ReactNode | – | Visible label. The whole row (box + label) is the 44px touch target. |
onCheckedChange | ((checked: CheckedState) => void) | – | Called with the new checked state. |
required | boolean | – | Marks the control as required in forms. |
type | "button" | "submit" | "reset" | – | Button type (defaults to "button"). |
Accessibility
- Space toggles; checked="indeterminate" is announced as mixed.
- With label, clicking the text toggles the box.
Mobile
- The whole label row is a 44px touch target; the box itself has an invisible 44px hit area.