← Components

Selection and input · Material 3: Checkbox

Checkbox

Select any number of options, or confirm one. The check draws itself in.

Examples and variants

States

Checked, unchecked, indeterminate and disabled.

Example
function CheckboxStates() {
  return (
    <div className="flex flex-col gap-3">
      <Label className="flex items-center gap-2">
        <Checkbox defaultChecked />
        Email me updates
      </Label>
      <Label className="flex items-center gap-2">
        <Checkbox />
        Weekly digest
      </Label>
      <Label className="flex items-center gap-2">
        <Checkbox indeterminate />
        Some projects
      </Label>
      <Label className="flex items-center gap-2 text-(--field-label-disabled)">
        <Checkbox disabled />
        Disabled
      </Label>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/checkbox.json
npx shadcn@latest add https://isotopi.com/r/label.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install @base-ui/react cn
  2. Copy these 2 files to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/checkbox.tsxcheckbox
      components/ui/checkbox.tsx
      "use client"
      
      import * as React from "react"
      import { Checkbox as CheckboxPrimitive } from "@base-ui/react/checkbox"
      import { cn } from "cn"
      
      type Mark = "in" | "out" | "off"
      
      function quietMotion(node: HTMLElement | null) {
        if (typeof window === "undefined") return true
        if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return true
        const motion = node?.closest<HTMLElement>("[data-motion]")?.dataset.motion
        return motion === "calm" || motion === "still"
      }
      
      function Checkbox({
        className,
        indeterminate,
        onCheckedChange,
        ...props
      }: CheckboxPrimitive.Root.Props) {
        const rootRef = React.useRef<HTMLElement>(null)
        const [mark, setMark] = React.useState<Mark>(props.defaultChecked ? "in" : "off")
      
        return (
          <CheckboxPrimitive.Root
            ref={rootRef}
            data-slot="checkbox"
            indeterminate={indeterminate}
            className={cn(
              "hit-target peer relative flex size-(--checkbox-size) shrink-0 items-center justify-center rounded-(--checkbox-corner) border-0 bg-(--checkbox-container) text-(--checkbox-mark) shadow-1 outline-none hover:bg-(--checkbox-container-hover) focus-visible:shadow-(--focus-ring) data-disabled:cursor-not-allowed data-disabled:bg-(--checkbox-container-disabled) data-disabled:shadow-none active:scale-(--motion-scale-press)",
              className
            )}
            {...props}
            onCheckedChange={(checked, details) => {
              onCheckedChange?.(checked, details)
              if (checked) {
                setMark("in")
                return
              }
              setMark(quietMotion(rootRef.current) ? "off" : "out")
            }}
          >
            <span aria-hidden className="checkbox-fill" />
            {indeterminate ? (
              <span aria-hidden className="checkbox-dash" />
            ) : mark === "off" ? null : (
              <svg
                viewBox="0 0 16 16"
                aria-hidden
                className={cn("relative size-icon-sm", mark === "out" && "checkbox-erase")}
                onAnimationEnd={() => {
                  if (mark === "out") setMark("off")
                }}
              >
                <path
                  d="M3.5 8.2 6.6 11.2 12.6 4.8"
                  fill="none"
                  stroke="currentColor"
                  strokeWidth="2"
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  className="checkbox-draw"
                />
              </svg>
            )}
          </CheckboxPrimitive.Root>
        )
      }
      
      export { Checkbox }
      
    • components/ui/label.tsxlabel
      components/ui/label.tsx
      "use client"
      
      import * as React from "react"
      import { cn } from "cn"
      
      function Label({ className, ...props }: React.ComponentProps<"label">) {
        return (
          <label
            data-slot="label"
            className={cn(
              "flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:text-(--field-label-disabled) group-data-[invalid=true]/field:text-(--color-danger-fg) peer-disabled:cursor-not-allowed peer-disabled:text-(--field-label-disabled)",
              className
            )}
            {...props}
          />
        )
      }
      
      export { Label }
      

Imports

What the examples on this page import:

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

Anatomy

  1. 1Box · --checkbox-size, --checkbox-corner, --checkbox-container; checked fills --checkbox-container-checked.
  2. 2Mark · A check or dash in --checkbox-mark.
  3. 3Label · Wrap in Label so the text toggles it too.

Props

PropTypeDefaultDescription
checked / defaultCheckedboolean—Checked state.
indeterminateboolean—Shows a dash (some children selected).
onCheckedChange(checked: boolean) => void—Called on change.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--checkbox-sizevar(--size-checkbox)16pxsame
--checkbox-cornervar(--shape-xs)6pxsame
--checkbox-containervar(--surface-container)oklch(0.965 0.005 95)oklch(0.12 0 0)
--checkbox-container-checkedvar(--color-brand)oklch(0.18 0.008 260)oklch(0.98 0 0)
--checkbox-markvar(--color-fg-on-accent)oklch(0.97 0.004 95)oklch(0.14 0 0)
--checkbox-container-hovercolor-mix(in oklch, var(--checkbox-container), var(--on-surface) var(--state-hover-mix))color-mix(in oklch, var(--checkbox-container), var(--on-surface) var(--state-hover-mix))same
--checkbox-container-disabledvar(--color-bg-disabled)color-mix(in oklch, var(--color-fg-default) var(--state-disabled-container-mix), transparent)same
--checkbox-container-checked-disabledcolor-mix(in oklch, var(--color-fg-disabled) 55%, transparent)color-mix(in oklch, var(--color-fg-disabled) 55%, transparent)same

Motion

TriggerWhat happensTokens
CheckThe box squashes and the check path draws in.--duration-fast, --ease-bounce

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • role=checkbox with aria-checked (true, false or mixed).
  • Space toggles.
  • The label is part of the hit area.

Usage

Do

  • Use for independent options.

Don’t

  • Don't use for a single on/off setting that applies at once (use Switch).

Code

components/ui/checkbox.tsx
"use client"

import * as React from "react"
import { Checkbox as CheckboxPrimitive } from "@base-ui/react/checkbox"
import { cn } from "cn"

type Mark = "in" | "out" | "off"

function quietMotion(node: HTMLElement | null) {
  if (typeof window === "undefined") return true
  if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return true
  const motion = node?.closest<HTMLElement>("[data-motion]")?.dataset.motion
  return motion === "calm" || motion === "still"
}

function Checkbox({
  className,
  indeterminate,
  onCheckedChange,
  ...props
}: CheckboxPrimitive.Root.Props) {
  const rootRef = React.useRef<HTMLElement>(null)
  const [mark, setMark] = React.useState<Mark>(props.defaultChecked ? "in" : "off")

  return (
    <CheckboxPrimitive.Root
      ref={rootRef}
      data-slot="checkbox"
      indeterminate={indeterminate}
      className={cn(
        "hit-target peer relative flex size-(--checkbox-size) shrink-0 items-center justify-center rounded-(--checkbox-corner) border-0 bg-(--checkbox-container) text-(--checkbox-mark) shadow-1 outline-none hover:bg-(--checkbox-container-hover) focus-visible:shadow-(--focus-ring) data-disabled:cursor-not-allowed data-disabled:bg-(--checkbox-container-disabled) data-disabled:shadow-none active:scale-(--motion-scale-press)",
        className
      )}
      {...props}
      onCheckedChange={(checked, details) => {
        onCheckedChange?.(checked, details)
        if (checked) {
          setMark("in")
          return
        }
        setMark(quietMotion(rootRef.current) ? "off" : "out")
      }}
    >
      <span aria-hidden className="checkbox-fill" />
      {indeterminate ? (
        <span aria-hidden className="checkbox-dash" />
      ) : mark === "off" ? null : (
        <svg
          viewBox="0 0 16 16"
          aria-hidden
          className={cn("relative size-icon-sm", mark === "out" && "checkbox-erase")}
          onAnimationEnd={() => {
            if (mark === "out") setMark("off")
          }}
        >
          <path
            d="M3.5 8.2 6.6 11.2 12.6 4.8"
            fill="none"
            stroke="currentColor"
            strokeWidth="2"
            strokeLinecap="round"
            strokeLinejoin="round"
            className="checkbox-draw"
          />
        </svg>
      )}
    </CheckboxPrimitive.Root>
  )
}

export { Checkbox }