Disclosure

A disclosure shows and hides a section of the page. A button controls it, and the button tells screen readers whether the section is open.

Installation

Pick your package manager in a tab, and the other install command follows. The page remembers your choice.

Install the prerequisites

The components need React and React DOM:

npm install react react-dom

Install the library

Then install the package:

npm install @ariakit/react

Usage

Put the button and the content in a DisclosureProvider. The provider holds the open state, so both parts stay in sync without props between them.

import * as Ariakit from "@ariakit/react";

export function Details() {
  return (
    <Ariakit.DisclosureProvider>
      <Ariakit.Disclosure>Show details</Ariakit.Disclosure>
      <Ariakit.DisclosureContent className="disclosure-content">
        The order ships in two to three days.
      </Ariakit.DisclosureContent>
    </Ariakit.DisclosureProvider>
  );
}

The Disclosure on line 6 is a button. A click on it opens the content, and another click closes it again.

Animation

The content gets a data-enter attribute while it opens, so a CSS animation can run at that moment:

.disclosure-content[data-enter] {
  animation: fade-in 150ms ease-out;
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}