CheckboxGroup

A checkbox group allows a user to select multiple items from a list of options.

installyarn add react-aria-components
version1.2.1
usageimport {CheckboxGroup} from 'react-aria-components'

Example#


import {CheckboxGroup, Checkbox, Label} from 'react-aria-components';

<CheckboxGroup>
  <Label>Favorite sports</Label>
  <Checkbox value="soccer">
    <div className="checkbox" aria-hidden="true">
      <svg viewBox="0 0 18 18"><polyline points="1 9 7 14 15 4" /></svg>
    </div>
    Soccer
  </Checkbox>
  <Checkbox value="baseball">
    <div className="checkbox" aria-hidden="true">
      <svg viewBox="0 0 18 18"><polyline points="1 9 7 14 15 4" /></svg>
    </div>
    Baseball
  </Checkbox>
  <Checkbox value="basketball">
    <div className="checkbox" aria-hidden="true">
      <svg viewBox="0 0 18 18"><polyline points="1 9 7 14 15 4" /></svg>
    </div>
    Basketball
  </Checkbox>
</CheckboxGroup>
import {
  Checkbox,
  CheckboxGroup,
  Label
} from 'react-aria-components';

<CheckboxGroup>
  <Label>Favorite sports</Label>
  <Checkbox value="soccer">
    <div className="checkbox" aria-hidden="true">
      <svg viewBox="0 0 18 18">
        <polyline points="1 9 7 14 15 4" />
      </svg>
    </div>
    Soccer
  </Checkbox>
  <Checkbox value="baseball">
    <div className="checkbox" aria-hidden="true">
      <svg viewBox="0 0 18 18">
        <polyline points="1 9 7 14 15 4" />
      </svg>
    </div>
    Baseball
  </Checkbox>
  <Checkbox value="basketball">
    <div className="checkbox" aria-hidden="true">
      <svg viewBox="0 0 18 18">
        <polyline points="1 9 7 14 15 4" />
      </svg>
    </div>
    Basketball
  </Checkbox>
</CheckboxGroup>
import {
  Checkbox,
  CheckboxGroup,
  Label
} from 'react-aria-components';

<CheckboxGroup>
  <Label>
    Favorite sports
  </Label>
  <Checkbox value="soccer">
    <div
      className="checkbox"
      aria-hidden="true"
    >
      <svg viewBox="0 0 18 18">
        <polyline points="1 9 7 14 15 4" />
      </svg>
    </div>
    Soccer
  </Checkbox>
  <Checkbox value="baseball">
    <div
      className="checkbox"
      aria-hidden="true"
    >
      <svg viewBox="0 0 18 18">
        <polyline points="1 9 7 14 15 4" />
      </svg>
    </div>
    Baseball
  </Checkbox>
  <Checkbox value="basketball">
    <div
      className="checkbox"
      aria-hidden="true"
    >
      <svg viewBox="0 0 18 18">
        <polyline points="1 9 7 14 15 4" />
      </svg>
    </div>
    Basketball
  </Checkbox>
</CheckboxGroup>
Show CSS
@import "@react-aria/example-theme";

.react-aria-CheckboxGroup {
  display: flex;
  flex-direction: column;
  gap: 0.571rem;
  color: var(--text-color);
}
@import "@react-aria/example-theme";

.react-aria-CheckboxGroup {
  display: flex;
  flex-direction: column;
  gap: 0.571rem;
  color: var(--text-color);
}
@import "@react-aria/example-theme";

.react-aria-CheckboxGroup {
  display: flex;
  flex-direction: column;
  gap: 0.571rem;
  color: var(--text-color);
}

Features#


Checkbox groups can be built in HTML with the <fieldset> and <input> elements, however these can be difficult to style. CheckboxGroup helps achieve accessible checkbox groups that can be styled as needed.

  • Accessible – Checkbox groups are exposed to assistive technology via ARIA, and automatically associate a nested <Label>. Description and error message help text slots are supported as well.
  • HTML form integration – Each individual checkbox uses a visually hidden <input> element under the hood, which enables HTML form integration and autofill.
  • Validation – Support for native HTML constraint validation with customizable UI, custom validation functions, and server-side validation errors.

Anatomy#


ShoppingMusicTravelInputInterestsCheckbox group labelGroupCheckbox label

A checkbox group consists of a set of checkboxes, and a label. Each checkbox includes a label and a visual selection indicator. Zero or more checkboxes within the group can be selected at a time. Users may click or touch a checkbox to select it, or use the Tab key to navigate to it and the Space key to toggle it.

CheckboxGroup also supports optional description and error message elements, which can be used to provide more context about the field, and any validation messages. These are linked with the inputs via the aria-describedby attribute.

import {Checkbox, CheckboxGroup, FieldError, Label, Text} from 'react-aria-components';

<CheckboxGroup>
  <Label />
  <Checkbox />
  <Text slot="description" />
  <FieldError />
</CheckboxGroup>
import {
  Checkbox,
  CheckboxGroup,
  FieldError,
  Label,
  Text
} from 'react-aria-components';

<CheckboxGroup>
  <Label />
  <Checkbox />
  <Text slot="description" />
  <FieldError />
</CheckboxGroup>
import {
  Checkbox,
  CheckboxGroup,
  FieldError,
  Label,
  Text
} from 'react-aria-components';

<CheckboxGroup>
  <Label />
  <Checkbox />
  <Text slot="description" />
  <FieldError />
</CheckboxGroup>

Individual checkboxes must have a visual label. If the checkbox group does not have a visible label, an aria-label or aria-labelledby prop must be passed instead to identify the element to assistive technology.

Concepts#

CheckboxGroup makes use of the following concepts:

Forms
Validating and submitting form data, and integrating with form libraries.

Composed components#

A CheckboxGroup uses the following components, which may also be used standalone or reused in other components.

Label
A label provides context for an element.

Starter kits#


To help kick-start your project, we offer starter kits that include example implementations of all React Aria components with various styling solutions. All components are fully styled, including support for dark mode, high contrast mode, and all UI states. Each starter comes with a pre-configured Storybook that you can experiment with, or use as a starting point for your own component library.

Vanilla CSS
Download ZIP
Preview
Tailwind CSS
Download ZIP
Preview

Reusable wrappers#


If you will use a CheckboxGroup in multiple places in your app, you can wrap all of the pieces into a reusable component. This way, the DOM structure, styling code, and other logic are defined in a single place and reused everywhere to ensure consistency.

This example wraps CheckboxGroup and all of its children together into a single component which accepts a label prop, which is passed to the right place. It also shows how to use the description slot to render help text, and FieldError component to render validation errors.

import type {CheckboxGroupProps, CheckboxProps, ValidationResult} from 'react-aria-components';
import {FieldError, Text} from 'react-aria-components';

interface MyCheckboxGroupProps extends Omit<CheckboxGroupProps, 'children'> {
  children?: React.ReactNode;
  label?: string;
  description?: string;
  errorMessage?: string | ((validation: ValidationResult) => string);
}

function MyCheckboxGroup({
  label,
  description,
  errorMessage,
  children,
  ...props
}: MyCheckboxGroupProps) {
  return (
    <CheckboxGroup {...props}>
      {label}
      {children}
      {description && <Text slot="description">{description}</Text>}
      <FieldError>{errorMessage}</FieldError>
    </CheckboxGroup>
  );
}

interface MyCheckboxProps extends Omit<CheckboxProps, 'children'> {
  children?: React.ReactNode;
}

function MyCheckbox({ children, ...props }: MyCheckboxProps) {
  return (
    <Checkbox {...props}>
      <div className="checkbox" aria-hidden="true">
        <svg viewBox="0 0 18 18">
          <polyline points="1 9 7 14 15 4" />
        </svg>
      </div>
      {children}
    </Checkbox>
  );
}

<MyCheckboxGroup label="Favorite sports">
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
import type {
  CheckboxGroupProps,
  CheckboxProps,
  ValidationResult
} from 'react-aria-components';
import {FieldError, Text} from 'react-aria-components';

interface MyCheckboxGroupProps
  extends Omit<CheckboxGroupProps, 'children'> {
  children?: React.ReactNode;
  label?: string;
  description?: string;
  errorMessage?:
    | string
    | ((validation: ValidationResult) => string);
}

function MyCheckboxGroup({
  label,
  description,
  errorMessage,
  children,
  ...props
}: MyCheckboxGroupProps) {
  return (
    <CheckboxGroup {...props}>
      {label}
      {children}
      {description && (
        <Text slot="description">{description}</Text>
      )}
      <FieldError>{errorMessage}</FieldError>
    </CheckboxGroup>
  );
}

interface MyCheckboxProps
  extends Omit<CheckboxProps, 'children'> {
  children?: React.ReactNode;
}

function MyCheckbox(
  { children, ...props }: MyCheckboxProps
) {
  return (
    <Checkbox {...props}>
      <div className="checkbox" aria-hidden="true">
        <svg viewBox="0 0 18 18">
          <polyline points="1 9 7 14 15 4" />
        </svg>
      </div>
      {children}
    </Checkbox>
  );
}

<MyCheckboxGroup label="Favorite sports">
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
import type {
  CheckboxGroupProps,
  CheckboxProps,
  ValidationResult
} from 'react-aria-components';
import {
  FieldError,
  Text
} from 'react-aria-components';

interface MyCheckboxGroupProps
  extends
    Omit<
      CheckboxGroupProps,
      'children'
    > {
  children?:
    React.ReactNode;
  label?: string;
  description?: string;
  errorMessage?:
    | string
    | ((
      validation:
        ValidationResult
    ) => string);
}

function MyCheckboxGroup(
  {
    label,
    description,
    errorMessage,
    children,
    ...props
  }: MyCheckboxGroupProps
) {
  return (
    <CheckboxGroup
      {...props}
    >
      {label}
      {children}
      {description && (
        <Text slot="description">
          {description}
        </Text>
      )}
      <FieldError>
        {errorMessage}
      </FieldError>
    </CheckboxGroup>
  );
}

interface MyCheckboxProps
  extends
    Omit<
      CheckboxProps,
      'children'
    > {
  children?:
    React.ReactNode;
}

function MyCheckbox(
  { children, ...props }:
    MyCheckboxProps
) {
  return (
    <Checkbox {...props}>
      <div
        className="checkbox"
        aria-hidden="true"
      >
        <svg viewBox="0 0 18 18">
          <polyline points="1 9 7 14 15 4" />
        </svg>
      </div>
      {children}
    </Checkbox>
  );
}

<MyCheckboxGroup label="Favorite sports">
  <MyCheckbox value="soccer">
    Soccer
  </MyCheckbox>
  <MyCheckbox value="baseball">
    Baseball
  </MyCheckbox>
  <MyCheckbox value="basketball">
    Basketball
  </MyCheckbox>
</MyCheckboxGroup>

Value#


Default value#

An initial, uncontrolled value can be provided to the CheckboxGroup using the defaultValue prop, which accepts an array of selected items that map to the value prop on each Checkbox.

<MyCheckboxGroup
  label="Favorite sports (uncontrolled)"
  defaultValue={['soccer', 'baseball']}
>
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
<MyCheckboxGroup
  label="Favorite sports (uncontrolled)"
  defaultValue={['soccer', 'baseball']}
>
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
<MyCheckboxGroup
  label="Favorite sports (uncontrolled)"
  defaultValue={[
    'soccer',
    'baseball'
  ]}
>
  <MyCheckbox value="soccer">
    Soccer
  </MyCheckbox>
  <MyCheckbox value="baseball">
    Baseball
  </MyCheckbox>
  <MyCheckbox value="basketball">
    Basketball
  </MyCheckbox>
</MyCheckboxGroup>

Controlled value#

A controlled value can be provided using the value prop, which accepts an array of selected items, which map to the value prop on each Checkbox. The onChange event is fired when the user checks or unchecks an option. It receives a new array containing the updated selected values.

function Example() {
  let [selected, setSelected] = React.useState(['soccer', 'baseball']);

  return (
    <MyCheckboxGroup
      label="Favorite sports (controlled)"
      value={selected}
      onChange={setSelected}
    >
      <MyCheckbox value="soccer">Soccer</MyCheckbox>
      <MyCheckbox value="baseball">Baseball</MyCheckbox>
      <MyCheckbox value="basketball">Basketball</MyCheckbox>
    </MyCheckboxGroup>
  );
}
function Example() {
  let [selected, setSelected] = React.useState([
    'soccer',
    'baseball'
  ]);

  return (
    <MyCheckboxGroup
      label="Favorite sports (controlled)"
      value={selected}
      onChange={setSelected}
    >
      <MyCheckbox value="soccer">Soccer</MyCheckbox>
      <MyCheckbox value="baseball">Baseball</MyCheckbox>
      <MyCheckbox value="basketball">Basketball</MyCheckbox>
    </MyCheckboxGroup>
  );
}
function Example() {
  let [
    selected,
    setSelected
  ] = React.useState([
    'soccer',
    'baseball'
  ]);

  return (
    <MyCheckboxGroup
      label="Favorite sports (controlled)"
      value={selected}
      onChange={setSelected}
    >
      <MyCheckbox value="soccer">
        Soccer
      </MyCheckbox>
      <MyCheckbox value="baseball">
        Baseball
      </MyCheckbox>
      <MyCheckbox value="basketball">
        Basketball
      </MyCheckbox>
    </MyCheckboxGroup>
  );
}

HTML forms#

CheckboxGroup supports the name prop, paired with the Checkbox value prop, for integration with HTML forms.

<MyCheckboxGroup label="Favorite sports" name="sports">
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
<MyCheckboxGroup label="Favorite sports" name="sports">
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
<MyCheckboxGroup
  label="Favorite sports"
  name="sports"
>
  <MyCheckbox value="soccer">
    Soccer
  </MyCheckbox>
  <MyCheckbox value="baseball">
    Baseball
  </MyCheckbox>
  <MyCheckbox value="basketball">
    Basketball
  </MyCheckbox>
</MyCheckboxGroup>

Validation#


CheckboxGroup supports the isRequired prop to ensure the user selects at least one item, as well as custom client and server-side validation. Individual checkboxes also support validation, and errors from all checkboxes are aggregated at the group level. CheckboxGroup can also be integrated with other form libraries. See the Forms guide to learn more.

Group validation#

The isRequired prop at the CheckboxGroup level requires that at least one item is selected. To display validation errors, add a <FieldError> element as a child of the CheckboxGroup. This allows you to render error messages from all of the above sources with consistent custom styles.

import {Form, FieldError, Button} from 'react-aria-components';

<Form>
  <CheckboxGroup name="condiments" isRequired>    <Label>Sandwich condiments</Label>
    <MyCheckbox value="lettuce">Lettuce</MyCheckbox>
    <MyCheckbox value="tomato">Tomato</MyCheckbox>
    <MyCheckbox value="onion">Onion</MyCheckbox>
    <MyCheckbox value="sprouts">Sprouts</MyCheckbox>
    <FieldError />  </CheckboxGroup>
  <Button type="submit">Submit</Button>
</Form>
import {
  Button,
  FieldError,
  Form
} from 'react-aria-components';

<Form>
  <CheckboxGroup name="condiments" isRequired>    <Label>Sandwich condiments</Label>
    <MyCheckbox value="lettuce">Lettuce</MyCheckbox>
    <MyCheckbox value="tomato">Tomato</MyCheckbox>
    <MyCheckbox value="onion">Onion</MyCheckbox>
    <MyCheckbox value="sprouts">Sprouts</MyCheckbox>
    <FieldError />  </CheckboxGroup>
  <Button type="submit">Submit</Button>
</Form>
import {
  Button,
  FieldError,
  Form
} from 'react-aria-components';

<Form>
  <CheckboxGroup
    name="condiments"
    isRequired
  >    <Label>
      Sandwich
      condiments
    </Label>
    <MyCheckbox value="lettuce">
      Lettuce
    </MyCheckbox>
    <MyCheckbox value="tomato">
      Tomato
    </MyCheckbox>
    <MyCheckbox value="onion">
      Onion
    </MyCheckbox>
    <MyCheckbox value="sprouts">
      Sprouts
    </MyCheckbox>
    <FieldError />  </CheckboxGroup>
  <Button type="submit">
    Submit
  </Button>
</Form>
Show CSS
.react-aria-CheckboxGroup {
  .react-aria-FieldError {
    font-size: 12px;
    color: var(--invalid-color);
  }
}
.react-aria-CheckboxGroup {
  .react-aria-FieldError {
    font-size: 12px;
    color: var(--invalid-color);
  }
}
.react-aria-CheckboxGroup {
  .react-aria-FieldError {
    font-size: 12px;
    color: var(--invalid-color);
  }
}

By default, FieldError displays default validation messages provided by the browser. See Customizing error messages in the Forms guide to learn how to provide your own custom errors.

Individual Checkbox validation#

To require that specific checkboxes are checked, set the isRequired prop at the Checkbox level instead of the CheckboxGroup. The following example shows how to require that all items are selected.

<Form>
  <CheckboxGroup>
    <Label>Agree to the following</Label>
    <MyCheckbox value="terms" isRequired>Terms and conditions</MyCheckbox>
    <MyCheckbox value="privacy" isRequired>Privacy policy</MyCheckbox>
    <MyCheckbox value="cookies" isRequired>Cookie policy</MyCheckbox>    <FieldError />
  </CheckboxGroup>
  <Button type="submit">Submit</Button>
</Form>
<Form>
  <CheckboxGroup>
    <Label>Agree to the following</Label>
    <MyCheckbox value="terms" isRequired>
      Terms and conditions
    </MyCheckbox>
    <MyCheckbox value="privacy" isRequired>
      Privacy policy
    </MyCheckbox>
    <MyCheckbox value="cookies" isRequired>
      Cookie policy
    </MyCheckbox>    <FieldError />
  </CheckboxGroup>
  <Button type="submit">Submit</Button>
</Form>
<Form>
  <CheckboxGroup>
    <Label>
      Agree to the
      following
    </Label>
    <MyCheckbox
      value="terms"
      isRequired
    >
      Terms and
      conditions
    </MyCheckbox>
    <MyCheckbox
      value="privacy"
      isRequired
    >
      Privacy policy
    </MyCheckbox>
    <MyCheckbox
      value="cookies"
      isRequired
    >
      Cookie policy
    </MyCheckbox>    <FieldError />
  </CheckboxGroup>
  <Button type="submit">
    Submit
  </Button>
</Form>

Description#

The description slot can be used to associate additional help text with a checkbox group.

<CheckboxGroup>
  <Label>Pets</Label>
  <MyCheckbox value="dogs">Dogs</MyCheckbox>
  <MyCheckbox value="cats">Cats</MyCheckbox>
  <MyCheckbox value="dragons">Dragons</MyCheckbox>
  <Text slot="description">Select your pets.</Text>
</CheckboxGroup>
<CheckboxGroup>
  <Label>Pets</Label>
  <MyCheckbox value="dogs">Dogs</MyCheckbox>
  <MyCheckbox value="cats">Cats</MyCheckbox>
  <MyCheckbox value="dragons">Dragons</MyCheckbox>
  <Text slot="description">Select your pets.</Text>
</CheckboxGroup>
<CheckboxGroup>
  <Label>Pets</Label>
  <MyCheckbox value="dogs">
    Dogs
  </MyCheckbox>
  <MyCheckbox value="cats">
    Cats
  </MyCheckbox>
  <MyCheckbox value="dragons">
    Dragons
  </MyCheckbox>
  <Text slot="description">
    Select your pets.
  </Text>
</CheckboxGroup>
Show CSS
.react-aria-CheckboxGroup {
  [slot=description] {
    font-size: 12px;
  }
}
.react-aria-CheckboxGroup {
  [slot=description] {
    font-size: 12px;
  }
}
.react-aria-CheckboxGroup {
  [slot=description] {
    font-size: 12px;
  }
}

Disabled#


The entire CheckboxGroup can be disabled with the isDisabled prop.

<MyCheckboxGroup label="Favorite sports" isDisabled>
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
<MyCheckboxGroup label="Favorite sports" isDisabled>
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
<MyCheckboxGroup
  label="Favorite sports"
  isDisabled
>
  <MyCheckbox value="soccer">
    Soccer
  </MyCheckbox>
  <MyCheckbox value="baseball">
    Baseball
  </MyCheckbox>
  <MyCheckbox value="basketball">
    Basketball
  </MyCheckbox>
</MyCheckboxGroup>

To disable an individual checkbox, pass isDisabled to the Checkbox instead.

<MyCheckboxGroup label="Favorite sports">
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball" isDisabled>Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
<MyCheckboxGroup label="Favorite sports">
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball" isDisabled>
    Baseball
  </MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
<MyCheckboxGroup label="Favorite sports">
  <MyCheckbox value="soccer">
    Soccer
  </MyCheckbox>
  <MyCheckbox
    value="baseball"
    isDisabled
  >
    Baseball
  </MyCheckbox>
  <MyCheckbox value="basketball">
    Basketball
  </MyCheckbox>
</MyCheckboxGroup>

Read only#

The isReadOnly prop makes the selection immutable. Unlike isDisabled, the CheckboxGroup remains focusable. See the MDN docs for more information.

<MyCheckboxGroup
  label="Favorite sports"
  defaultValue={['baseball']}
  isReadOnly
>
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
<MyCheckboxGroup
  label="Favorite sports"
  defaultValue={['baseball']}
  isReadOnly
>
  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</MyCheckboxGroup>
<MyCheckboxGroup
  label="Favorite sports"
  defaultValue={[
    'baseball'
  ]}
  isReadOnly
>
  <MyCheckbox value="soccer">
    Soccer
  </MyCheckbox>
  <MyCheckbox value="baseball">
    Baseball
  </MyCheckbox>
  <MyCheckbox value="basketball">
    Basketball
  </MyCheckbox>
</MyCheckboxGroup>

Props#


CheckboxGroup#

NameTypeDefaultDescription
valuestring[]The current value (controlled).
defaultValuestring[]The default value (uncontrolled).
isDisabledbooleanWhether the input is disabled.
isReadOnlybooleanWhether the input can be selected but not changed by the user.
namestringThe name of the input element, used when submitting an HTML form. See MDN.
isRequiredbooleanWhether user input is required on the input before form submission.
isInvalidbooleanWhether the input value is invalid.
validate( (value: string[] )) => ValidationErrortruenullundefined

A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if validationBehavior="native". For realtime validation, use the isInvalid prop instead.

validationBehavior'native''aria''native'

Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.

childrenReactNode( (values: CheckboxGroupRenderProps{
defaultChildren: ReactNodeundefined
} )) => ReactNode
The children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: CheckboxGroupRenderProps{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: CheckboxGroupRenderProps{
defaultStyle: CSSProperties
} )) => CSSProperties
The inline style for the element. A function may be provided to compute the style based on component state.
Events
NameTypeDescription
onChange( (value: T )) => voidHandler that is called when the value changes.
onFocus( (e: FocusEvent<Target> )) => voidHandler that is called when the element receives focus.
onBlur( (e: FocusEvent<Target> )) => voidHandler that is called when the element loses focus.
onFocusChange( (isFocused: boolean )) => voidHandler that is called when the element's focus status changes.
Layout
NameTypeDescription
slotstringnull

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Accessibility
NameTypeDescription
idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
aria-errormessagestringIdentifies the element that provides an error message for the object.

Checkbox#

Within a <CheckboxGroup>, most <Checkbox> props are set automatically. The value prop is required to identify the checkbox within the group.

Show props
NameTypeDefaultDescription
inputRefMutableRefObject<HTMLInputElement>A ref for the HTML input element.
isIndeterminateboolean

Indeterminism is presentational only. The indeterminate visual representation remains regardless of user interaction.

defaultSelectedbooleanWhether the element should be selected (uncontrolled).
isSelectedbooleanWhether the element should be selected (controlled).
valuestringThe value of the input element, used when submitting an HTML form. See MDN.
isDisabledbooleanWhether the input is disabled.
isReadOnlybooleanWhether the input can be selected but not changed by the user.
isRequiredbooleanWhether user input is required on the input before form submission.
isInvalidbooleanWhether the input value is invalid.
validate( (value: boolean )) => ValidationErrortruenullundefined

A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if validationBehavior="native". For realtime validation, use the isInvalid prop instead.

autoFocusbooleanWhether the element should receive focus on render.
namestringThe name of the input element, used when submitting an HTML form. See MDN.
validationBehavior'native''aria''native'

Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.

childrenReactNode( (values: CheckboxRenderProps{
defaultChildren: ReactNodeundefined
} )) => ReactNode
The children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: CheckboxRenderProps{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: CheckboxRenderProps{
defaultStyle: CSSProperties
} )) => CSSProperties
The inline style for the element. A function may be provided to compute the style based on component state.
Events
NameTypeDescription
onChange( (isSelected: boolean )) => voidHandler that is called when the element's selection state changes.
onFocus( (e: FocusEvent<Target> )) => voidHandler that is called when the element receives focus.
onBlur( (e: FocusEvent<Target> )) => voidHandler that is called when the element loses focus.
onFocusChange( (isFocused: boolean )) => voidHandler that is called when the element's focus status changes.
onKeyDown( (e: KeyboardEvent )) => voidHandler that is called when a key is pressed.
onKeyUp( (e: KeyboardEvent )) => voidHandler that is called when a key is released.
onHoverStart( (e: HoverEvent )) => voidHandler that is called when a hover interaction starts.
onHoverEnd( (e: HoverEvent )) => voidHandler that is called when a hover interaction ends.
onHoverChange( (isHovering: boolean )) => voidHandler that is called when the hover state changes.
Layout
NameTypeDescription
slotstringnull

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Accessibility
NameTypeDescription
idstringThe element's unique identifier. See MDN.
excludeFromTabOrderboolean

Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available.

aria-controlsstringIdentifies the element (or elements) whose contents or presence are controlled by the current element.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
aria-errormessagestringIdentifies the element that provides an error message for the object.

Text#

<Text> accepts all HTML attributes.

FieldError#

A <FieldError> displays validation errors.

Show props
NameTypeDescription
childrenReactNode( (values: FieldErrorRenderProps{
defaultChildren: ReactNodeundefined
} )) => ReactNode
The children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: FieldErrorRenderProps{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: FieldErrorRenderProps{
defaultStyle: CSSProperties
} )) => CSSProperties
The inline style for the element. A function may be provided to compute the style based on component state.

Styling#


React Aria components can be styled in many ways, including using CSS classes, inline styles, utility classes (e.g. Tailwind), CSS-in-JS (e.g. Styled Components), etc. By default, all components include a builtin className attribute which can be targeted using CSS selectors. These follow the react-aria-ComponentName naming convention.

.react-aria-CheckboxGroup {
  /* ... */
}
.react-aria-CheckboxGroup {
  /* ... */
}
.react-aria-CheckboxGroup {
  /* ... */
}

A custom className can also be specified on any component. This overrides the default className provided by React Aria with your own.

<Checkbox className="my-checkbox">
  {/* ... */}
</Checkbox>
<Checkbox className="my-checkbox">
  {/* ... */}
</Checkbox>
<Checkbox className="my-checkbox">
  {/* ... */}
</Checkbox>

In addition, some components support multiple UI states (e.g. focused, placeholder, readonly, etc.). React Aria components expose states using data attributes, which you can target in CSS selectors. For example:

.react-aria-Checkbox[data-selected] {
  /* ... */
}
.react-aria-Checkbox[data-selected] {
  /* ... */
}
.react-aria-Checkbox[data-selected] {
  /* ... */
}

The className and style props also accept functions which receive states for styling. This lets you dynamically determine the classes or styles to apply, which is useful when using utility CSS libraries like Tailwind.

<Checkbox
  className={({ isPressed }) => isPressed ? 'bg-gray-700' : 'bg-gray-600'}
/>
<Checkbox
  className={({ isPressed }) =>
    isPressed ? 'bg-gray-700' : 'bg-gray-600'}
/>
<Checkbox
  className={(
    { isPressed }
  ) =>
    isPressed
      ? 'bg-gray-700'
      : 'bg-gray-600'}
/>

Render props may also be used as children to alter what elements are rendered based on the current state. For example, you could render an extra element when the checkbox is selected.

<Checkbox>
  {({isSelected}) => (
    <>
      {isSelected && <CheckIcon />}
      Subscribe
    </>
  )}
</Checkbox>
<Checkbox>
  {({isSelected}) => (
    <>
      {isSelected && <CheckIcon />}
      Subscribe
    </>
  )}
</Checkbox>
<Checkbox>
  {(
    { isSelected }
  ) => (
    <>
      {isSelected && (
        <CheckIcon />
      )}
      Subscribe
    </>
  )}
</Checkbox>

The states and selectors for each component used in a CheckboxGroup are documented below.

CheckboxGroup#

A CheckboxGroup can be targeted with the .react-aria-CheckboxGroup CSS selector, or by overriding with a custom className. It supports the following states and render props:

NameCSS SelectorDescription
isDisabled[data-disabled]Whether the checkbox group is disabled.
isReadOnly[data-readonly]Whether the checkbox group is read only.
isRequired[data-required]Whether the checkbox group is required.
isInvalid[data-invalid]Whether the checkbox group is invalid.
stateState of the checkbox group.

Checkbox#

A Checkbox can be targeted with the .react-aria-Checkbox CSS selector, or by overriding with a custom className. It supports the following states:

NameCSS SelectorDescription
isSelected[data-selected]Whether the checkbox is selected.
isIndeterminate[data-indeterminate]Whether the checkbox is indeterminate.
isHovered[data-hovered]Whether the checkbox is currently hovered with a mouse.
isPressed[data-pressed]Whether the checkbox is currently in a pressed state.
isFocused[data-focused]Whether the checkbox is focused, either via a mouse or keyboard.
isFocusVisible[data-focus-visible]Whether the checkbox is keyboard focused.
isDisabled[data-disabled]Whether the checkbox is disabled.
isReadOnly[data-readonly]Whether the checkbox is read only.
isInvalid[data-invalid]Whether the checkbox invalid.
isRequired[data-required]Whether the checkbox is required.

Text#

The help text elements within a CheckboxGroup can be targeted with the [slot=description] and [slot=errorMessage] CSS selectors, or by adding a custom className.

FieldError#

A FieldError can be targeted with the .react-aria-FieldError CSS selector, or by overriding with a custom className. It supports the following render props:

NameDescription
isInvalidWhether the input value is invalid.
validationErrorsThe current error messages for the input if it is invalid, otherwise an empty array.
validationDetailsThe native validation details for the input.

Advanced customization#


Contexts#

All React Aria Components export a corresponding context that can be used to send props to them from a parent element. This enables you to build your own compositional APIs similar to those found in React Aria Components itself. You can send any prop or ref via context that you could pass to the corresponding component. The local props and ref on the component are merged with the ones passed via context, with the local props taking precedence (following the rules documented in mergeProps).

The components in a CheckboxGroup support the following contexts:

ComponentContextPropsRef
CheckboxGroupCheckboxGroupContextCheckboxGroupPropsHTMLDivElement
CheckboxCheckboxContextCheckboxPropsHTMLLabelElement

This example shows a CheckboxDescription component that accepts a checkbox in its children and renders a description element below it. It uses the useId hook to generate a unique id for the description, and associates it with the checkbox via the aria-describedby attribute passed to the CheckboxContext provider.

import {CheckboxContext} from 'react-aria-components';
import {useId} from 'react-aria';

interface CheckboxDescriptionProps {
  children?: React.ReactNode;
  description?: string;
}

function CheckboxDescription(
  { children, description }: CheckboxDescriptionProps
) {
  let descriptionId = useId();
  return (
    <div>
      <CheckboxContext.Provider value={{ 'aria-describedby': descriptionId }}>        {children}
      </CheckboxContext.Provider>
      <small id={descriptionId}>{description}</small>
    </div>
  );
}

<MyCheckboxGroup
  label="Email settings"
  defaultValue={['newsletter', 'deals', 'notifications']}
>
  <CheckboxDescription description="Receive our newsletter once per week.">
    <MyCheckbox value="newsletter">Newsletter</MyCheckbox>
  </CheckboxDescription>
  <CheckboxDescription description="The best deals and sales for members.">
    <MyCheckbox value="deals">Deals</MyCheckbox>
  </CheckboxDescription>
  <CheckboxDescription description="Notifications about your orders.">
    <MyCheckbox value="notifications">Notifications</MyCheckbox>
  </CheckboxDescription>
</MyCheckboxGroup>
import {CheckboxContext} from 'react-aria-components';
import {useId} from 'react-aria';

interface CheckboxDescriptionProps {
  children?: React.ReactNode;
  description?: string;
}

function CheckboxDescription(
  { children, description }: CheckboxDescriptionProps
) {
  let descriptionId = useId();
  return (
    <div>
      <CheckboxContext.Provider
        value={{ 'aria-describedby': descriptionId }}
      >        {children}
      </CheckboxContext.Provider>
      <small id={descriptionId}>{description}</small>
    </div>
  );
}

<MyCheckboxGroup
  label="Email settings"
  defaultValue={['newsletter', 'deals', 'notifications']}
>
  <CheckboxDescription description="Receive our newsletter once per week.">
    <MyCheckbox value="newsletter">Newsletter</MyCheckbox>
  </CheckboxDescription>
  <CheckboxDescription description="The best deals and sales for members.">
    <MyCheckbox value="deals">Deals</MyCheckbox>
  </CheckboxDescription>
  <CheckboxDescription description="Notifications about your orders.">
    <MyCheckbox value="notifications">
      Notifications
    </MyCheckbox>
  </CheckboxDescription>
</MyCheckboxGroup>
import {CheckboxContext} from 'react-aria-components';
import {useId} from 'react-aria';

interface CheckboxDescriptionProps {
  children?:
    React.ReactNode;
  description?: string;
}

function CheckboxDescription(
  {
    children,
    description
  }: CheckboxDescriptionProps
) {
  let descriptionId =
    useId();
  return (
    <div>
      <CheckboxContext.Provider
        value={{
          'aria-describedby':
            descriptionId
        }}
      >        {children}
      </CheckboxContext.Provider>
      <small
        id={descriptionId}
      >
        {description}
      </small>
    </div>
  );
}

<MyCheckboxGroup
  label="Email settings"
  defaultValue={[
    'newsletter',
    'deals',
    'notifications'
  ]}
>
  <CheckboxDescription description="Receive our newsletter once per week.">
    <MyCheckbox value="newsletter">
      Newsletter
    </MyCheckbox>
  </CheckboxDescription>
  <CheckboxDescription description="The best deals and sales for members.">
    <MyCheckbox value="deals">
      Deals
    </MyCheckbox>
  </CheckboxDescription>
  <CheckboxDescription description="Notifications about your orders.">
    <MyCheckbox value="notifications">
      Notifications
    </MyCheckbox>
  </CheckboxDescription>
</MyCheckboxGroup>

Custom children#

CheckboxGroup passes props to its child components, such as the label, via their associated contexts. These contexts are exported so you can also consume them in your own custom components. This enables you to reuse existing components from your app or component library together with React Aria Components.

ComponentContextPropsRef
LabelLabelContextLabelPropsHTMLLabelElement
TextTextContextTextPropsHTMLElement

This example consumes from LabelContext in an existing styled label component to make it compatible with React Aria Components. The useContextProps hook merges the local props and ref with the ones provided via context by CheckboxGroup.

import type {LabelProps} from 'react-aria-components';
import {LabelContext, useContextProps} from 'react-aria-components';

const MyCustomLabel = React.forwardRef(
  (props: LabelProps, ref: React.ForwardedRef<HTMLLabelElement>) => {
    // Merge the local props and ref with the ones provided via context.
    [props, ref] = useContextProps(props, ref, LabelContext);
    // ... your existing Label component
    return <label {...props} ref={ref} />;
  }
);
import type {LabelProps} from 'react-aria-components';
import {
  LabelContext,
  useContextProps
} from 'react-aria-components';

const MyCustomLabel = React.forwardRef(
  (
    props: LabelProps,
    ref: React.ForwardedRef<HTMLLabelElement>
  ) => {
    // Merge the local props and ref with the ones provided via context.
    [props, ref] = useContextProps(
      props,
      ref,
      LabelContext
    );
    // ... your existing Label component
    return <label {...props} ref={ref} />;
  }
);
import type {LabelProps} from 'react-aria-components';
import {
  LabelContext,
  useContextProps
} from 'react-aria-components';

const MyCustomLabel =
  React.forwardRef(
    (
      props: LabelProps,
      ref:
        React.ForwardedRef<
          HTMLLabelElement
        >
    ) => {
      // Merge the local props and ref with the ones provided via context.
      [props, ref] =
        useContextProps(
          props,
          ref,
          LabelContext
        );
      // ... your existing Label component
      return (
        <label
          {...props}
          ref={ref}
        />
      );
    }
  );

Now you can use MyCustomLabel within a CheckboxGroup, in place of the builtin React Aria Components Label.

<CheckboxGroup>
  <MyCustomLabel>Favorite sports</MyCustomLabel>  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</CheckboxGroup>
<CheckboxGroup>
  <MyCustomLabel>Favorite sports</MyCustomLabel>  <MyCheckbox value="soccer">Soccer</MyCheckbox>
  <MyCheckbox value="baseball">Baseball</MyCheckbox>
  <MyCheckbox value="basketball">Basketball</MyCheckbox>
</CheckboxGroup>
<CheckboxGroup>
  <MyCustomLabel>
    Favorite sports
  </MyCustomLabel>  <MyCheckbox value="soccer">
    Soccer
  </MyCheckbox>
  <MyCheckbox value="baseball">
    Baseball
  </MyCheckbox>
  <MyCheckbox value="basketball">
    Basketball
  </MyCheckbox>
</CheckboxGroup>

State#

CheckboxGroup provides a CheckboxGroupState object to its children via CheckboxGroupStateContext. This can be used to access and manipulate the checkbox group's state.

This example shows a SelectionCount component that can be placed within a CheckboxGroup to display the number of selected items.

import {CheckboxGroupStateContext} from 'react-aria-components';

function SelectionCount() {
  let state = React.useContext(CheckboxGroupStateContext)!;  return <small>{state.value.length} items selected.</small>;
}

<MyCheckboxGroup label="Sandwich condiments">
  <MyCheckbox value="lettuce">Lettuce</MyCheckbox>
  <MyCheckbox value="tomato">Tomato</MyCheckbox>
  <MyCheckbox value="onion">Onion</MyCheckbox>
  <MyCheckbox value="sprouts">Sprouts</MyCheckbox>
  <SelectionCount />
</MyCheckboxGroup>
import {CheckboxGroupStateContext} from 'react-aria-components';

function SelectionCount() {
  let state = React.useContext(CheckboxGroupStateContext)!;  return <small>{state.value.length} items selected.
  </small>;
}

<MyCheckboxGroup label="Sandwich condiments">
  <MyCheckbox value="lettuce">Lettuce</MyCheckbox>
  <MyCheckbox value="tomato">Tomato</MyCheckbox>
  <MyCheckbox value="onion">Onion</MyCheckbox>
  <MyCheckbox value="sprouts">Sprouts</MyCheckbox>
  <SelectionCount />
</MyCheckboxGroup>
import {CheckboxGroupStateContext} from 'react-aria-components';

function SelectionCount() {
  let state = React
    .useContext(
      CheckboxGroupStateContext
    )!;  return (
    <small>
      {state.value
        .length}{' '}
      items selected.
    </small>
  );
}

<MyCheckboxGroup label="Sandwich condiments">
  <MyCheckbox value="lettuce">
    Lettuce
  </MyCheckbox>
  <MyCheckbox value="tomato">
    Tomato
  </MyCheckbox>
  <MyCheckbox value="onion">
    Onion
  </MyCheckbox>
  <MyCheckbox value="sprouts">
    Sprouts
  </MyCheckbox>
  <SelectionCount />
</MyCheckboxGroup>

Hooks#

If you need to customize things further, such as accessing internal state or customizing DOM structure, you can drop down to the lower level Hook-based API. See useCheckboxGroup for more details.