alpha

Disclosure

A collapsible section of content composed of a heading that expands and collapses a panel.

installyarn add @react-spectrum/accordion
version3.0.0-alpha.35
usageimport {Disclosure, DisclosureHeader, DisclosurePanel} from '@react-spectrum/accordion'

Example#


<Disclosure>
  <DisclosureHeader>System Requirements</DisclosureHeader>
  <DisclosurePanel>
    <p>Details about system requirements here.</p>
  </DisclosurePanel>
</Disclosure>
<Disclosure>
  <DisclosureHeader>System Requirements</DisclosureHeader>
  <DisclosurePanel>
    <p>Details about system requirements here.</p>
  </DisclosurePanel>
</Disclosure>
<Disclosure>
  <DisclosureHeader>
    System Requirements
  </DisclosureHeader>
  <DisclosurePanel>
    <p>
      Details about
      system
      requirements
      here.
    </p>
  </DisclosurePanel>
</Disclosure>

Content#


Disclosure follows the ARIA Disclosure pattern and consists of two children: DisclosureHeader and DisclosurePanel. The DisclosureHeader controls the expansion of the DisclosurePanel and the contents inside.

Disclosure can be used as a standalone collapsible section of content. However, multiple discosures can be combined to form an Accordion.

Internationalization#

In order to internationalize a disclosure, all text content within the disclosure should be localized.

Events#


Disclosure accepts an onExpandedChange prop which is triggered when it is expanded or collapsed. The example below uses onExpandedChange to programmatically control disclosure expansion.

function ControlledExpansion() {
  let [isExpanded, setIsExpanded] = React.useState<boolean>(false);

  return (
    <>
      <Disclosure isExpanded={isExpanded} onExpandedChange={setIsExpanded}>
        <DisclosureHeader>System Requirements</DisclosureHeader>
        <DisclosurePanel>
          <p>Details about system requirements here.</p>
        </DisclosurePanel>
      </Disclosure>
      <div style={{ marginTop: '20px' }}>
        {isExpanded
          ? 'The disclosure is expanded'
          : 'The disclosure is collapsed'}
      </div>
    </>
  );
}
function ControlledExpansion() {
  let [isExpanded, setIsExpanded] = React.useState<boolean>(
    false
  );

  return (
    <>
      <Disclosure
        isExpanded={isExpanded}
        onExpandedChange={setIsExpanded}
      >
        <DisclosureHeader>
          System Requirements
        </DisclosureHeader>
        <DisclosurePanel>
          <p>Details about system requirements here.</p>
        </DisclosurePanel>
      </Disclosure>
      <div style={{ marginTop: '20px' }}>
        {isExpanded
          ? 'The disclosure is expanded'
          : 'The disclosure is collapsed'}
      </div>
    </>
  );
}
function ControlledExpansion() {
  let [
    isExpanded,
    setIsExpanded
  ] = React.useState<
    boolean
  >(false);

  return (
    <>
      <Disclosure
        isExpanded={isExpanded}
        onExpandedChange={setIsExpanded}
      >
        <DisclosureHeader>
          System
          Requirements
        </DisclosureHeader>
        <DisclosurePanel>
          <p>
            Details about
            system
            requirements
            here.
          </p>
        </DisclosurePanel>
      </Disclosure>
      <div
        style={{
          marginTop:
            '20px'
        }}
      >
        {isExpanded
          ? 'The disclosure is expanded'
          : 'The disclosure is collapsed'}
      </div>
    </>
  );
}

The disclosure is collapsed

Props#


Disclosure#

NameTypeDescription
childrenReact.ReactNodeThe contents of the disclosure. The first child should be the header, and the second child should be the panel.
isQuietbooleanWhether the Disclosure should be displayed with a quiet style.
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).
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.

Accessibility
NameTypeDescription
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.

DisclosureHeader#

NameTypeDefaultDescription
childrenReact.ReactNodeThe contents of the disclosure header.
levelnumber3The heading level of the disclosure header.
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.

DisclosurePanel#

NameTypeDescription
childrenReact.ReactNodeThe contents of the accordion panel.
Accessibility
NameTypeDefaultDescription
role'group''region''group'The accessibility role for the disclosure's panel.
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.

Visual Options#


Disabled#

<Disclosure isDisabled>
  <DisclosureHeader>System Requirements</DisclosureHeader>
  <DisclosurePanel>
    <p>Details about system requirements here.</p>
  </DisclosurePanel>
</Disclosure>
<Disclosure isDisabled>
  <DisclosureHeader>System Requirements</DisclosureHeader>
  <DisclosurePanel>
    <p>Details about system requirements here.</p>
  </DisclosurePanel>
</Disclosure>
<Disclosure isDisabled>
  <DisclosureHeader>
    System Requirements
  </DisclosureHeader>
  <DisclosurePanel>
    <p>
      Details about
      system
      requirements
      here.
    </p>
  </DisclosurePanel>
</Disclosure>

Expanded#

<Disclosure defaultExpanded>
  <DisclosureHeader>System Requirements</DisclosureHeader>
  <DisclosurePanel>
    <p>Details about system requirements here.</p>
  </DisclosurePanel>
</Disclosure>
<Disclosure defaultExpanded>
  <DisclosureHeader>System Requirements</DisclosureHeader>
  <DisclosurePanel>
    <p>Details about system requirements here.</p>
  </DisclosurePanel>
</Disclosure>
<Disclosure
  defaultExpanded
>
  <DisclosureHeader>
    System Requirements
  </DisclosureHeader>
  <DisclosurePanel>
    <p>
      Details about
      system
      requirements
      here.
    </p>
  </DisclosurePanel>
</Disclosure>

Details about system requirements here.

Quiet#

<Disclosure isQuiet>
  <DisclosureHeader>System Requirements</DisclosureHeader>
  <DisclosurePanel>
    <p>Details about system requirements here.</p>
  </DisclosurePanel>
</Disclosure>
<Disclosure isQuiet>
  <DisclosureHeader>System Requirements</DisclosureHeader>
  <DisclosurePanel>
    <p>Details about system requirements here.</p>
  </DisclosurePanel>
</Disclosure>
<Disclosure isQuiet>
  <DisclosureHeader>
    System Requirements
  </DisclosureHeader>
  <DisclosurePanel>
    <p>
      Details about
      system
      requirements
      here.
    </p>
  </DisclosurePanel>
</Disclosure>