Checkbox

A checkbox with its check mark built in.

Wrap a checkbox and its text in a Label, so a press on the text toggles it.

Preview
basic.tsx
import { Checkbox } from "~/components/ui/checkbox"
import { Label } from "~/components/ui/label"

export default function Example() {
  return (
    <div className="grid gap-md">
      <Label>
        <Checkbox defaultChecked />
        Send me product updates
      </Label>
      <Label>
        <Checkbox />
        Accept the terms
      </Label>
      <Label>
        <Checkbox disabled />
        Unavailable option
      </Label>
    </div>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesNothing else in the system.
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20lucide-react@1.73.0

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/checkbox.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20 lucide-react@1.73.0

Then copy the source:

components/ui/checkbox.tsx
import {
  Checkbox as CheckboxPrimitive,
  CheckboxIndicator as CheckboxIndicatorPrimitive,
} from "@ui-sdk/react/checkbox"
import { Check, Minus } from "lucide-react"
import type * as React from "react"

import { cnState } from "~/lib/utils"

/**
 * A filled checkbox with its check mark built in. Pass `children` to replace
 * the indicator. Wrap it in a `Label` with its text, or name it with `aria-label`.
 */
function Checkbox({
  className,
  children,
  ...props
}: React.ComponentProps<typeof CheckboxPrimitive>) {
  return (
    <CheckboxPrimitive
      className={cnState(
        [
          "peer relative flex size-4 shrink-0 cursor-pointer items-center justify-center rounded-sm border border-transparent bg-input/90 text-primary-foreground",
          "transition-shadow outline-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/30",
          "after:absolute after:-inset-x-3 after:-inset-y-2",
          "data-checked:border-primary data-checked:bg-primary data-indeterminate:border-primary data-indeterminate:bg-primary",
          "aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20",
          "data-disabled:cursor-not-allowed data-disabled:opacity-50",
        ].join(" "),
        className,
      )}
      {...props}
    >
      {children ?? <CheckboxIndicator />}
    </CheckboxPrimitive>
  )
}

/** Shows the check mark, or a dash while the checkbox is indeterminate. */
function CheckboxIndicator({
  className,
  children,
  ...props
}: React.ComponentProps<typeof CheckboxIndicatorPrimitive>) {
  return (
    <CheckboxIndicatorPrimitive
      aria-hidden="true"
      className={cnState("flex items-center justify-center text-current", className)}
      {...props}
    >
      {children ?? (
        <>
          <Check className="size-3.5 in-data-indeterminate:hidden" />
          <Minus className="hidden size-3.5 in-data-indeterminate:block" />
        </>
      )}
    </CheckboxIndicatorPrimitive>
  )
}

export { Checkbox, CheckboxIndicator }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Checkbox

A filled checkbox with its check mark built in. Pass children to replace the indicator. Wrap it in a Label with its text, or name it with aria-label.

checked

Whether it's checked. Pass it to control the checkbox.

Type CheckedState

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: CheckboxState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: CheckboxState) => string)

controller

Toggles it from anywhere. Create one with createCheckboxController().

Type { toggle: (details?: CheckboxChangeOptions) => boolean; getChecked: () => CheckedState; setChecked: (checked: CheckedState, details?: CheckboxChangeOptions) => boolean; focus: () => void; }

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultChecked

Whether it's checked at first, when uncontrolled. A form reset returns to it.

Type CheckedState · Default false

disabled

Disables it like a native control: it can't be focused or changed, and isn't submitted.

Type boolean

form

The id of a form to belong to, when it isn't inside that form.

Type string

name

The name it submits under. Without one, it submits nothing.

Type string

onCheckedChange

Runs when a user or a form reset wants to change it. details.reason says why, and details.cancel() stops it.

Type (checked: CheckedState, details: ChangeDetails<CheckboxCheckedChangeReason>) => void

parent

Makes it the parent of its group: it checks or unchecks every checkbox in the group, and shows checked, unchecked or indeterminate from them.

Type boolean · Default false

readOnly

Keeps it focusable and submitted, but stops changes.

Type boolean

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<CheckboxState>

required

Blocks form submission until it's checked, with the browser's own message.

Type boolean

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: CheckboxState) => CSSProperties)

value

What it submits while checked.

Type string · Default "on"

CheckboxIndicator

Shows the check mark, or a dash while the checkbox is indeterminate.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<CheckboxState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<CheckboxState>) => string)

keepMounted

Whether the indicator stays in the page while unchecked, so it can be styled or animated in both states.

Type boolean · Default false

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<CheckboxState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<CheckboxState>) => CSSProperties)