🚀ReacUI is currently in Beta. We're actively improving it! ❣️

Checkbox

A checkbox component for selecting multiple options from a set

Installation

The Checkbox component is part of the ReacUI library. Install the package to use it in your project.

npm install reacui

Import

Import the Checkbox component into your React component.

import { Checkbox } from 'reacui';

Basic Usage

The Checkbox component provides a styled checkbox input.

<Checkbox id="basic-checkbox" />

With Labels

Combine checkboxes with labels for better accessibility and user experience.

<div className="flex items-center">
  <Checkbox id="terms" />
  <label htmlFor="terms" className="ml-2 block text-sm text-gray-900 dark:text-white">
    I agree to the terms and conditions
  </label>
</div>

<div className="flex items-center">
  <Checkbox id="newsletter" />
  <label htmlFor="newsletter" className="ml-2 block text-sm text-gray-900 dark:text-white">
    Subscribe to our newsletter
  </label>
</div>

Checkbox States

Checkboxes can be in different states such as checked or disabled.

<div className="flex items-center">
  <Checkbox id="unchecked" />
  <label htmlFor="unchecked" className="ml-2 block text-sm text-gray-900 dark:text-white">
    Unchecked
  </label>
</div>

<div className="flex items-center">
  <Checkbox id="checked" defaultChecked />
  <label htmlFor="checked" className="ml-2 block text-sm text-gray-900 dark:text-white">
    Checked
  </label>
</div>

<div className="flex items-center">
  <Checkbox id="disabled-unchecked" disabled />
  <label htmlFor="disabled-unchecked" className="ml-2 block text-sm text-gray-500 dark:text-gray-400">
    Disabled Unchecked
  </label>
</div>

<div className="flex items-center">
  <Checkbox id="disabled-checked" defaultChecked disabled />
  <label htmlFor="disabled-checked" className="ml-2 block text-sm text-gray-500 dark:text-gray-400">
    Disabled Checked
  </label>
</div>

In Forms

Use checkboxes in forms to collect multiple selections.

Notification Preferences

Get notified when new features are released

Receive product tips and promotional offers

<div className="p-6 bg-white dark:bg-secondary-800 border border-gray-200 dark:border-gray-700 rounded-lg shadow-sm">
  <h3 className="text-lg font-medium text-gray-900 dark:text-white mb-4">Notification Preferences</h3>
  <form className="space-y-4">
    <div className="flex items-start">
      <div className="flex items-center h-5">
        <Checkbox id="email-updates" defaultChecked />
      </div>
      <div className="ml-3 text-sm">
        <label htmlFor="email-updates" className="font-medium text-gray-700 dark:text-gray-200">
          Email updates
        </label>
        <p className="text-gray-500 dark:text-gray-400">Get notified when new features are released</p>
      </div>
    </div>
    <div className="flex items-start">
      <div className="flex items-center h-5">
        <Checkbox id="marketing" />
      </div>
      <div className="ml-3 text-sm">
        <label htmlFor="marketing" className="font-medium text-gray-700 dark:text-gray-200">
          Marketing emails
        </label>
        <p className="text-gray-500 dark:text-gray-400">Receive product tips and promotional offers</p>
      </div>
    </div>
    <div className="pt-4 border-t border-gray-200 dark:border-gray-700">
      <Button variant="primary">Save preferences</Button>
    </div>
  </form>
</div>

API Reference

The following props are available for the Checkbox component.

PropTypeDefaultDescription
idstringundefinedID for the checkbox input, useful for linking with labels
checkedbooleanundefinedControlled checked state of the checkbox
defaultCheckedbooleanfalseInitial checked state for uncontrolled checkbox
disabledbooleanfalseWhether the checkbox is disabled
onChangefunctionundefinedFunction called when checkbox state changes
classNamestring''Additional CSS classes to apply to the checkbox