beta

CheckboxGroup

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

installyarn add react-aria-components
version1.0.0-beta.2
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
.react-aria-CheckboxGroup {
  display: flex;
  flex-direction: column;
  gap: 0.571rem;

  [slot=description] {
    font-size: 12px;
  }

  [slot=errorMessage] {
    font-size: 12px;
    color: var(--spectrum-global-color-red-600);
  }
}

.react-aria-Checkbox {
  --label-color: var(--spectrum-alias-text-color);
  --deselected-color: gray;
  --deselected-color-pressed: dimgray;
  --selected-color: slateblue;
  --selected-color-pressed: lch(from slateblue calc(l - 10%) c h);
  --checkmark-color: white;
  --invalid-color: var(--spectrum-global-color-red-600);
  --invalid-color-pressed: var(--spectrum-global-color-red-700);

  display: flex;
  align-items: center;
  gap: 0.571rem;
  font-size: 1.143rem;
  color: var(--label-color);

  .checkbox {
    width: 1.143rem;
    height: 1.143rem;
    border: 2px solid var(--deselected-color);
    border-radius: 4px;
    transition: all 200ms;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  & svg {
    width: 1rem;
    height: 1rem;
    fill: none;
    stroke: var(--checkmark-color);
    stroke-width: 3px;
    stroke-dasharray: 22px;
    stroke-dashoffset: 66;
    transition: all 200ms;
  }

  &[data-pressed] .checkbox {
    border-color: var(--deselected-color-pressed);
  }

  &[data-selected],
  &[data-indeterminate] {
    .checkbox {
      border-color: var(--selected-color);
      background: var(--selected-color);
    }

    &[data-pressed] .checkbox {
      border-color: var(--selected-color-pressed);
      background: var(--selected-color-pressed);
    }

    & svg {
      stroke-dashoffset: 44;
    }
  }

  &[data-invalid] {
    .checkbox {
      border-color: var(--invalid-color);
    }

    &[data-pressed] .checkbox {
      border-color: var(--invalid-color-pressed);
    }

    &[data-selected],
    &[data-indeterminate] {
      .checkbox {
        background: var(--invalid-color);
      }

      &[data-pressed] .checkbox {
        background: var(--invalid-color-pressed);
      }
    }
  }

  &[data-indeterminate] {
    & svg {
      stroke: none;
      fill: var(--checkmark-color);
    }
  }

  &[data-focus-visible] .checkbox {
    box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px var(--selected-color);
  }

  &[data-disabled] {
    opacity: 0.4;
  }
}

@media (forced-colors: active) {
  .react-aria-Checkbox {
    forced-color-adjust: none;

    --label-color: ButtonText;
    --deselected-color: ButtonBorder;
    --deselected-color-pressed: ButtonBorder;
    --selected-color: Highlight;
    --selected-color-pressed: Highlight;
    --checkmark-color: HighlightText;
    --invalid-color: LinkText;
    --invalid-color-pressed: LinkText;
    --spectrum-alias-background-color-default: Canvas;

    &[data-disabled] {
      opacity: 1;
      --deselected-color: GrayText;
      --selected-color: GrayText;
      --label-color: GrayText;
    }
  }
}
.react-aria-CheckboxGroup {
  display: flex;
  flex-direction: column;
  gap: 0.571rem;

  [slot=description] {
    font-size: 12px;
  }

  [slot=errorMessage] {
    font-size: 12px;
    color: var(--spectrum-global-color-red-600);
  }
}

.react-aria-Checkbox {
  --label-color: var(--spectrum-alias-text-color);
  --deselected-color: gray;
  --deselected-color-pressed: dimgray;
  --selected-color: slateblue;
  --selected-color-pressed: lch(from slateblue calc(l - 10%) c h);
  --checkmark-color: white;
  --invalid-color: var(--spectrum-global-color-red-600);
  --invalid-color-pressed: var(--spectrum-global-color-red-700);

  display: flex;
  align-items: center;
  gap: 0.571rem;
  font-size: 1.143rem;
  color: var(--label-color);

  .checkbox {
    width: 1.143rem;
    height: 1.143rem;
    border: 2px solid var(--deselected-color);
    border-radius: 4px;
    transition: all 200ms;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  & svg {
    width: 1rem;
    height: 1rem;
    fill: none;
    stroke: var(--checkmark-color);
    stroke-width: 3px;
    stroke-dasharray: 22px;
    stroke-dashoffset: 66;
    transition: all 200ms;
  }

  &[data-pressed] .checkbox {
    border-color: var(--deselected-color-pressed);
  }

  &[data-selected],
  &[data-indeterminate] {
    .checkbox {
      border-color: var(--selected-color);
      background: var(--selected-color);
    }

    &[data-pressed] .checkbox {
      border-color: var(--selected-color-pressed);
      background: var(--selected-color-pressed);
    }

    & svg {
      stroke-dashoffset: 44;
    }
  }

  &[data-invalid] {
    .checkbox {
      border-color: var(--invalid-color);
    }

    &[data-pressed] .checkbox {
      border-color: var(--invalid-color-pressed);
    }

    &[data-selected],
    &[data-indeterminate] {
      .checkbox {
        background: var(--invalid-color);
      }

      &[data-pressed] .checkbox {
        background: var(--invalid-color-pressed);
      }
    }
  }

  &[data-indeterminate] {
    & svg {
      stroke: none;
      fill: var(--checkmark-color);
    }
  }

  &[data-focus-visible] .checkbox {
    box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px var(--selected-color);
  }

  &[data-disabled] {
    opacity: 0.4;
  }
}

@media (forced-colors: active) {
  .react-aria-Checkbox {
    forced-color-adjust: none;

    --label-color: ButtonText;
    --deselected-color: ButtonBorder;
    --deselected-color-pressed: ButtonBorder;
    --selected-color: Highlight;
    --selected-color-pressed: Highlight;
    --checkmark-color: HighlightText;
    --invalid-color: LinkText;
    --invalid-color-pressed: LinkText;
    --spectrum-alias-background-color-default: Canvas;

    &[data-disabled] {
      opacity: 1;
      --deselected-color: GrayText;
      --selected-color: GrayText;
      --label-color: GrayText;
    }
  }
}
.react-aria-CheckboxGroup {
  display: flex;
  flex-direction: column;
  gap: 0.571rem;

  [slot=description] {
    font-size: 12px;
  }

  [slot=errorMessage] {
    font-size: 12px;
    color: var(--spectrum-global-color-red-600);
  }
}

.react-aria-Checkbox {
  --label-color: var(--spectrum-alias-text-color);
  --deselected-color: gray;
  --deselected-color-pressed: dimgray;
  --selected-color: slateblue;
  --selected-color-pressed: lch(from slateblue calc(l - 10%) c h);
  --checkmark-color: white;
  --invalid-color: var(--spectrum-global-color-red-600);
  --invalid-color-pressed: var(--spectrum-global-color-red-700);

  display: flex;
  align-items: center;
  gap: 0.571rem;
  font-size: 1.143rem;
  color: var(--label-color);

  .checkbox {
    width: 1.143rem;
    height: 1.143rem;
    border: 2px solid var(--deselected-color);
    border-radius: 4px;
    transition: all 200ms;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  & svg {
    width: 1rem;
    height: 1rem;
    fill: none;
    stroke: var(--checkmark-color);
    stroke-width: 3px;
    stroke-dasharray: 22px;
    stroke-dashoffset: 66;
    transition: all 200ms;
  }

  &[data-pressed] .checkbox {
    border-color: var(--deselected-color-pressed);
  }

  &[data-selected],
  &[data-indeterminate] {
    .checkbox {
      border-color: var(--selected-color);
      background: var(--selected-color);
    }

    &[data-pressed] .checkbox {
      border-color: var(--selected-color-pressed);
      background: var(--selected-color-pressed);
    }

    & svg {
      stroke-dashoffset: 44;
    }
  }

  &[data-invalid] {
    .checkbox {
      border-color: var(--invalid-color);
    }

    &[data-pressed] .checkbox {
      border-color: var(--invalid-color-pressed);
    }

    &[data-selected],
    &[data-indeterminate] {
      .checkbox {
        background: var(--invalid-color);
      }

      &[data-pressed] .checkbox {
        background: var(--invalid-color-pressed);
      }
    }
  }

  &[data-indeterminate] {
    & svg {
      stroke: none;
      fill: var(--checkmark-color);
    }
  }

  &[data-focus-visible] .checkbox {
    box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px var(--selected-color);
  }

  &[data-disabled] {
    opacity: 0.4;
  }
}

@media (forced-colors: active) {
  .react-aria-Checkbox {
    forced-color-adjust: none;

    --label-color: ButtonText;
    --deselected-color: ButtonBorder;
    --deselected-color-pressed: ButtonBorder;
    --selected-color: Highlight;
    --selected-color-pressed: Highlight;
    --checkmark-color: HighlightText;
    --invalid-color: LinkText;
    --invalid-color-pressed: LinkText;
    --spectrum-alias-background-color-default: Canvas;

    &[data-disabled] {
      opacity: 1;
      --deselected-color: GrayText;
      --selected-color: GrayText;
      --label-color: GrayText;
    }
  }
}

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.

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 {CheckboxGroup, Checkbox, Label, Text} from 'react-aria-components';

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

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

<CheckboxGroup>
  <Label />
  <Checkbox />
  <Text slot="description" />
  <Text slot="errorMessage" />
</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.

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.

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 and errorMessage slots to render help text (see below for details).

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

interface MyCheckboxGroupProps extends Omit<CheckboxGroupProps, 'children'> {
  children?: React.ReactNode,
  label?: string,
  description?: string,
  errorMessage?: string
}

function MyCheckboxGroup({
  label,
  description,
  errorMessage,
  children,
  ...props
}: MyCheckboxGroupProps) {
  return (
    <CheckboxGroup {...props}>
      {label}
      {children}
      {description && <Text slot="description">{description}</Text>}
      {errorMessage && <Text slot="errorMessage">{errorMessage}</Text>}
    </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
} from 'react-aria-components';
import {Text} from 'react-aria-components';

interface MyCheckboxGroupProps
  extends Omit<CheckboxGroupProps, 'children'> {
  children?: React.ReactNode;
  label?: string;
  description?: string;
  errorMessage?: string;
}

function MyCheckboxGroup({
  label,
  description,
  errorMessage,
  children,
  ...props
}: MyCheckboxGroupProps) {
  return (
    <CheckboxGroup {...props}>
      {label}
      {children}
      {description && (
        <Text slot="description">{description}</Text>
      )}
      {errorMessage && (
        <Text slot="errorMessage">{errorMessage}</Text>
      )}
    </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
} from 'react-aria-components';
import {Text} from 'react-aria-components';

interface MyCheckboxGroupProps
  extends
    Omit<
      CheckboxGroupProps,
      'children'
    > {
  children?:
    React.ReactNode;
  label?: string;
  description?: string;
  errorMessage?: string;
}

function MyCheckboxGroup(
  {
    label,
    description,
    errorMessage,
    children,
    ...props
  }: MyCheckboxGroupProps
) {
  return (
    <CheckboxGroup
      {...props}
    >
      {label}
      {children}
      {description && (
        <Text slot="description">
          {description}
        </Text>
      )}
      {errorMessage && (
        <Text slot="errorMessage">
          {errorMessage}
        </Text>
      )}
    </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#


Group validation#

CheckboxGroups can display a validation state to communicate to the user if the current value is invalid. Implement your own validation logic in your app and set the isInvalid prop to either the CheckboxGroup or an individual Checkbox to mark it as invalid.

If the group as a whole is invalid, for example if the user must choose at least one option but failed to do so, then pass isInvalid to the CheckboxGroup.

The following example shows how to require that at least one option is selected. It sets the isInvalid prop when no options are selected and removes it otherwise.

function Example() {
  let [selected, setSelected] = React.useState<string[]>([]);

  return (
    <MyCheckboxGroup
      label="Sandwich condiments"
      value={selected}
      onChange={setSelected}
      isRequired
      isInvalid={selected.length === 0}
    >
      <MyCheckbox value="lettuce">Lettuce</MyCheckbox>
      <MyCheckbox value="tomato">Tomato</MyCheckbox>
      <MyCheckbox value="onion">Onion</MyCheckbox>
      <MyCheckbox value="sprouts">Sprouts</MyCheckbox>
    </MyCheckboxGroup>
  );
}
function Example() {
  let [selected, setSelected] = React.useState<string[]>(
    []
  );

  return (
    <MyCheckboxGroup
      label="Sandwich condiments"
      value={selected}
      onChange={setSelected}
      isRequired
      isInvalid={selected.length === 0}
    >
      <MyCheckbox value="lettuce">Lettuce</MyCheckbox>
      <MyCheckbox value="tomato">Tomato</MyCheckbox>
      <MyCheckbox value="onion">Onion</MyCheckbox>
      <MyCheckbox value="sprouts">Sprouts</MyCheckbox>
    </MyCheckboxGroup>
  );
}
function Example() {
  let [
    selected,
    setSelected
  ] = React.useState<
    string[]
  >([]);

  return (
    <MyCheckboxGroup
      label="Sandwich condiments"
      value={selected}
      onChange={setSelected}
      isRequired
      isInvalid={selected
        .length === 0}
    >
      <MyCheckbox value="lettuce">
        Lettuce
      </MyCheckbox>
      <MyCheckbox value="tomato">
        Tomato
      </MyCheckbox>
      <MyCheckbox value="onion">
        Onion
      </MyCheckbox>
      <MyCheckbox value="sprouts">
        Sprouts
      </MyCheckbox>
    </MyCheckboxGroup>
  );
}

Individual Checkbox validation#

If an individual checkbox is invalid, for example if the user must select a particular option but failed to do so, then pass isInvalid to the Checkbox element instead.

The following example shows how to require that all items are selected. It uses the isRequired prop on each individual Checkbox element to indicate to assistive technology that every checkbox is required. By default, the isRequired prop on the CheckboxGroup only indicates that the group is required, not any individual option. In addition, isInvalid is set on each checkbox that is not yet checked.

function Example() {
  let [selected, setSelected] = React.useState<string[]>([]);

  return (
    <MyCheckboxGroup
      label="Agree to the following"
      isRequired
      value={selected}
      onChange={setSelected}
    >
      <MyCheckbox
        value="terms"
        isRequired
        isInvalid={!selected.includes('terms')}
      >
        Terms and conditions
      </MyCheckbox>
      <MyCheckbox
        value="privacy"
        isRequired
        isInvalid={!selected.includes('privacy')}
      >
        Privacy policy
      </MyCheckbox>
      <MyCheckbox
        value="cookies"
        isRequired
        isInvalid={!selected.includes('cookies')}
      >
        Cookie policy
      </MyCheckbox>
    </MyCheckboxGroup>
  );
}
function Example() {
  let [selected, setSelected] = React.useState<string[]>(
    []
  );

  return (
    <MyCheckboxGroup
      label="Agree to the following"
      isRequired
      value={selected}
      onChange={setSelected}
    >
      <MyCheckbox
        value="terms"
        isRequired
        isInvalid={!selected.includes('terms')}
      >
        Terms and conditions
      </MyCheckbox>
      <MyCheckbox
        value="privacy"
        isRequired
        isInvalid={!selected.includes('privacy')}
      >
        Privacy policy
      </MyCheckbox>
      <MyCheckbox
        value="cookies"
        isRequired
        isInvalid={!selected.includes('cookies')}
      >
        Cookie policy
      </MyCheckbox>
    </MyCheckboxGroup>
  );
}
function Example() {
  let [
    selected,
    setSelected
  ] = React.useState<
    string[]
  >([]);

  return (
    <MyCheckboxGroup
      label="Agree to the following"
      isRequired
      value={selected}
      onChange={setSelected}
    >
      <MyCheckbox
        value="terms"
        isRequired
        isInvalid={!selected
          .includes(
            'terms'
          )}
      >
        Terms and
        conditions
      </MyCheckbox>
      <MyCheckbox
        value="privacy"
        isRequired
        isInvalid={!selected
          .includes(
            'privacy'
          )}
      >
        Privacy policy
      </MyCheckbox>
      <MyCheckbox
        value="cookies"
        isRequired
        isInvalid={!selected
          .includes(
            'cookies'
          )}
      >
        Cookie policy
      </MyCheckbox>
    </MyCheckboxGroup>
  );
}

Help text#

The description slot can be used to associate additional help text with a checkbox group. Additionally, the errorMessage slot can be used to help the user fix a validation error. It should be combined with the isInvalid prop to semantically mark the checkbox group as invalid for assistive technologies.

function Example() {
  let [checked, setChecked] = React.useState(['dogs', 'dragons']);
  let isValid = checked.length === 2 && checked.includes('dogs') &&
    checked.includes('dragons');

  return (
    <CheckboxGroup
      onChange={setChecked}
      value={checked}
      isInvalid={!isValid}    >
      <Label>Pets</Label>
      <MyCheckbox value="dogs">Dogs</MyCheckbox>
      <MyCheckbox value="cats">Cats</MyCheckbox>
      <MyCheckbox value="dragons">Dragons</MyCheckbox>
      {isValid && <Text slot="description">Select your pets.</Text>}
      {!isValid && (
        <Text slot="errorMessage">
          {checked.includes('cats')
            ? 'No cats allowed.'
            : 'Select only dogs and dragons.'}
        </Text>
      )}    </CheckboxGroup>
  );
}
function Example() {
  let [checked, setChecked] = React.useState([
    'dogs',
    'dragons'
  ]);
  let isValid = checked.length === 2 &&
    checked.includes('dogs') && checked.includes('dragons');

  return (
    <CheckboxGroup
      onChange={setChecked}
      value={checked}
      isInvalid={!isValid}    >
      <Label>Pets</Label>
      <MyCheckbox value="dogs">Dogs</MyCheckbox>
      <MyCheckbox value="cats">Cats</MyCheckbox>
      <MyCheckbox value="dragons">Dragons</MyCheckbox>
      {isValid && (
        <Text slot="description">Select your pets.</Text>
      )}
      {!isValid && (
        <Text slot="errorMessage">
          {checked.includes('cats')
            ? 'No cats allowed.'
            : 'Select only dogs and dragons.'}
        </Text>
      )}    </CheckboxGroup>
  );
}
function Example() {
  let [
    checked,
    setChecked
  ] = React.useState([
    'dogs',
    'dragons'
  ]);
  let isValid =
    checked.length ===
      2 &&
    checked.includes(
      'dogs'
    ) &&
    checked.includes(
      'dragons'
    );

  return (
    <CheckboxGroup
      onChange={setChecked}
      value={checked}
      isInvalid={!isValid}    >
      <Label>Pets</Label>
      <MyCheckbox value="dogs">
        Dogs
      </MyCheckbox>
      <MyCheckbox value="cats">
        Cats
      </MyCheckbox>
      <MyCheckbox value="dragons">
        Dragons
      </MyCheckbox>
      {isValid && (
        <Text slot="description">
          Select your
          pets.
        </Text>
      )}
      {!isValid && (
        <Text slot="errorMessage">
          {checked
              .includes(
                'cats'
              )
            ? 'No cats allowed.'
            : 'Select only dogs and dragons.'}
        </Text>
      )}    </CheckboxGroup>
  );
}

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#

NameTypeDescription
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.
isRequiredbooleanWhether user input is required on the input before form submission.
isInvalidbooleanWhether the input value is invalid.
namestringThe name of the input element, used when submitting an HTML form. See MDN.
childrenReactNode( (values: CheckboxGroupRenderProps )) => ReactNodeThe children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: CheckboxGroupRenderProps )) => stringThe CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: CheckboxGroupRenderProps )) => CSSPropertiesThe 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.
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
NameTypeDescription
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.
autoFocusbooleanWhether the element should receive focus on render.
namestringThe name of the input element, used when submitting an HTML form. See MDN.
childrenReactNode( (values: CheckboxRenderProps )) => ReactNodeThe children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: CheckboxRenderProps )) => stringThe CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: CheckboxRenderProps )) => CSSPropertiesThe 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.
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.

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.

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
CheckboxCheckboxContextCheckboxPropsHTMLInputElement

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.