Checkbox
shadcn-htmx/checkboxFreeComponent
Native <input type="checkbox"> with shadcn polish — peer-checked SVG indicator, indeterminate support, htmx-ready.
Live preview
live · rendered by the registry
Rendered by shadcn-htmx on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://shadcn-htmx.productdevbook.com/r/checkbox.jsonSource
1/** @jsxImportSource hono/jsx */2import { cn, type ClassValue } from "@/registry/lib/cn"34// Native <input type="checkbox"> with shadcn polish. Source of truth:5// repos/shadcn-ui/apps/v4/registry/new-york-v4/ui/checkbox.tsx6//7// The upstream shadcn uses Radix Checkbox, which renders a styled div with8// role="checkbox" and hides the native input. We can't depend on Radix in9// our SSR setup, so we keep a real <input type="checkbox"> (form-submittable,10// keyboard-accessible, accessible-name aware) and layer a check icon on top.11//12// Pairing with a Label: use the htmlFor pattern — clicking the label toggles13// the checkbox. Indeterminate state must be set via JS (the HTML attribute14// alone won't do it); we mirror the data-state attribute on the wrapper so15// custom styling can react.1617const inputBase =18 "peer size-4 shrink-0 appearance-none rounded-[4px] border border-input bg-background shadow-xs transition-shadow outline-none " +19 "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 " +20 "disabled:cursor-not-allowed disabled:opacity-50 " +21 "checked:border-primary checked:bg-primary " +22 "indeterminate:border-primary indeterminate:bg-primary " +23 "aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 " +24 "dark:bg-input/30"2526export function checkboxClasses(opts?: { class?: ClassValue }): string {27 return cn(inputBase, opts?.class)28}2930type CheckboxProps = {31 id?: string32 name?: string33 value?: string34 checked?: boolean35 defaultChecked?: boolean36 // Render the SSR hook for an initially-indeterminate checkbox. `indeterminate`37 // is an IDL-only property with no HTML content attribute (WHATWG HTML: it is38 // initially false and "cannot be set using an HTML attribute"), so an SSR39 // component emits data-initial-indeterminate="true" and a tiny on-mount40 // script flips el.indeterminate. See repos/whatwg-html/source (indeterminate41 // IDL attribute) and the input/checkbox MDN reference.42 indeterminate?: boolean43 required?: boolean44 disabled?: boolean45 readonly?: boolean46 form?: string47 class?: ClassValue48 ariaLabel?: string49 ariaLabelledby?: string50 ariaDescribedby?: string51 ariaInvalid?: boolean52 // ID of a visible element containing an error message. Pair with53 // ariaInvalid for the full WCAG error-identification pattern.54 // See repos/mdn/files/en-us/web/accessibility/aria/reference/attributes/aria-errormessage/index.md55// … truncated
More from shadcn-htmx
All 83 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | shadcn-htmx | Components | Free | no deps | |
| Active Searchactive-search | shadcn-htmx | Components | Free | no deps | |
| Alertalert | shadcn-htmx | Components | Free | no deps | |
| Alert Dialogalert-dialog | shadcn-htmx | Components | Free | no deps | |
| Aspect Ratioaspect-ratio | shadcn-htmx | Components | Free | no deps | |
| Auto Gridauto-grid | shadcn-htmx | Components | Free | no deps | |
| Autocompleteautocomplete | shadcn-htmx | Components | Free | no deps | |
| Autosize Textareaautosize-textarea | shadcn-htmx | Components | Free | no deps |
