Basic UI
  • Docs
  • Compressor
  • Parser
Getting Started
  • Introduction
  • Themenew
Components
  • Button
  • Button Group
  • Badge
  • Radio Group
  • Checkbox
  • Avatar
  • Label
  • File Dropzone
  1. Docs/
  2. Components/
  3. Checkbox

Checkbox

Previous pageNext page

A control that allows users to toggle a single option on or off.

Loading...

Installation

pnpm dlx shadcn@latest add @basic-ui/checkbox

Usage

"use client"
 
import { useState } from 'react';
import { Checkbox, CheckboxWrapper } from '@/registry/basic-ui/checkbox';










Previous
Radio Group
Next
Avatar

On This Page

InstallationUsageExamplesSizesDesign SystemSizePropsCheckboxWrapper PropsCheckbox Props
checkbox-default
export default function Example() {
const [checked, setChecked] = useState(false);
return (
<CheckboxWrapper htmlFor="terms">
<Checkbox id="terms" checked={checked} onCheckedChange={setChecked} />
<span>Accept terms and conditions</span>
</CheckboxWrapper>
);
}

Examples

Sizes

Displays the available size tokens for the checkbox item along with shared wrappers.

Loading...

Design System

Size

SizeHeightWidthRadius
s--checkbox-radio-height-s--checkbox-radio-width-s--checkbox-radio-radius-s
r--checkbox-radio-height-r--checkbox-radio-height-r--checkbox-radio-radius-r
m--checkbox-radio-height-m--checkbox-radio-height-m--checkbox-radio-radius-m

Props

CheckboxWrapper Props

PropTypeDefaultRequiredDescription
htmlForstringundefinedYesThe HTML for attribute of the checkbox wrapper
childrenReactNodeundefinedYesThe children of the checkbox wrapper
classNamestringundefinedNoThe class name of the checkbox wrapper

Checkbox Props

PropTypeDefaultRequiredDescription
idstringundefinedYesThe id of the checkbox
classNamestringundefinedNoThe class name of the checkbox
checkedbooleanfalseNoWhether the checkbox is checked
onCheckedChange(checked: boolean) => voidundefinedNoThe function to call when the checkbox is checked
defaultCheckedbooleanfalseNoWhether the checkbox is default checked
indeterminatebooleanfalseNoWhether the checkbox is indeterminate
disabledbooleanfalseNoWhether the checkbox is disabled
sizes / r / mrNoThe size of the checkbox
Email preferences
checkbox-size
Checkbox sizes
'use client';

import { Checkbox, CheckboxWrapper } from '@/registry/basic-ui/checkbox';

export function CheckboxDefault() {
  return (
    <fieldset className="space-y-3">
      <legend className="text-sm font-semibold text-(--grey-text-alpha-1-default)">
        Email preferences
      </legend>
      <CheckboxWrapper htmlFor="insights">
        <Checkbox id="insights" />
        <span>unchecked</span>
      </CheckboxWrapper>
      <CheckboxWrapper htmlFor="default-checked">
        <Checkbox id="default-checked" defaultChecked />
        <span>default checked</span>
      </CheckboxWrapper>
      <CheckboxWrapper htmlFor="half-checked">
        <Checkbox id="half-checked" indeterminate />
        <span>half checked</span>
      </CheckboxWrapper>
      <CheckboxWrapper htmlFor="disabled-checked">
        <Checkbox id="disabled-checked" disabled={true} defaultChecked />
        <span>disabled checked</span>
      </CheckboxWrapper>
      <CheckboxWrapper htmlFor="disabled-half-checked">
        <Checkbox id="disabled-half-checked" disabled={true} indeterminate />
        <span>disabled half checked</span>
      </CheckboxWrapper>
      <CheckboxWrapper htmlFor="disabled-unchecked">
        <Checkbox id="disabled-unchecked" disabled={true} />
        <span>disabled unchecked</span>
      </CheckboxWrapper>
    </fieldset>
  );
}
'use client';

import { Checkbox, CheckboxWrapper } from '@/registry/basic-ui/checkbox';

const sizeVariants: Array<{
  id: string;
  label: string;
  size: 's' | 'r' | 'm';
}> = [
  { id: 'checkbox-size-s', label: 'Small (s)', size: 's' },
  { id: 'checkbox-size-r', label: 'Regular (r)', size: 'r' },
  { id: 'checkbox-size-m', label: 'Medium (m)', size: 'm' },
];

export function CheckboxSize() {
  return (
    <fieldset className="space-y-3">
      <legend className="text-sm font-semibold text-(--grey-text-alpha-1-default)">
        Checkbox sizes
      </legend>
      {sizeVariants.map(({ id, label, size }) => (
        <CheckboxWrapper key={id} htmlFor={id}>
          <Checkbox id={id} size={size} defaultChecked={size === 'r'} />
          <span>{label}</span>
        </CheckboxWrapper>
      ))}
    </fieldset>
  );
}