Gecko UIGecko UI

Checkbox

Customizable checkbox with support for indeterminate state

Checkbox

A checkbox component with custom styling and support for indeterminate (partial) state, useful for "select all" scenarios where only some items are selected.

Installation

import { Checkbox } from '@geckoui/geckoui';

Basic Usage

import { useState } from 'react';

function Example() {
  const [checked, setChecked] = useState(false);

  return (
    <Checkbox
      checked={checked}
      onChange={(e) => setChecked(e.target.checked)}
    />
  );
}

Props API

PropTypeDefaultDescription
checkedboolean-Controlled checked state
partialbooleanfalseDisplay indeterminate icon (for partial selection)
disabledbooleanfalseDisable the checkbox
classNamestring-CSS class for the checkbox box
onChangeChangeEventHandler-Callback when checked state changes
...restInputHTMLAttributes-All standard HTML input attributes

Examples

With Label

import { useState } from 'react';

function CheckboxWithLabel() {
  const [agreed, setAgreed] = useState(false);

  return (
    <label className="flex items-center gap-2 cursor-pointer">
      <Checkbox
        checked={agreed}
        onChange={(e) => setAgreed(e.target.checked)}
      />
      <span>I agree to the terms and conditions</span>
    </label>
  );
}

Indeterminate State

import { useState } from 'react';

function SelectAll() {
  const [items, setItems] = useState([
    { id: 1, checked: false },
    { id: 2, checked: false },
    { id: 3, checked: false },
  ]);

  const allChecked = items.every(item => item.checked);
  const someChecked = items.some(item => item.checked);

  const handleSelectAll = (e: React.ChangeEvent<HTMLInputElement>) => {
    setItems(items.map(item => ({ ...item, checked: e.target.checked })));
  };

  return (
    <div>
      <label className="flex items-center gap-2">
        <Checkbox
          checked={someChecked || allChecked}
          partial={someChecked && !allChecked}
          onChange={handleSelectAll}
        />
        <span>Select All</span>
      </label>
      {/* Individual items... */}
    </div>
  );
}

Disabled States

<div className="space-y-2">
  <Checkbox checked disabled />
  <Checkbox disabled />
</div>

Multiple Checkboxes

function NotificationSettings() {
  const [settings, setSettings] = useState({
    email: true,
    sms: false,
    push: true,
  });

  return (
    <div className="space-y-3">
      <label className="flex items-center gap-2">
        <Checkbox
          checked={settings.email}
          onChange={(e) => setSettings({...settings, email: e.target.checked})}
        />
        <span>Email notifications</span>
      </label>
      {/* Other checkboxes... */}
    </div>
  );
}

Keyboard Navigation

The checkbox supports full keyboard navigation:

  • Space or Enter: Toggle checkbox
  • Tab: Move focus to next element
  • Shift + Tab: Move focus to previous element

Accessibility

  • Uses proper ARIA attributes (aria-checked, role="checkbox")
  • Keyboard accessible with Space and Enter keys
  • Focus management for keyboard users
  • Works with screen readers
  • Disabled state properly communicated

Styling

The component uses BEM-style class names:

  • GeckoUICheckbox - Main container
  • GeckoUICheckbox__button - Button wrapper
  • GeckoUICheckbox__box - Visual checkbox box
  • GeckoUICheckbox__input - Hidden native input
  • GeckoUICheckbox__icon - Check or indeterminate icon

Icons

  • CheckIcon: Displayed when checked={true} and partial={false}
  • IndeterminateIcon: Displayed when partial={true}
  • Radio - For mutually exclusive selections
  • Switch - Toggle alternative
  • RHFCheckbox - React Hook Form integration