alpha

CheckboxGroup

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

installyarn add react-aria-components
version1.0.0-alpha.6
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-static-red-600);
  --invalid-color-pressed: var(--spectrum-global-color-static-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-validation-state=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-static-red-600);
  --invalid-color-pressed: var(--spectrum-global-color-static-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-validation-state=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-static-red-600);
  --invalid-color-pressed: var(--spectrum-global-color-static-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-validation-state=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.

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.

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.
validationStateValidationStateWhether the input should display its "valid" or "invalid" visual styling.
isRequiredboolean

Whether user input is required on the input before form submission. Often paired with the necessityIndicator prop to add a visual indicator to the input.

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
NameTypeDefaultDescription
onChange( (value: T )) => voidHandler that is called when the value changes.
Layout
NameTypeDefaultDescription
slotstringA slot name for the component. Slots allow the component to receive props from a parent component.
Accessibility
NameTypeDefaultDescription
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
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.
validationStateValidationStateWhether the input should display its "valid" or "invalid" visual styling.
isRequiredboolean

Whether user input is required on the input before form submission. Often paired with the necessityIndicator prop to add a visual indicator to the input.

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
NameTypeDefaultDescription
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
NameTypeDefaultDescription
slotstringA slot name for the component. Slots allow the component to receive props from a parent component.
Accessibility
NameTypeDefaultDescription
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 DOM attributes, which you can target in CSS selectors. These are ARIA attributes wherever possible, or data attributes when a relevant ARIA attribute does not exist. 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[aria-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.
validationState[data-validation-state="invalid" | "valid"]The validation state of the checkbox group.
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.
validationState[data-validation-state="valid | invalid"]Whether the checkbox is valid or 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.

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>

Usage#


The following examples show how to use the MyCheckboxGroup and MyCheckbox components created in the above example.

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>
  );
}

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 pass "invalid" to the validationState 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 validationState="invalid" to the CheckboxGroup.

The following example shows how to require that at least one option is selected. It sets the validationState prop to "invalid" 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
      validationState={selected.length === 0 ? 'invalid' : undefined}
    >
      <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
      validationState={selected.length === 0
        ? 'invalid'
        : undefined}
    >
      <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
      validationState={selected
          .length === 0
        ? 'invalid'
        : undefined}
    >
      <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 validationState="invalid" 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, validationState="invalid" 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
        validationState={!selected.includes('terms') ? 'invalid' : undefined}
      >
        Terms and conditions
      </MyCheckbox>
      <MyCheckbox
        value="privacy"
        isRequired
        validationState={!selected.includes('privacy') ? 'invalid' : undefined}
      >
        Privacy policy
      </MyCheckbox>
      <MyCheckbox
        value="cookies"
        isRequired
        validationState={!selected.includes('cookies') ? 'invalid' : undefined}
      >
        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
        validationState={!selected.includes('terms')
          ? 'invalid'
          : undefined}
      >
        Terms and conditions
      </MyCheckbox>
      <MyCheckbox
        value="privacy"
        isRequired
        validationState={!selected.includes('privacy')
          ? 'invalid'
          : undefined}
      >
        Privacy policy
      </MyCheckbox>
      <MyCheckbox
        value="cookies"
        isRequired
        validationState={!selected.includes('cookies')
          ? 'invalid'
          : undefined}
      >
        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
        validationState={!selected
            .includes(
              'terms'
            )
          ? 'invalid'
          : undefined}
      >
        Terms and
        conditions
      </MyCheckbox>
      <MyCheckbox
        value="privacy"
        isRequired
        validationState={!selected
            .includes(
              'privacy'
            )
          ? 'invalid'
          : undefined}
      >
        Privacy policy
      </MyCheckbox>
      <MyCheckbox
        value="cookies"
        isRequired
        validationState={!selected
            .includes(
              'cookies'
            )
          ? 'invalid'
          : undefined}
      >
        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 validationState 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 (
    <MyCheckboxGroup
      label="Pets"
      onChange={setChecked}
      value={checked}
      validationState={isValid ? 'valid' : 'invalid'}
      description={isValid ? 'Select your pets.' : undefined}
      errorMessage={isValid
        ? undefined
        : checked.includes('cats')
        ? 'No cats allowed.'
        : 'Select only dogs and dragons.'}
    >
      <MyCheckbox value="dogs">Dogs</MyCheckbox>
      <MyCheckbox value="cats">Cats</MyCheckbox>
      <MyCheckbox value="dragons">Dragons</MyCheckbox>
    </MyCheckboxGroup>
  );
}
function Example() {
  let [checked, setChecked] = React.useState([
    'dogs',
    'dragons'
  ]);
  let isValid = checked.length === 2 &&
    checked.includes('dogs') && checked.includes('dragons');

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

  return (
    <MyCheckboxGroup
      label="Pets"
      onChange={setChecked}
      value={checked}
      validationState={isValid
        ? 'valid'
        : 'invalid'}
      description={isValid
        ? 'Select your pets.'
        : undefined}
      errorMessage={isValid
        ? undefined
        : checked
            .includes(
              'cats'
            )
        ? 'No cats allowed.'
        : 'Select only dogs and dragons.'}
    >
      <MyCheckbox value="dogs">
        Dogs
      </MyCheckbox>
      <MyCheckbox value="cats">
        Cats
      </MyCheckbox>
      <MyCheckbox value="dragons">
        Dragons
      </MyCheckbox>
    </MyCheckboxGroup>
  );
}

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>

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>

Advanced customization#


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.