Checkbox
A control that allows the user to toggle between checked and not checked.
By clicking this checkbox, you agree to the terms.
import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel, FieldTitle,} from "~/components/ui/field";
export default function CheckboxDemo() { return ( <FieldGroup class="max-w-sm"> <Field orientation="horizontal"> <Checkbox id="terms-checkbox" name="terms-checkbox" /> <FieldLabel for="terms-checkbox">Accept terms and conditions</FieldLabel> </Field> <Field orientation="horizontal"> <Checkbox id="terms-checkbox-2" name="terms-checkbox-2" defaultChecked /> <FieldContent> <FieldLabel for="terms-checkbox-2">Accept terms and conditions</FieldLabel> <FieldDescription>By clicking this checkbox, you agree to the terms.</FieldDescription> </FieldContent> </Field> <Field orientation="horizontal" data-disabled> <Checkbox id="toggle-checkbox" name="toggle-checkbox" disabled /> <FieldLabel for="toggle-checkbox">Enable notifications</FieldLabel> </Field> <FieldLabel> <Field orientation="horizontal"> <Checkbox id="toggle-checkbox-2" name="toggle-checkbox-2" /> <FieldContent> <FieldTitle>Enable notifications</FieldTitle> <FieldDescription> You can enable or disable notifications at any time. </FieldDescription> </FieldContent> </Field> </FieldLabel> </FieldGroup> );}Installation
Usage
import { Checkbox } from "~/components/ui/checkbox";<Checkbox />Checked State
Use defaultChecked for uncontrolled checkboxes, or checked and onChange to control the state.
import { createSignal } from "solid-js";
const [checked, setChecked] = createSignal(false);
<Checkbox checked={checked()} onChange={setChecked} />;Invalid State
Set aria-invalid on the checkbox and data-invalid on the field wrapper to show the invalid styles.
import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldGroup, FieldLabel } from "~/components/ui/field";
export default function CheckboxInvalid() { return ( <FieldGroup class="mx-auto w-56"> <Field orientation="horizontal" data-invalid> <Checkbox id="terms-checkbox-invalid" name="terms-checkbox-invalid" aria-invalid /> <FieldLabel for="terms-checkbox-invalid">Accept terms and conditions</FieldLabel> </Field> </FieldGroup> );}Basic
Pair the checkbox with Field and FieldLabel for proper layout and labeling.
import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldGroup, FieldLabel } from "~/components/ui/field";
export default function CheckboxBasic() { return ( <FieldGroup class="mx-auto w-56"> <Field orientation="horizontal"> <Checkbox id="terms-checkbox-basic" name="terms-checkbox-basic" /> <FieldLabel for="terms-checkbox-basic">Accept terms and conditions</FieldLabel> </Field> </FieldGroup> );}Description
Use FieldContent and FieldDescription for helper text.
By clicking this checkbox, you agree to the terms and conditions.
import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel,} from "~/components/ui/field";
export default function CheckboxDescription() { return ( <FieldGroup class="mx-auto w-72"> <Field orientation="horizontal"> <Checkbox id="terms-checkbox-desc" name="terms-checkbox-desc" defaultChecked /> <FieldContent> <FieldLabel for="terms-checkbox-desc">Accept terms and conditions</FieldLabel> <FieldDescription> By clicking this checkbox, you agree to the terms and conditions. </FieldDescription> </FieldContent> </Field> </FieldGroup> );}Disabled
Use the disabled prop to prevent interaction and add the data-disabled attribute to the <Field> component for disabled styles.
import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldGroup, FieldLabel } from "~/components/ui/field";
export default function CheckboxDisabled() { return ( <FieldGroup class="mx-auto w-56"> <Field orientation="horizontal" data-disabled> <Checkbox id="toggle-checkbox-disabled" name="toggle-checkbox-disabled" disabled /> <FieldLabel for="toggle-checkbox-disabled">Enable notifications</FieldLabel> </Field> </FieldGroup> );}Group
Use multiple fields to create a checkbox list.
import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldDescription, FieldGroup, FieldLabel, FieldLegend, FieldSet,} from "~/components/ui/field";
export default function CheckboxGroup() { return ( <FieldSet> <FieldLegend variant="label">Show these items on the desktop:</FieldLegend> <FieldDescription>Select the items you want to show on the desktop.</FieldDescription> <FieldGroup class="gap-3"> <Field orientation="horizontal"> <Checkbox id="finder-pref-9k2-hard-disks-ljj-checkbox" name="finder-pref-9k2-hard-disks-ljj-checkbox" defaultChecked /> <FieldLabel for="finder-pref-9k2-hard-disks-ljj-checkbox" class="font-normal"> Hard disks </FieldLabel> </Field> <Field orientation="horizontal"> <Checkbox id="finder-pref-9k2-external-disks-1yg-checkbox" name="finder-pref-9k2-external-disks-1yg-checkbox" defaultChecked /> <FieldLabel for="finder-pref-9k2-external-disks-1yg-checkbox" class="font-normal"> External disks </FieldLabel> </Field> <Field orientation="horizontal"> <Checkbox id="finder-pref-9k2-cds-dvds-fzt-checkbox" name="finder-pref-9k2-cds-dvds-fzt-checkbox" /> <FieldLabel for="finder-pref-9k2-cds-dvds-fzt-checkbox" class="font-normal"> CDs, DVDs, and iPods </FieldLabel> </Field> <Field orientation="horizontal"> <Checkbox id="finder-pref-9k2-connected-servers-6l2-checkbox" name="finder-pref-9k2-connected-servers-6l2-checkbox" /> <FieldLabel for="finder-pref-9k2-connected-servers-6l2-checkbox" class="font-normal"> Connected servers </FieldLabel> </Field> </FieldGroup> </FieldSet> );}Table
| Name | Role | ||
|---|---|---|---|
| Sarah Chen | sarah.chen@example.com | Admin | |
| Marcus Rodriguez | marcus.rodriguez@example.com | User | |
| Priya Patel | priya.patel@example.com | User | |
| David Kim | david.kim@example.com | Editor |
import { createSignal, For } from "solid-js";import { Checkbox } from "~/components/ui/checkbox";import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow,} from "~/components/ui/table";
const tableData = [ { id: "1", name: "Sarah Chen", email: "sarah.chen@example.com", role: "Admin" }, { id: "2", name: "Marcus Rodriguez", email: "marcus.rodriguez@example.com", role: "User" }, { id: "3", name: "Priya Patel", email: "priya.patel@example.com", role: "User" }, { id: "4", name: "David Kim", email: "david.kim@example.com", role: "Editor" },];
export default function CheckboxTable() { const [selectedRows, setSelectedRows] = createSignal<Set<string>>(new Set(["1"])); const selectAll = () => selectedRows().size === tableData.length;
const handleSelectAll = (checked: boolean) => { setSelectedRows(checked ? new Set(tableData.map((row) => row.id)) : new Set<string>()); };
const handleSelectRow = (id: string, checked: boolean) => { const nextSelectedRows = new Set(selectedRows()); if (checked) nextSelectedRows.add(id); else nextSelectedRows.delete(id); setSelectedRows(nextSelectedRows); };
return ( <Table> <TableHeader> <TableRow> <TableHead class="w-8"> <Checkbox id="select-all-checkbox" name="select-all-checkbox" aria-label="Select all rows" checked={selectAll()} onChange={handleSelectAll} /> </TableHead> <TableHead>Name</TableHead> <TableHead>Email</TableHead> <TableHead>Role</TableHead> </TableRow> </TableHeader> <TableBody> <For each={tableData}> {(row) => ( <TableRow data-state={selectedRows().has(row.id) ? "selected" : undefined}> <TableCell> <Checkbox id={`row-${row.id}-checkbox`} name={`row-${row.id}-checkbox`} aria-label={`Select ${row.name}`} checked={selectedRows().has(row.id)} onChange={(checked) => handleSelectRow(row.id, checked)} /> </TableCell> <TableCell class="font-medium">{row.name}</TableCell> <TableCell>{row.email}</TableCell> <TableCell>{row.role}</TableCell> </TableRow> )} </For> </TableBody> </Table> );}API Reference
Checkbox is built on top of @kobalte/core/checkbox, which implements the WAI-ARIA checkbox pattern, including keyboard, disabled, indeterminate, and form behavior.
Checkbox
CheckboxLabel
Renders a <label> associated with the checkbox's hidden input. Place it as a sibling of Checkbox inside the same Field.