import SEO from "../components/SEO"; import { TOC, TOCList, TOCLink } from "../components/TOC"; import { AsPropWarning } from "../components/AsPropWarning"; import { Pipe } from "../components/Pipe"; # Disclosure Disclosure DisclosureButton DisclosurePanel useDisclosureContext - Source: https://github.com/reach/reach-ui/tree/main/packages/disclosure - WAI-ARIA: https://www.w3.org/TR/wai-aria-practices-1.2/#disclosure A disclosure is a button that controls the visibility of a panel of content. When the content inside the panel is hidden, it is often styled as a typical push button with a right-pointing arrow or triangle to hint that activating the button will display additional content. When the content is visible, the arrow or triangle typically points down. If you have a group of disclosures that stack vertically and exist within the same logical context, you may want to use [`@reach/accordion`](/accordion) instead. ## Installation From the command line in your project directory, run `npm install @reach/disclosure` or `yarn add @reach/disclosure`. Then import the components that you need: ```bash npm install @reach/disclosure # or yarn add @reach/disclosure ``` ```js import { Disclosure, DisclosureButton, DisclosurePanel, } from "@reach/disclosure"; ``` ## Usage ```jsx // jsx-demo function Example() { return ( Find out what lies beneath Here I am! I am the buried treasure! ); } ``` ## Component API ### Disclosure The wrapper component and context provider for a disclosure's button and panel components. A disclosure should never have more than one button or panel descendants. ```jsx Do a thing ... ``` It is important to note that the `Disclosure` component doesn't actually render a DOM node, so there is no way to add styles to a disclosure wrapper directly. If you want your disclosure to have a wrapper element you can nest one directly. ```jsx
Do a thing ...
``` #### Controlled Disclosure If you want to control the disclosure's value, you can do so by passing [`open`](#disclosure-open) and [`onChange`](#disclosure-onchange) props. ```jsx const [isOpen, setOpen] = React.useState(false); return ( setOpen(!isOpen)}> ); ``` #### Disclosure Props | Prop | Type | Required | | ---------------------------------------- | -------------------------- | -------- | | [`children`](#disclosure-children) | `node` | true | | [`defaultOpen`](#disclosure-defaultopen) | `boolean` | false | | [`id`](#disclosure-id) | `string` `number` | false | | [`onChange`](#disclosure-onchange) | `func` | false | | [`open`](#disclosure-open) | `boolean` | false | ##### Disclosure `children` `children: React.ReactNode` `Disclosure` expects to receive accept `DisclosureButton` and `DisclosurePanel` components as either direct children or descendants. It can also accept wrapper elements if desired, though it is not recommended to pass other arbitrary components within a disclosure in most cases. ```jsx
{/* OK! */} Click Me Collapse or open this content! {/* Also OK! */}
Click Me Collapse or open this content!
{/* Probably confusing, you should avoid! */}
Mary had a little lamb, little lamb, blah blah blah
Click Me Collapse or open this content!
You miss 100% of the shots you don't take
``` ##### Disclosure `defaultOpen` `defaultOpen?: boolean` Whether or not an uncontrolled disclosure component should default to its `open` state on the initial render. Defaults to `false`. ##### Disclosure `id` `id?: string | number` An id used to assign aria and id attributes to nested `DisclosureButton` and `DisclosurePanel` components. Since the Disclosure component itself does not render a DOM element, an `id` prop will not appear in the DOM directly as may be expected. Rather, we need to generate IDs for the panel and button based on a disclosure ID for aria compliance. If no `id` is passed we will generate descendant IDs for you. ```jsx
{" "} {/* no ID passed here! */} {/*