alpha

DisclosureGroup

A DisclosureGroup is a grouping of related disclosures, sometimes called an Accordion. It supports both single and multiple expanded items.

installyarn add react-aria-components
version1.4.0
usageimport {DisclosureGroup} from 'react-aria-components'

Example#


import {Button, UNSTABLE_Disclosure as Disclosure, UNSTABLE_DisclosureGroup as DisclosureGroup, UNSTABLE_DisclosurePanel as DisclosurePanel} from 'react-aria-components';

<DisclosureGroup defaultExpandedKeys={['personal']}>
  <Disclosure id="personal">
    <h3>
      <Button slot="trigger">
        <svg viewBox="0 0 24 24">
          <path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
        </svg>
        Personal Information
      </Button>
    </h3>
    <DisclosurePanel>
      <p>Personal information form here.</p>
    </DisclosurePanel>
  </Disclosure>
  <Disclosure id="billing">
    <h3>
      <Button slot="trigger">
        <svg viewBox="0 0 24 24">
          <path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
        </svg>
        Billing Address
      </Button>
    </h3>
    <DisclosurePanel>
      <p>Billing address form here.</p>
    </DisclosurePanel>
  </Disclosure>
</DisclosureGroup>
import {
  Button,
  UNSTABLE_Disclosure as Disclosure,
  UNSTABLE_DisclosureGroup as DisclosureGroup,
  UNSTABLE_DisclosurePanel as DisclosurePanel
} from 'react-aria-components';

<DisclosureGroup defaultExpandedKeys={['personal']}>
  <Disclosure id="personal">
    <h3>
      <Button slot="trigger">
        <svg viewBox="0 0 24 24">
          <path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
        </svg>
        Personal Information
      </Button>
    </h3>
    <DisclosurePanel>
      <p>Personal information form here.</p>
    </DisclosurePanel>
  </Disclosure>
  <Disclosure id="billing">
    <h3>
      <Button slot="trigger">
        <svg viewBox="0 0 24 24">
          <path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
        </svg>
        Billing Address
      </Button>
    </h3>
    <DisclosurePanel>
      <p>Billing address form here.</p>
    </DisclosurePanel>
  </Disclosure>
</DisclosureGroup>
import {
  Button,
  UNSTABLE_Disclosure
    as Disclosure,
  UNSTABLE_DisclosureGroup
    as DisclosureGroup,
  UNSTABLE_DisclosurePanel
    as DisclosurePanel
} from 'react-aria-components';

<DisclosureGroup
  defaultExpandedKeys={[
    'personal'
  ]}
>
  <Disclosure id="personal">
    <h3>
      <Button slot="trigger">
        <svg viewBox="0 0 24 24">
          <path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
        </svg>
        Personal
        Information
      </Button>
    </h3>
    <DisclosurePanel>
      <p>
        Personal
        information
        form here.
      </p>
    </DisclosurePanel>
  </Disclosure>
  <Disclosure id="billing">
    <h3>
      <Button slot="trigger">
        <svg viewBox="0 0 24 24">
          <path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
        </svg>
        Billing Address
      </Button>
    </h3>
    <DisclosurePanel>
      <p>
        Billing address
        form here.
      </p>
    </DisclosurePanel>
  </Disclosure>
</DisclosureGroup>

Props#


DisclosureGroup#

NameTypeDescription
allowsMultipleExpandedbooleanWhether multiple items can be expanded at the same time.
isDisabledbooleanWhether all items are disabled.
expandedKeysIterable<Key>The currently expanded keys in the collection (controlled).
defaultExpandedKeysIterable<Key>The initial expanded keys in the collection (uncontrolled).
childrenReactNode( (values: DisclosureGroupRenderProps{
defaultChildren: ReactNodeundefined
} )) => ReactNode
The children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: DisclosureGroupRenderProps{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: DisclosureGroupRenderProps{
defaultStyle: CSSProperties
} )) => CSSPropertiesundefined
The inline style for the element. A function may be provided to compute the style based on component state.
Events
NameTypeDescription
onExpandedChange( (keys: Set<Key> )) => anyHandler that is called when items are expanded or collapsed.
Accessibility
NameTypeDescription
idstringThe element's unique identifier. See MDN.

Disclosure#

Within a <DisclosureGroup>, most <Disclosure> props are set automatically. The id prop is required to identify the disclosure within the group.

Show props
NameTypeDescription
childrenReactNode( (values: DisclosureRenderProps{
defaultChildren: ReactNodeundefined
} )) => ReactNode
The children of the component. A function may be provided to alter the children based on component state.
idKeyAn id for the disclosure when used within a DisclosureGroup, matching the id used in expandedKeys.
isDisabledbooleanWhether the disclosure is disabled.
isExpandedbooleanWhether the disclosure is expanded (controlled).
defaultExpandedbooleanWhether the disclosure is expanded by default (uncontrolled).
classNamestring( (values: DisclosureRenderProps{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: DisclosureRenderProps{
defaultStyle: CSSProperties
} )) => CSSPropertiesundefined
The inline style for the element. A function may be provided to compute the style based on component state.
Events
NameTypeDescription
onExpandedChange( (isExpanded: boolean )) => voidHandler that is called when the disclosure's expanded state changes.
Layout
NameTypeDescription
slotstringnull

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

Button#

Show props
NameTypeDefaultDescription
formstring

The <form> element to associate the button with. The value of this attribute must be the id of a <form> in the same document.

formActionstring

The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner.

formEncTypestringIndicates how to encode the form data that is submitted.
formMethodstringIndicates the HTTP method used to submit the form.
formNoValidatebooleanIndicates that the form is not to be validated when it is submitted.
formTargetstringOverrides the target attribute of the button's form owner.
namestringSubmitted as a pair with the button's value as part of the form data.
valuestringThe value associated with the button's name when it's submitted with the form data.
isPendingboolean

Whether the button is in a pending state. This disables press and hover events while retaining focusability, and announces the pending state to screen readers.

isDisabledbooleanWhether the button is disabled.
autoFocusbooleanWhether the element should receive focus on render.
type'button''submit''reset''button'The behavior of the button when used in an HTML form.
preventFocusOnPressboolean

Whether to prevent focus from moving to the button when pressing it.

Caution, this can make the button inaccessible and should only be used when alternative keyboard interaction is provided, such as ComboBox's MenuTrigger or a NumberField's increment/decrement control.

childrenReactNode( (values: ButtonRenderProps{
defaultChildren: ReactNodeundefined
} )) => ReactNode
The children of the component. A function may be provided to alter the children based on component state.
classNamestring( (values: ButtonRenderProps{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: ButtonRenderProps{
defaultStyle: CSSProperties
} )) => CSSPropertiesundefined
The inline style for the element. A function may be provided to compute the style based on component state.
Events
NameTypeDescription
onPress( (e: PressEvent )) => voidHandler that is called when the press is released over the target.
onPressStart( (e: PressEvent )) => voidHandler that is called when a press interaction starts.
onPressEnd( (e: PressEvent )) => void

Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target.

onPressChange( (isPressed: boolean )) => voidHandler that is called when the press state changes.
onPressUp( (e: PressEvent )) => void

Handler that is called when a press is released over the target, regardless of whether it started on the target or not.

onFocus( (e: FocusEvent<Target> )) => voidHandler that is called when the element receives focus.
onBlur( (e: FocusEvent<Target> )) => voidHandler that is called when the element loses focus.
onFocusChange( (isFocused: boolean )) => voidHandler that is called when the element's focus status changes.
onKeyDown( (e: KeyboardEvent )) => voidHandler that is called when a key is pressed.
onKeyUp( (e: KeyboardEvent )) => voidHandler that is called when a key is released.
onHoverStart( (e: HoverEvent )) => voidHandler that is called when a hover interaction starts.
onHoverEnd( (e: HoverEvent )) => voidHandler that is called when a hover interaction ends.
onHoverChange( (isHovering: boolean )) => voidHandler that is called when the hover state changes.
Layout
NameTypeDescription
slotstringnull

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

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

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

aria-expandedboolean'true''false'Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed.
aria-haspopupboolean'menu''listbox''tree''grid''dialog''true''false'Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element.
aria-controlsstringIdentifies the element (or elements) whose contents or presence are controlled by the current element.
aria-pressedboolean'true''false''mixed'Indicates the current "pressed" state of toggle buttons.
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.

DisclosurePanel#

Show props
NameTypeDescription
childrenReactNodeThe children of the component.
classNamestring( (values: {}{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: {}{
defaultStyle: CSSProperties
} )) => CSSPropertiesundefined
The inline style for the element. A function may be provided to compute the style based on component state.
Accessibility
NameTypeDefaultDescription
role'group''region''group'The accessibility role for the disclosure's panel.

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-DisclosureGroup {
  /* ... */
}
.react-aria-DisclosureGroup {
  /* ... */
}
.react-aria-DisclosureGroup {
  /* ... */
}

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

<DisclosureGroup className="my-accordion">
  {/* ... */}
</DisclosureGroup>
<DisclosureGroup className="my-accordion">
  {/* ... */}
</DisclosureGroup>
<DisclosureGroup className="my-accordion">
  {/* ... */}
</DisclosureGroup>

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-DisclosureGroup[data-disabled] {
  /* ... */
}
.react-aria-DisclosureGroup[data-disabled] {
  /* ... */
}
.react-aria-DisclosureGroup[data-disabled] {
  /* ... */
}

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.

<Disclosure
  className={({ isExpanded }) =>
    isExpanded ? 'border-blue-500' : 'border-gray-600'}
/>
<Disclosure
  className={({ isExpanded }) =>
    isExpanded ? 'border-blue-500' : 'border-gray-600'}
/>
<Disclosure
  className={(
    { isExpanded }
  ) =>
    isExpanded
      ? 'border-blue-500'
      : 'border-gray-600'}
/>

The states, selectors, and render props for each component used in a DisclosureGroup are documented below.

DisclosureGroup#

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

NameCSS SelectorDescription
isDisabled[data-disabled]Whether the disclosure group is disabled.
stateState of the disclosure group.

Disclosure#

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

NameCSS SelectorDescription
isExpanded[data-expanded]Whether the disclosure is expanded.
isFocusVisibleWithin[data-focus-visible-within]Whether the disclosure has keyboard focus.
isDisabled[data-disabled]Whether the disclosure is disabled.
stateState of the disclosure.

Button#

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

NameCSS SelectorDescription
isHovered[data-hovered]Whether the button is currently hovered with a mouse.
isPressed[data-pressed]Whether the button is currently in a pressed state.
isFocused[data-focused]Whether the button is focused, either via a mouse or keyboard.
isFocusVisible[data-focus-visible]Whether the button is keyboard focused.
isDisabled[data-disabled]Whether the button is disabled.
isPending[data-pending]Whether the button is currently in a pending state.

DisclosurePanel#

A DisclosurePanel can be targeted with the .react-aria-DisclosurePanel CSS selector, or by overriding with a custom className.