Search for a command to run...
A control that allows the user to toggle between checked and not checked.
npx shadcn@latest add @zaidan/checkboxpnpx shadcn add @zaidan/checkboxyarn dlx shadcn@latest add @zaidan/checkboxbunx shadcn@latest add @zaidan/checkboxInstall the following dependencies:
npm i @kobalte/corepnpm add @kobalte/coreyarn add @kobalte/corebun add @kobalte/coreCopy and paste the following code into your project.
1import * as CheckboxPrimitive from "@kobalte/core/checkbox";2import type { PolymorphicProps } from "@kobalte/core/polymorphic";3import { CheckIcon } from "lucide-solid";4import { type ComponentProps, splitProps, type ValidComponent } from "solid-js";5
6import { cn } from "~/lib/utils";7
8type CheckboxProps<T extends ValidComponent = "div"> = PolymorphicProps<9 T,10 CheckboxPrimitive.CheckboxRootProps<T>11> &12 Pick<ComponentProps<T>, "class">;13
14const Checkbox = <T extends ValidComponent = "div">(props: CheckboxProps<T>) => {15 const [local, others] = splitProps(props as CheckboxProps, ["class", "id"]);16 return (17 <CheckboxPrimitive.Root18 data-slot="checkbox"19 class="peer data-disabled:cursor-not-allowed data-disabled:opacity-50"20 {...others}21 >22 <CheckboxPrimitive.Input data-slot="checkbox-input" class="peer sr-only" id={local.id} />23 <CheckboxPrimitive.Control24 class={cn(25 "relative z-checkbox shrink-0 outline-none after:absolute after:-inset-x-3 after:-inset-y-2",26 local.class,27 )}28 >29 <CheckboxPrimitive.Indicator30 data-slot="checkbox-indicator"31 class="z-checkbox-indicator grid place-content-center text-current transition-none"32 >33 <CheckIcon class="size-3.5" />34 </CheckboxPrimitive.Indicator>35 </CheckboxPrimitive.Control>36 </CheckboxPrimitive.Root>37 );38};39
40type CheckboxLabelProps<T extends ValidComponent = "label"> = PolymorphicProps<41 T,42 CheckboxPrimitive.CheckboxLabelProps<T>43> &44 Pick<ComponentProps<T>, "class" | "children">;45
46const CheckboxLabel = <T extends ValidComponent = "label">(props: CheckboxLabelProps<T>) => {47 const [local, others] = splitProps(props as CheckboxLabelProps, ["class", "children"]);48 return (49 <CheckboxPrimitive.Label50 data-slot="checkbox-label"51 class={cn(52 "font-medium text-sm leading-none peer-data-disabled:cursor-not-allowed peer-data-disabled:opacity-70",53 local.class,54 )}55 {...others}56 >57 {local.children}58 </CheckboxPrimitive.Label>59 );60};61
62export { Checkbox, CheckboxLabel };Here are the source code of all the examples from the preview page:
import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldLabel } from "~/components/ui/field";function CheckboxBasic() { return ( <Example title="Basic"> <Field orientation="horizontal"> <Checkbox id="terms" /> <FieldLabel for="terms">Accept terms and conditions</FieldLabel> </Field> </Example> );}import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldContent, FieldDescription, FieldLabel } from "~/components/ui/field";function CheckboxWithDescription() { return ( <Example title="With Description"> <Field orientation="horizontal"> <Checkbox id="terms-2" defaultChecked /> <FieldContent> <FieldLabel for="terms-2">Accept terms and conditions</FieldLabel> <FieldDescription> By clicking this checkbox, you agree to the terms and conditions. </FieldDescription> </FieldContent> </Field> </Example> );}import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldLabel } from "~/components/ui/field";function CheckboxInvalid() { return ( <Example title="Invalid"> <Field orientation="horizontal" data-invalid> <Checkbox id="terms-3" aria-invalid /> <FieldLabel for="terms-3">Accept terms and conditions</FieldLabel> </Field> </Example> );}import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldLabel } from "~/components/ui/field";function CheckboxDisabled() { return ( <Example title="Disabled"> <Field orientation="horizontal"> <Checkbox id="toggle" disabled /> <FieldLabel for="toggle">Enable notifications</FieldLabel> </Field> </Example> );}import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel, FieldTitle,} from "~/components/ui/field";function CheckboxWithTitle() { return ( <Example title="With Title"> <FieldGroup> <FieldLabel for="toggle-2"> <Field orientation="horizontal"> <Checkbox id="toggle-2" defaultChecked /> <FieldContent> <FieldTitle>Enable notifications</FieldTitle> <FieldDescription> You can enable or disable notifications at any time. </FieldDescription> </FieldContent> </Field> </FieldLabel> <FieldLabel for="toggle-4"> <Field orientation="horizontal" data-disabled> <Checkbox id="toggle-4" disabled /> <FieldContent> <FieldTitle>Enable notifications</FieldTitle> <FieldDescription> You can enable or disable notifications at any time. </FieldDescription> </FieldContent> </Field> </FieldLabel> </FieldGroup> </Example> );}import { createSignal, For } from "solid-js";import { Checkbox } from "~/components/ui/checkbox";import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow,} from "~/components/ui/table";function CheckboxInTable() { const [selectedRows, setSelectedRows] = createSignal<Set<string>>(new Set(["1"]));
const selectAll = () => selectedRows().size === tableData.length;
const handleSelectAll = (checked: boolean) => { if (checked) { setSelectedRows(new Set(tableData.map((row) => row.id))); } else { setSelectedRows(new Set<string>()); } };
const handleSelectRow = (id: string, checked: boolean) => { const newSelected = new Set(selectedRows()); if (checked) { newSelected.add(id); } else { newSelected.delete(id); } setSelectedRows(newSelected); };
return ( <Example title="In Table"> <Table> <TableHeader> <TableRow> <TableHead class="w-8"> <Checkbox id="select-all" 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}`} 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> </Example> );}import { Checkbox } from "~/components/ui/checkbox";import { Field, FieldLabel } from "~/components/ui/field";function CheckboxGroup() { return ( <Example title="Group"> <Field> <FieldLabel>Show these items on the desktop:</FieldLabel> <Field orientation="horizontal"> <Checkbox id="finder-pref-9k2-hard-disks-ljj" /> <FieldLabel for="finder-pref-9k2-hard-disks-ljj" class="font-normal"> Hard disks </FieldLabel> </Field> <Field orientation="horizontal"> <Checkbox id="finder-pref-9k2-external-disks-1yg" /> <FieldLabel for="finder-pref-9k2-external-disks-1yg" class="font-normal"> External disks </FieldLabel> </Field> <Field orientation="horizontal"> <Checkbox id="finder-pref-9k2-cds-dvds-fzt" /> <FieldLabel for="finder-pref-9k2-cds-dvds-fzt" class="font-normal"> CDs, DVDs, and iPods </FieldLabel> </Field> <Field orientation="horizontal"> <Checkbox id="finder-pref-9k2-connected-servers-6l2" /> <FieldLabel for="finder-pref-9k2-connected-servers-6l2" class="font-normal"> Connected servers </FieldLabel> </Field> </Field> </Example> );}