{/* Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License. */}

import {Layout} from '@react-spectrum/docs';
export default Layout;

import docs from 'docs:react-aria-components';
import statelyDocs from 'docs:@react-stately/disclosure';
import {PropTable, HeaderInfo, TypeLink, PageDescription, StateTable, ContextTable} from '@react-spectrum/docs';
import styles from '@react-spectrum/docs/src/docs.css';
import packageData from 'react-aria-components/package.json';
import ChevronRight from '@spectrum-icons/workflow/ChevronRight';
import {Keyboard} from '@react-spectrum/text';
import {ExampleCard} from '@react-spectrum/docs/src/ExampleCard';
import Disclosure from '@react-spectrum/docs/pages/assets/component-illustrations/Disclosure.svg'
import Anatomy from './DisclosureGroupAnatomy.svg';

---
category: Navigation
keywords: [disclosure, accordion, collapse, expand, aria]
type: component
---

# DisclosureGroup

<PageDescription>{docs.exports.DisclosureGroup.description}</PageDescription>

<HeaderInfo
  packageData={packageData}
  componentNames={['DisclosureGroup']}
  sourceData={[
    {type: 'W3C', url: 'https://www.w3.org/WAI/ARIA/apg/patterns/accordion/examples/accordion/'}
  ]} />

## Example

```tsx example
import {DisclosureGroup, Disclosure, Button, DisclosurePanel, Heading} from 'react-aria-components';
import {ChevronRight} from 'lucide-react';

<DisclosureGroup defaultExpandedKeys={['personal']}>
  <Disclosure id="personal">
    <Heading>
      <Button slot="trigger">
        <ChevronRight size={18} />
        Personal Information
      </Button>
    </Heading>
    <DisclosurePanel>
      Personal information form here.
    </DisclosurePanel>
  </Disclosure>
  <Disclosure id="billing">
    <Heading>
      <Button slot="trigger">
        <ChevronRight size={18} />
        Billing Address
      </Button>
    </Heading>
    <DisclosurePanel>
      Billing address form here.
    </DisclosurePanel>
  </Disclosure>
</DisclosureGroup>
```

```css hidden
@import "@react-aria/example-theme";
@import './Button.mdx' layer(button);
@import './Disclosure.mdx' layer(disclosure);
```

## Features

Disclosure groups can be built by combining multiple disclosures built in HTML with the [&lt;details&gt;](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details) and [&lt;summary&gt;](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/summary) HTML elements, but this can be difficult to style, especially when adding adjacent interactive elements, like buttons, to the disclosure's heading. `DisclosureGroup` helps achieve accessible disclosures implemented with the correct ARIA pattern while making custom styling easy.

* **Accessible** - Disclosure group is exposed to assistive technology via ARIA. Uses hidden="until-found" in supported browsers, enabling find-in-page search support and improved search engine visibility for collapsed content.
* **Styleable** - Disclosures include builtin states for styling such as keyboard focus, disabled, and expanded states.

## Anatomy

<Anatomy />

A disclosure group consists of a set of disclosures. Each disclosure includes a button within a heading and panel of content that is either shown or hidden. Zero or more disclosures within a group can be expanded at the same time, however, by default, only one disclosure can be expanded at a time. Users may click or touch a disclosure to expand it, or use the <Keyboard>Tab</Keyboard> key to navigate between disclosures and the <Keyboard>Enter</Keyboard> or <Keyboard>Space</Keyboard> key to toggle it.

```tsx
import {DisclosureGroup, Disclosure, Button, DisclosurePanel, Heading} from 'react-aria-components';

<DisclosureGroup>
  <Disclosure>
    <Heading>
      <Button />
    </Heading>
    <DisclosurePanel />
  </Disclosure>
</DisclosureGroup>
```

### Composed Components

<section className={styles.cardGroup} data-size="small">

<ExampleCard
  url="Disclosure.html"
  title="Disclosure"
  description="A disclosure is a collapsible section of content.">
  <Disclosure />
</ExampleCard>

</section>

## Expanded

### Default expanded

An uncontrolled DisclosureGroup can be expanded by default using the `defaultExpandedKeys` prop. The `defaultExpandedKeys` must match the `id` on the disclosure(s) component that you wish to expand.

```tsx example export=true
import type {DisclosureProps} from 'react-aria-components';

interface MyDisclosureProps extends Omit<DisclosureProps, 'children'> {
  title?: string,
  children?: React.ReactNode
}

function MyDisclosure({title, children, ...props}: MyDisclosureProps) {
  return (
    <Disclosure {...props}>
      <Heading>
        <Button slot="trigger">
          <ChevronRight size={20} />
          {title}
        </Button>
      </Heading>
      <DisclosurePanel>
        {children}
      </DisclosurePanel>
    </Disclosure>
  )
}

<DisclosureGroup defaultExpandedKeys={["system"]}>
  <MyDisclosure id="system" title="System Requirements" >
    Details about system requirements here
  </MyDisclosure>
  <MyDisclosure id="personal" title="Personal Information" >
    Details about personal information here
  </MyDisclosure>
</DisclosureGroup>

```

### Controlled expanded

A controlled DisclosureGroup can be expanded and collapsed using `expandedKeys` and `onExpandedChange`. The `expandedKeys` must match the `id` on the disclosure component(s) that you wish to expand.

```tsx example
import {Key} from "@react-types/shared";

function ControlledExpanded(){
  let [expandedKeys, setExpandedKeys] = React.useState<Set<Key>>(new Set(["system"]));

  return (
    <>
    <DisclosureGroup expandedKeys={expandedKeys} onExpandedChange={setExpandedKeys}>
      <MyDisclosure id="system" title="System Requirements" >
        Details about system requirements here
      </MyDisclosure>
      <MyDisclosure id="personal" title="Personal Information" >
        Details about personal information here
      </MyDisclosure>
    </DisclosureGroup>
    <div style={{ marginTop: '20px' }}>You have expanded: {expandedKeys}</div>
    </>
  )
}
```
### Multiple expanded

By default, only one disclosure will be open at a time. To allow multiple disclosures to expand, use the `allowsMultipleExpanded` prop.

```tsx example
<DisclosureGroup defaultExpandedKeys={["system", "personal"]} allowsMultipleExpanded>
  <MyDisclosure id="system" title="System Requirements" >
    Details about system requirements here
  </MyDisclosure>
  <MyDisclosure id="personal" title="Personal Information" >
    Details about personal information here
  </MyDisclosure>
</DisclosureGroup>
```

## Disabled

A DisclosureGroup can be disabled using the `isDisabled` prop.

```tsx example
<DisclosureGroup isDisabled>
  <MyDisclosure id="system" title="System Requirements" >
    Details about system requirements here
  </MyDisclosure>
  <MyDisclosure id="personal" title="Personal Information" >
    Details about personal information here"
  </MyDisclosure>
</DisclosureGroup>
```

## Interactive elements

In some use cases, you may want to add an interactive element, like a button, adjacent to the disclosure's heading. Please ensure that these elements are siblings of the Heading element and not children.

```tsx example
<DisclosureGroup>
  <Disclosure id="system">
    <div style={{display: 'flex', alignItems: 'center'}}>
      <Heading>
        <Button slot="trigger">
          <ChevronRight size={18} />
          System Requirements
        </Button>
      </Heading>
      <Button>Click me</Button>
    </div>
    <DisclosurePanel>
      Details about system requirements here.
    </DisclosurePanel>
  </Disclosure>
  <Disclosure id="personal">
    <div style={{display: 'flex', alignItems: 'center'}}>
      <Heading>
        <Button slot="trigger">
          <ChevronRight size={18} />
          Personal Information
        </Button>
      </Heading>
      <Button>Click me</Button>
    </div>
    <DisclosurePanel>
      Details about personal information here.
    </DisclosurePanel>
  </Disclosure>
</DisclosureGroup>
```

## Props

### DisclosureGroup

<PropTable component={docs.exports.DisclosureGroup} links={docs.links} />

### Disclosure

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

<details>
  <summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show props</summary>

<PropTable component={docs.exports.Disclosure} links={docs.links} />

</details>

### Button

<details>
  <summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show props</summary>

<PropTable component={docs.exports.Button} links={docs.links} />

</details>

### DisclosurePanel

<details>
  <summary style={{fontWeight: 'bold'}}><ChevronRight size="S" /> Show props</summary>

<PropTable component={docs.exports.DisclosurePanel} links={docs.links} />

</details>

## 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.

```css
.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.

```jsx
<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:

```css
.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](https://tailwindcss.com/).

```jsx
<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:

<StateTable properties={docs.exports.DisclosureGroupRenderProps.properties} />

### 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:

<StateTable properties={docs.exports.DisclosureRenderProps.properties} />

Use the `--disclosure-panel-width` and `--disclosure-panel-height` CSS variables to implement animations.

### 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:

<StateTable properties={docs.exports.ButtonRenderProps.properties} />

### DisclosurePanel

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

<StateTable properties={docs.exports.DisclosurePanelRenderProps.properties} />

## Advanced Customization

### State

DisclosureGroup provides a <TypeLink links={statelyDocs.links} type={statelyDocs.exports.DisclosureGroupState} /> object to its children via `DisclosureGroupStateContext`. This can be used to access and manipulate the disclosure group's state.

### Hook

If you need to customize things even further, such as accessing internal state, you can drop down to the lower level Hook-based API. See [useDisclosureGroupState](useDisclosure.html#disclosure-group) for more details.
