Skip to content
All components
Forms

Checkbox

Turn an independent option on or off, or show that only some items in a set are selected.

Checked, unchecked and indeterminate.

Installation

tdpxl is not an npm dependency of your app. The @tdpxl/cli command (not published to npm yet) copies the source into your project, with the registry items and npm packages it imports.

npx @tdpxl/cli add checkbox

Then import it from @/components/ui/checkbox. New project? Start with the installation guide.

Examples

Each example is one branch of the official demo, rendered in its own frame. The code is that branch as you would write it, with imports pointing at the default install paths.

Checked

Show a checked checkbox with its label. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"

import { Label } from "@/components/ui/label"

export function CheckboxChecked() {
  const id = React.useId()
  
  
  

  return (
    <div className="flex flex-col gap-6">
      <div className="flex items-center gap-2">
        <Checkbox id="checkbox-demo-terms" defaultChecked />
        <Label htmlFor="checkbox-demo-terms">Accept the terms</Label>
      </div>
    </div>
  )
}

Unchecked

Show an unchecked checkbox with its label. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"

import { Label } from "@/components/ui/label"

export function CheckboxUnchecked() {
  const id = React.useId()
  
  
  

  return (
    <div className="flex flex-col gap-6">
      <div className="flex items-center gap-2">
        <Checkbox id="checkbox-demo-news" />
        <Label htmlFor="checkbox-demo-news">Send me product updates</Label>
      </div>
    </div>
  )
}

Indeterminate

Represent a mixed checkbox selection. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"

import { Label } from "@/components/ui/label"

export function CheckboxIndeterminate() {
  const id = React.useId()
  const [selected, setSelected] = React.useState<string[]>(["invoice"])
  
  

  return (
    <div className="flex flex-col gap-6">
      <div className="flex items-center gap-2">
        <Checkbox id="checkbox-demo-mixed" checked="indeterminate" />
        <Label htmlFor="checkbox-demo-mixed">Some items selected</Label>
      </div>
    </div>
  )
}

Disabled

Inspect the unavailable control using its real disabled semantics. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"

import { Label } from "@/components/ui/label"

export function CheckboxDisabled() {
  const id = React.useId()
  
  
  

  return (
    <div className="flex flex-col gap-6">
      <div className="flex items-center gap-2">
        <Checkbox id="checkbox-demo-off" disabled />
        <Label htmlFor="checkbox-demo-off">Disabled</Label>
      </div>
    </div>
  )
}

With description

Pair a checkbox with a label and an associated supporting description. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"

import { Label } from "@/components/ui/label"

const files = [
  { id: "contract", label: "Contract.pdf" },
  { id: "invoice", label: "Invoice-0425.pdf" },
  { id: "receipt", label: "Receipt-0425.pdf" },
]

export function CheckboxWithDescription() {
  const id = React.useId()
  
  
  

  return (
    <div className="flex flex-col gap-6">
      <div className="flex max-w-sm items-start gap-2">
        <Checkbox
          id={`${id}-backup`}
          className="mt-0.5"
          defaultChecked
          aria-describedby={`${id}-backup-hint`}
        />
        <div className="flex flex-col gap-1">
          <Label htmlFor={`${id}-backup`}>Back up automatically</Label>
          <p id={`${id}-backup-hint`} className="text-paragraph-sm text-sub-600">
            Copies new files every night. Backups older than 30 days are removed.
          </p>
        </div>
      </div>
    </div>
  )
}

Checkbox group

Group related independent choices under a fieldset legend and a shared hint. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"
import { FieldDescription, FieldLegend, FieldSet } from "@/components/ui/field"
import { Label } from "@/components/ui/label"

export function CheckboxCheckboxGroup() {
  const id = React.useId()
  
  
  

  return (
    <div className="flex flex-col gap-6">
      <FieldSet aria-describedby={`${id}-share-hint`} className="max-w-sm">
        <FieldLegend>Share with</FieldLegend>
        <FieldDescription id={`${id}-share-hint`} className="-mt-3">
          Selected groups get view access.
        </FieldDescription>
        <div className="flex flex-col gap-3">
          <div className="flex items-center gap-2">
            <Checkbox id={`${id}-share-team`} defaultChecked />
            <Label htmlFor={`${id}-share-team`}>My team</Label>
          </div>
          <div className="flex items-center gap-2">
            <Checkbox id={`${id}-share-finance`} />
            <Label htmlFor={`${id}-share-finance`}>Finance</Label>
          </div>
          <div className="flex items-center gap-2">
            <Checkbox id={`${id}-share-guests`} />
            <Label htmlFor={`${id}-share-guests`}>Guests</Label>
          </div>
        </div>
      </FieldSet>
    </div>
  )
}

Invalid

Associate a checkbox's invalid state with contextual feedback. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"
import { FieldError } from "@/components/ui/field"
import { Label } from "@/components/ui/label"

export function CheckboxInvalid() {
  const id = React.useId()
  
  
  

  return (
    <div className="flex flex-col gap-6">
      <div className="flex max-w-sm flex-col gap-1.5">
        <div className="flex items-center gap-2">
          <Checkbox
            id={`${id}-consent`}
            required
            aria-invalid
            aria-describedby={`${id}-consent-error`}
          />
          <Label htmlFor={`${id}-consent`}>I confirm the billing details are correct</Label>
        </div>
        <FieldError id={`${id}-consent-error`}>
          Confirm the billing details to place the order.
        </FieldError>
      </div>
    </div>
  )
}

Select all

Drive a parent checkbox from its children, including the mixed state. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"
import { FieldLegend, FieldSet } from "@/components/ui/field"
import { Label } from "@/components/ui/label"

const files = [
  { id: "contract", label: "Contract.pdf" },
  { id: "invoice", label: "Invoice-0425.pdf" },
  { id: "receipt", label: "Receipt-0425.pdf" },
]

export function CheckboxSelectAll() {
  const id = React.useId()
  const [selected, setSelected] = React.useState<string[]>(["invoice"])
  const allSelected = selected.length === files.length
  const parentState = allSelected ? true : selected.length ? "indeterminate" : false

  return (
    <div className="flex flex-col gap-6">
      <FieldSet className="max-w-sm">
        <FieldLegend>Files to download</FieldLegend>
        <div className="flex items-center gap-2">
          <Checkbox
            id={`${id}-all`}
            checked={parentState}
            onCheckedChange={(checked) => setSelected(checked === true ? files.map((file) => file.id) : [])}
          />
          <Label htmlFor={`${id}-all`}>Select all files</Label>
        </div>
        <div className="flex flex-col gap-3 ps-7">
          {files.map((file) => (
            <div key={file.id} className="flex items-center gap-2">
              <Checkbox
                id={`${id}-${file.id}`}
                checked={selected.includes(file.id)}
                onCheckedChange={(checked) =>
                  setSelected((current) =>
                    checked === true ? [...current, file.id] : current.filter((value) => value !== file.id)
                  )
                }
              />
              <Label htmlFor={`${id}-${file.id}`}>{file.label}</Label>
            </div>
          ))}
        </div>
      </FieldSet>
    </div>
  )
}

Sizes

Compare the sm and md checkbox sizes. Both sizes keep a 24px pointer target around the visible box.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"

import { Label } from "@/components/ui/label"

export function CheckboxSizes() {
  const id = React.useId()
  
  
  

  return (
    <div className="flex flex-col gap-6">
      <div className="flex flex-col gap-3">
        <div className="flex items-center gap-2">
          <Checkbox id={`${id}-size-sm`} size="sm" defaultChecked />
          <Label htmlFor={`${id}-size-sm`} className="text-label-xs">
            Small
          </Label>
        </div>
        <div className="flex items-center gap-2">
          <Checkbox id={`${id}-size-md`} defaultChecked />
          <Label htmlFor={`${id}-size-md`}>Medium</Label>
        </div>
      </div>
    </div>
  )
}

API reference

Generated from the component's TypeScript signatures. Inherited DOM and primitive props are listed separately.

Checkbox

Represent an independently selectable choice or mixed selection.

Checkbox props
PropTypeDefaultDescription
inputMode"search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal" | undefined—Hints at the type of data that might be entered by the user while editing the element or its contents
aria-describedbystring | undefined—Identifies the element (or elements) that describes the object.
aria-invalidboolean | "false" | "true" | "grammar" | "spelling" | undefined—Indicates the entered value does not conform to the format expected by the application.
aria-labelstring | undefined—Defines a string value that labels the current element.
aria-labelledbystring | undefined—Identifies the element (or elements) that labels the current element.
childrenReact.ReactNode—Represents all of the things React can render.
size"sm" | "md" | undefined"md"Box size: sm (16px) or md (20px). Both keep a 24px pointer target. Defaults to md.
Inherited props (22)
Checkbox inherited props
PropTypeDefaultDescription
checkedCheckboxPrimitive.CheckedState | undefined—Current checked state. Apply the requested value from onCheckedChange when the parent owns this state.
defaultCheckedCheckboxPrimitive.CheckedState | undefined—Initial checked state for a control supporting uncontrolled selection. Inherited DOM typing does not give a layout wrapper checkbox behavior.
requiredboolean | undefined—Mark this control as required using its native or primitive semantics. Consumers own contextual validation feedback.
onCheckedChange((checked: CheckboxPrimitive.CheckedState) => void) | undefined—Receive the next checked state. Apply it to checked when the parent controls the component.
formstring | undefined—ID of the form associated with this control, including when it is rendered outside that form element.
classNamestring | undefined—Additional classes for this part. Preserve contrast, visible focus and responsive content when adapting styles.
defaultValuestring | number | readonly string[] | undefined—Initial value for a control supporting uncontrolled state. Inherited DOM typing alone does not give a layout wrapper input behavior.
autoFocusboolean | undefined—Request automatic focus when the element mounts or the primitive opens, according to its source behavior. Choose initial focus deliberately in context.
idstring | undefined—Stable DOM identifier used by labels, descriptions or other explicit associations.
roleReact.AriaRole | undefined—Semantic role appropriate to the rendered element. Preserve required relationships and keyboard behavior.
onFocusReact.FocusEventHandler<HTMLButtonElement> | undefined—Receive the native focus event. Preserve the component's existing keyboard and focus behavior.
onBlurReact.FocusEventHandler<HTMLButtonElement> | undefined—Receive the native blur event when focus leaves this control.
onChangeReact.ChangeEventHandler<HTMLButtonElement, Element> | undefined—Receive the native change event; read the actual control value or checked state from its event target.
onKeyDownReact.KeyboardEventHandler<HTMLButtonElement> | undefined—Receive keyboard events. Preserve built-in keyboard behavior unless deliberately implementing an accessible alternative.
onClickReact.MouseEventHandler<HTMLButtonElement> | undefined—Receive activation events. The consumer owns the action performed and its result.
onSelectReact.ReactEventHandler<HTMLButtonElement> | undefined—Handle the selection event defined by this part's source signature. DOM text-selection events differ from menu or command activation callbacks.
asChildboolean | undefined—Merge this part into one child instead of its default element. Preserve that child's semantics, ref support and accessibility attributes.
disabledboolean | undefined—Make the supported control unavailable through its native or primitive disabled semantics. This does not disable arbitrary child links.
namestring | undefined—Name used by the control's form submission or primitive form integration.
type"button" | "submit" | "reset" | undefined—Native element or primitive mode defined by the source union. Choose a supported value; its meaning depends on the component.
valuestring | number | readonly string[] | undefined—Current value in the shape defined by the source type. For controlled interactive inputs, apply updates from the change callback.
refReact.Ref<HTMLButtonElement> | undefined—Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref).

Accessibility & usage

From registry/ui/checkbox.meta.json (authored guidance; human review may still be pending). Behavior is checked by Storybook tests, not certified here.

When to use

  • Independent options in a list where any number can be selected.
  • A single acknowledgement or consent that is confirmed when the form is submitted.
  • A parent 'Select all' that shows the mixed state when only some children are selected.

When not to use

  • One choice from mutually exclusive options.
  • A setting that applies immediately when changed.
  • Actions; a checkbox never triggers an operation by itself.

Anatomy

Checkbox
The Radix checkbox button (role=checkbox) with checked, defaultChecked, onCheckedChange, required, disabled and name. size is sm (16px) or md (20px, default); both keep a 24px pointer target. Inside a form it submits through a hidden native input.
Indicator
Check or minus icon shown for the checked and indeterminate states; the state is also exposed through aria-checked.
Label
A Label with htmlFor matching the checkbox id; clicking it toggles the checkbox.

Keyboard & interaction

  • Space toggles the focused checkbox; Enter does not toggle it, following the WAI-ARIA checkbox pattern.
  • Each checkbox is its own Tab stop; there is no arrow-key navigation between checkboxes.
  • checked="indeterminate" exposes aria-checked="mixed"; activating it moves to checked.
  • aria-invalid shows the error border; pair it with an associated error message.
  • Disabled checkboxes leave the Tab order and ignore pointer input.

Responsive behavior

  • The box looks 20 CSS px but its hit area is 24x24 CSS px (WCAG 2.5.8); the associated label extends the target further. Keep the hit area if you resize the box.
  • Stack checkbox groups vertically; place short options in a wrapping row only when labels are one or two words.
  • Long labels wrap; align the box with the first line (items-start with a small top offset).

Content guidelines

  • Write labels as positive statements of the checked state ('Send me product updates').
  • Group related checkboxes under a fieldset legend and add a hint such as 'Choose any' when the rule is not obvious.
  • Error for required acknowledgement: say what is needed to continue ('Confirm the billing details to place the order').
  • Use a description under the label for consequences, not a longer label.

Alternatives

  • Radio group — Exactly one choice from a visible set.
  • Switch — An on/off setting that takes effect immediately.
  • Toggle — A pressed formatting or view option in a toolbar.

Resources