DisclosureReact
A collapsible panel controlled by a button.
Rendered by React 19.3.1
Loading React…177Tests passing in React · 0 failing
9.5 KiBSize
1.10 msOpen
0.50 msClose
15.0 msOpenMutations
8.00 msCloseMutations
0.45 MiBMemory
productdevbook and 18 upstream authors12 commits · last
Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Import the component and assemble its parts:
<Collapsible.Root>
<Collapsible.Trigger />
<Collapsible.Panel />
</Collapsible.Root>Examples
The demo at the top is the one example this component has so far.
API reference
7 props across 3 parts, in React syntax. className, style, render are listed once, under Shared props.
PanelCollapsible.Panel
A panel with the collapsible contents. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
hiddenUntilFoundAllows the browser's built-in page search to find and expand the panel contents. | boolean | false |
keepMountedWhether to keep the element in the DOM while the panel is hidden. | boolean | false |
| Data attribute | Description |
|---|---|
data-open | Present when the collapsible panel is open. |
data-closed | Present when the collapsible panel is closed. |
data-starting-style | Present when the panel begins animating in. |
data-ending-style | Present when the panel is animating out. |
| CSS variable | Description |
|---|---|
--collapsible-panel-height | The collapsible panel's height. |
--collapsible-panel-width | The collapsible panel's width. |
RootCollapsible.Root
Groups all parts of the collapsible. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
defaultOpenWhether the collapsible panel is initially open. | boolean | false |
openWhether the collapsible panel is currently open. | boolean | — |
onOpenChangeEvent handler called when the panel is opened or closed. | ((open: boolean, eventDetails: CollapsibleRootChangeEventDetails) => void) | — |
disabledWhether the component should ignore user interaction. | boolean | false |
| Data attribute | Description |
|---|---|
data-open | Present when the collapsible is open. |
data-closed | Present when the collapsible is closed. |
data-starting-style | Present when the collapsible begins animating in. |
data-ending-style | Present when the collapsible is animating out. |
TriggerCollapsible.Trigger
A button that opens and closes the collapsible panel. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
nativeButtonWhether the component renders a native <button> element when replacing it | boolean | true |
| Data attribute | Description |
|---|---|
data-panel-open | Present when the collapsible panel is open. |
data-disabled | Present when the trigger or the collapsible is disabled. |
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
classNameCSS class applied to the element, or a function that
returns a class based on the component's state. | string | ((state: State) => string | undefined) |
styleStyle applied to the element, or a function that
returns a style object based on the component's state. | React.CSSProperties | ((state: State) => React.CSSProperties | undefined) |
renderAllows you to replace the component's HTML element
with a different tag, or compose it with another component. | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State> |
Keyboard and accessibility
Keyboard
| Keys | Does | Part |
|---|---|---|
| EnterSpace | Opens or closes the panel | Trigger |
| EnterSpace | Does nothing while the collapsible is disabled | Trigger |
Accessibility
| Part | Sets | So that |
|---|---|---|
| Trigger | aria-expanded, aria-controls to the panel while open | a screen reader says whether the section is open and where it is |
| Panel | hidden="until-found" with hiddenUntilFound | find in page reaches text in the closed panel and opens it |
Focus order
- Trigger is one tab stop.
- The content of an open Panel follows; a closed Panel's content is skipped.