New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

@bento/checkbox

Package Overview
Dependencies
Maintainers
3
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bento/checkbox

Checkbox component

Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
6
-77.78%
Maintainers
3
Weekly downloads
 
Created
Source

import { Meta, ArgTypes, Story, Controls, Source, } from '@storybook/addon-docs/blocks';

Checkbox

The @bento/checkbox package provides accessible and customizable checkbox inputs. It exports the CheckboxGroup and Checkbox primitives, enabling you to build individual or groups of checkboxes with consistent keyboard navigation, focus management, and ARIA support. React Aria is used to ensure that the checkboxes are accessible to all users.

The CheckboxGroup allows a user to select multiple items from a list of Checkbox components.

The Checkbox is a single checkbox option that can be selected by the user.

Installation

npm install --save @bento/checkbox

Props

The following properties are available to be used on the CheckboxGroup and Checkbox primitives:

Checkbox

PropTypeRequiredDescription
valuestringNoThe value of the checkbox, used when submitting an HTML form.
namestringNoThe name of the checkbox.
inputRefRef<HTMLInputElement>NoA ref for the HTML input element.
childrenReactNodeNoThe label for the checkbox. Accepts any renderable node.
isRequiredbooleanNoWhether the checkbox is required or not.
isReadOnlybooleanNoWhether the input can be selected but not changed by the user.
isDisabledbooleanNoWhether the checkbox is disabled or not. Shows that a selection exists,
but is not available in that circumstance.
autoFocusbooleanNoWhether the element should receive focus on render.
isIndeterminatebooleanNoWhether the checkbox is in an indeterminate state.
isSelectedbooleanNoWhether the checkbox is in a selected state.
slotstringNoA named part of a component that can be customized. This is implemented by the consuming component.
The exposed slot names of a component are available in the components documentation.
slotsRecord<string, object | Function>NoAn object that contains the customizations for the slots.
The main way you interact with the slot system as a consumer.
refRef<HTMLDivElement> | LegacyRef<Component<CheckboxProps & Slots, any, any>>NoAllows getting a ref to the component instance.
Once the component unmounts, React will set ref.current to null
(or call the ref with null if you passed a callback ref).
@see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom React Docs}
as"div"No

CheckboxGroup

PropTypeRequiredDescription
valuestring[]NoThe current value of the checkbox group (controlled).
defaultValuestring[]NoThe default value of the checkbox group (uncontrolled).
isDisabledbooleanNoWhether the input is disabled.
isReadOnlybooleanNoWhether the input can be selected but not changed by the user.
isRequiredbooleanNoWhether user input is required on the input before form submission.
isInvalidbooleanNoWhether the input value is invalid.
namestringNoThe name of the input element, used when submitting an HTML form.
formstringNoThe <form> element to associate the input with.
The value of this attribute must be the id of a <form> in the same document.
childrenReactNodeNoCheckbox children.
slotstringNoA named part of a component that can be customized. This is implemented by the consuming component.
The exposed slot names of a component are available in the components documentation.
slotsRecord<string, object | Function>NoAn object that contains the customizations for the slots.
The main way you interact with the slot system as a consumer.
refRef<HTMLDivElement> | LegacyRef<Component<CheckboxGroupProps & Slots, any, any>>NoAllows getting a ref to the component instance.
Once the component unmounts, React will set ref.current to null
(or call the ref with null if you passed a callback ref).
@see {@link https://react.dev/learn/referencing-values-with-refs#refs-and-the-dom React Docs}
as"div"No

Data Attributes, Slot Map and Props

Data Attributes

The following data attributes can be used to style and customize the CheckboxGroup and Checkbox primitives.

CheckboxGroup Data Attributes

AttributeDescriptionExample Values
data-disabledIndicates the checkbox group is disabled"true"
data-readonlyIndicates the checkbox group is readonly"true"
data-requiredIndicates the checkbox group is required"true"
data-invalidIndicates the checkbox group is invalid"true"

Checkbox Data Attributes

AttributeDescriptionExample Values
data-disabledIndicates the checkbox is disabled"true"
data-readonlyIndicates the checkbox is readonly"true"
data-requiredIndicates the checkbox is required"true"
data-invalidIndicates the checkbox is invalid"true"
data-indeterminateIndicates the checkbox is in an indeterminate state"true"
data-pressedIndicates the checkbox is being pressed"true"
data-hoveredIndicates the checkbox is hovered"true"
data-focusedIndicates the checkbox has focus"true"
data-focus-visibleIndicates focus should be visible"true"
data-selectedIndicates the checkbox is selected"true"

CheckboxGroup Slot Map

Slot NameDescription
labelLabel for checkbox group
descriptionDescription for checkbox group
errorError for checkbox group

Checkbox Slot Map

Slot NameDescription
icon-checkedIcon for checked checkbox
icon-uncheckedIcon for unchecked checkbox
icon-indeterminateIcon for indeterminate checkbox

Examples

Default Checkbox - Uncontrolled

The Checkbox primitive is uncontrolled by default, meaning it manages its own state internally. You can also use it in a controlled manner by providing the checked and onChange props.

/* v8 ignore next */
import React from 'react';
import { Checkbox } from '@bento/checkbox';

export function CheckboxExample(args: React.ComponentProps<typeof Checkbox>) {
  return (
    <Checkbox name="checkbox-example" value="checkbox-value" {...args}>
      Checkbox Label
    </Checkbox>
  );
}

Controlled Checkbox

The Checkbox can also be used in a controlled manner, where you manage its state externally with the checked and onChange props.

/* v8 ignore next */
import React, { useState } from 'react';
import { Checkbox } from '@bento/checkbox';

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

  return (
    <Checkbox
      name="checkbox-controlled-example"
      value="checkbox-controlled-value"
      isSelected={checked}
      onChange={setChecked}
    >
      Controlled Checkbox
    </Checkbox>
  );
}

Checkbox Group

The CheckboxGroup allows a user to select multiple items from a list of Checkbox components.

/* v8 ignore next */
import React from 'react';
import { Checkbox, CheckboxGroup } from '@bento/checkbox';
import { Text } from '@bento/text';
import { FieldError } from '@bento/field-error';

export function CheckboxGroupExample(args: React.ComponentProps<typeof CheckboxGroup>) {
  return (
    <CheckboxGroup name="checkbox-group-example" {...args}>
      <Text slot="label">Checkbox Group</Text>
      <Checkbox value="checkbox-1">Checkbox 1</Checkbox>
      <Checkbox value="checkbox-2">Checkbox 2</Checkbox>
      <Text slot="description">Select your options</Text>
      <FieldError slot="error">This is an error message</FieldError>
    </CheckboxGroup>
  );
}

Controlled Checkbox Group

The CheckboxGroup can also be used in a controlled manner, where you manage the state of the checkboxes externally with the value and onChange props.

/* v8 ignore next */
import React, { useState } from 'react';
import { Checkbox, CheckboxGroup } from '@bento/checkbox';
import { Text } from '@bento/text';

export function CheckboxGroupControlledExample() {
  const [selectedValues, setSelectedValues] = useState<string[]>([]);

  return (
    <CheckboxGroup value={selectedValues} onChange={setSelectedValues}>
      <Text slot="label">Checkbox Group</Text>
      <Checkbox value="option1">Option 1</Checkbox>
      <Checkbox value="option2">Option 2</Checkbox>
      <Checkbox value="option3">Option 3</Checkbox>
    </CheckboxGroup>
  );
}

Checkbox Group with Indeterminate State

The CheckboxGroup can also support an indeterminate state, which is useful when some but not all checkboxes are selected. The complex state management shown here is an example of how to handle indeterminate states in a checkbox group in a common way.

/* v8 ignore next */
import React, { useState } from 'react';
import { Checkbox, CheckboxGroup } from '@bento/checkbox';
import { Text } from '@bento/text';

export function CheckboxGroupIndeterminateExample() {
  const [checkedItems, setCheckedItems] = useState<Set<string>>(new Set());

  const items = [
    { id: 'option1', label: 'Option 1' },
    { id: 'option2', label: 'Option 2' },
    { id: 'option3', label: 'Option 3' }
  ];

  const allItemIds = items.map(function getItemId(item) {
    return item.id;
  });
  const isAllSelected = checkedItems.size === items.length;
  const isIndeterminate = checkedItems.size > 0 && checkedItems.size < items.length;

  function handleSelectAll(checked: boolean) {
    if (checked) {
      setCheckedItems(new Set([...allItemIds, 'select-all']));
    } else {
      setCheckedItems(new Set());
    }
  }

  function handleItemChange(itemId: string, checked: boolean) {
    setCheckedItems(function updateCheckedItems(prev) {
      const newSet = new Set(prev);
      if (checked) {
        newSet.add(itemId);
        if (
          allItemIds.every(function checkItemInSet(id) {
            return newSet.has(id);
          })
        ) {
          newSet.add('select-all');
        }
      } else {
        newSet.delete(itemId);
        newSet.delete('select-all');
      }
      return newSet;
    });
  }

  return (
    <CheckboxGroup value={Array.from(checkedItems)} data-value={Array.from(checkedItems)}>
      <Text slot="label">Select Items</Text>
      <Checkbox
        name="select-all"
        value="select-all"
        isSelected={isAllSelected}
        isIndeterminate={isIndeterminate}
        onChange={handleSelectAll}
      >
        Select All
      </Checkbox>

      {items.map((item) => (
        <Checkbox
          key={item.id}
          name={item.id}
          value={item.id}
          isSelected={checkedItems.has(item.id)}
          onChange={function handleItemChangeForItem(checked) {
            return handleItemChange(item.id, checked);
          }}
        >
          {item.label}
        </Checkbox>
      ))}
    </CheckboxGroup>
  );
}

Keywords

accessibility

FAQs

Package last updated on 04 Dec 2025

Related posts