Disclosure
A disclosure is a collapsible section of content. It is composed of a a header with a heading and trigger button, and a panel that contains the content.
install | yarn add react-aria-components |
---|---|
version | 1.4.1 |
usage | import {Disclosure} from 'react-aria-components' |
Example#
import {Button, Disclosure, DisclosurePanel, Heading} from 'react-aria-components';
<Disclosure>
<Heading>
<Button slot="trigger">
<svg viewBox="0 0 24 24">
<path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
</svg>
System Requirements
</Button>
</Heading>
<DisclosurePanel>
<p>Details about system requirements here.</p>
</DisclosurePanel>
</Disclosure>
import {
Button,
Disclosure,
DisclosurePanel,
Heading
} from 'react-aria-components';
<Disclosure>
<Heading>
<Button slot="trigger">
<svg viewBox="0 0 24 24">
<path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
</svg>
System Requirements
</Button>
</Heading>
<DisclosurePanel>
<p>Details about system requirements here.</p>
</DisclosurePanel>
</Disclosure>
import {
Button,
Disclosure,
DisclosurePanel,
Heading
} from 'react-aria-components';
<Disclosure>
<Heading>
<Button slot="trigger">
<svg viewBox="0 0 24 24">
<path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
</svg>
System
Requirements
</Button>
</Heading>
<DisclosurePanel>
<p>
Details about
system
requirements
here.
</p>
</DisclosurePanel>
</Disclosure>
Show CSS
.react-aria-Disclosure {
.react-aria-Button[slot=trigger] {
background: none;
border: none;
box-shadow: none;
font-weight: bold;
font-size: 16px;
display: flex;
align-items: center;
gap: 8px;
svg {
rotate: 0deg;
transition: rotate 200ms;
width: 12px;
height: 12px;
fill: none;
stroke: currentColor;
stroke-width: 3px;
}
}
&[data-expanded] .react-aria-Button[slot=trigger] svg {
rotate: 90deg;
}
}
.react-aria-DisclosurePanel {
margin-left: 32px;
}
.react-aria-Disclosure {
.react-aria-Button[slot=trigger] {
background: none;
border: none;
box-shadow: none;
font-weight: bold;
font-size: 16px;
display: flex;
align-items: center;
gap: 8px;
svg {
rotate: 0deg;
transition: rotate 200ms;
width: 12px;
height: 12px;
fill: none;
stroke: currentColor;
stroke-width: 3px;
}
}
&[data-expanded] .react-aria-Button[slot=trigger] svg {
rotate: 90deg;
}
}
.react-aria-DisclosurePanel {
margin-left: 32px;
}
.react-aria-Disclosure {
.react-aria-Button[slot=trigger] {
background: none;
border: none;
box-shadow: none;
font-weight: bold;
font-size: 16px;
display: flex;
align-items: center;
gap: 8px;
svg {
rotate: 0deg;
transition: rotate 200ms;
width: 12px;
height: 12px;
fill: none;
stroke: currentColor;
stroke-width: 3px;
}
}
&[data-expanded] .react-aria-Button[slot=trigger] svg {
rotate: 90deg;
}
}
.react-aria-DisclosurePanel {
margin-left: 32px;
}
Features#
Disclosures can be built with the <details> and <summary> HTML elements, but this can be difficult to style, especially when adding adjacent interactive elements, like buttons, to the disclosure's heading. Disclosure
helps achieve accessible disclosures implemented with the correct ARIA pattern while making custom styling easy.
- Flexible - Structured such that it can be used standalone or combined with other disclosures to form a
DisclosureGroup
- Keyboard Interaction - When focused, a disclosure's visibility can be toggled with either the Enter or Space key, and the appropriate ARIA attributes are automatically applied.
- Accessible - Uses hidden="until-found" in supported browsers, enabling find-in-page search support and improved search engine visibility for collapsed content
- Styleable - Keyboard focus, disabled and expanded states are provided for easy styling. These states only apply when interacting with an appropriate input device, unlike CSS pseudo classes.
Anatomy#
A disclosure consists of a button and panel of content. The button contains the label representing content within the panel, and the panel is the section of content that is associated with the button which is either expanded or collapsed.
import {Button, Disclosure, DisclosurePanel, Heading} from 'react-aria-components';
<Disclosure>
<Heading>
<Button />
</Heading>
<DisclosurePanel />
</Disclosure>
import {
Button,
Disclosure,
DisclosurePanel,
Heading
} from 'react-aria-components';
<Disclosure>
<Heading>
<Button />
</Heading>
<DisclosurePanel />
</Disclosure>
import {
Button,
Disclosure,
DisclosurePanel,
Heading
} from 'react-aria-components';
<Disclosure>
<Heading>
<Button />
</Heading>
<DisclosurePanel />
</Disclosure>
Reusable wrappers#
If you will use a Disclosure in multiple places in your app, you can wrap all of the pieces into a reusable component. This way, the DOM structure, styling code, and other logic are defined in a single place and reused everywhere to ensure consistency.
This example wraps Disclosure
and all of its children together into a single component.
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">
<svg viewBox="0 0 24 24">
<path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
</svg>
{title}
</Button>
</Heading>
<DisclosurePanel>
<p>{children}</p>
</DisclosurePanel>
</Disclosure>
)
}
<MyDisclosure title="Manage your account">
Details on managing your account
</MyDisclosure>
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">
<svg viewBox="0 0 24 24">
<path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
</svg>
{title}
</Button>
</Heading>
<DisclosurePanel>
<p>{children}</p>
</DisclosurePanel>
</Disclosure>
);
}
<MyDisclosure title="Manage your account">
Details on managing your account
</MyDisclosure>
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">
<svg viewBox="0 0 24 24">
<path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
</svg>
{title}
</Button>
</Heading>
<DisclosurePanel>
<p>{children}</p>
</DisclosurePanel>
</Disclosure>
);
}
<MyDisclosure title="Manage your account">
Details on managing
your account
</MyDisclosure>
Expanded#
An uncontrolled Disclosure can be expanded by default using the defaultExpanded
prop.
<MyDisclosure title="Download, Install, and Set Up" defaultExpanded>
Instructions on how to download, install, and set up
</MyDisclosure>
<MyDisclosure
title="Download, Install, and Set Up"
defaultExpanded
>
Instructions on how to download, install, and set up
</MyDisclosure>
<MyDisclosure
title="Download, Install, and Set Up"
defaultExpanded
>
Instructions on how
to download, install,
and set up
</MyDisclosure>
A controlled Disclosure can be expanded and collapsed using isExpanded
and onExpandedChange
function ControlledExpanded() {
let [isExpanded, setIsExpanded] = React.useState(true);
return (
<MyDisclosure
title="Download, Install, and Set Up"
isExpanded={isExpanded}
onExpandedChange={setIsExpanded}
>
Instructions on how to download, install, and set up
</MyDisclosure>
);
}
<ControlledExpanded />
function ControlledExpanded() {
let [isExpanded, setIsExpanded] = React.useState(true);
return (
<MyDisclosure
title="Download, Install, and Set Up"
isExpanded={isExpanded}
onExpandedChange={setIsExpanded}
>
Instructions on how to download, install, and set up
</MyDisclosure>
);
}
<ControlledExpanded />
function ControlledExpanded() {
let [
isExpanded,
setIsExpanded
] = React.useState(
true
);
return (
<MyDisclosure
title="Download, Install, and Set Up"
isExpanded={isExpanded}
onExpandedChange={setIsExpanded}
>
Instructions on how
to download,
install, and set up
</MyDisclosure>
);
}
<ControlledExpanded />
Disabled#
A Disclosure can be disabled using the isDisabled
prop.
<MyDisclosure title="Introduction to Knitting" isDisabled>
Details about knitting here
</MyDisclosure>
<MyDisclosure title="Introduction to Knitting" isDisabled>
Details about knitting here
</MyDisclosure>
<MyDisclosure
title="Introduction to Knitting"
isDisabled
>
Details about
knitting here
</MyDisclosure>
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.
<Disclosure>
<div style={{display: 'flex', alignItems: 'center'}}>
<Heading>
<Button slot="trigger">
<svg viewBox="0 0 24 24">
<path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
</svg>
System Requirements
</Button>
</Heading>
<Button>Click me</Button>
</div>
<DisclosurePanel>
<p>Details about system requirements here.</p>
</DisclosurePanel>
</Disclosure>
<Disclosure>
<div style={{display: 'flex', alignItems: 'center'}}>
<Heading>
<Button slot="trigger">
<svg viewBox="0 0 24 24">
<path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
</svg>
System Requirements
</Button>
</Heading>
<Button>Click me</Button>
</div>
<DisclosurePanel>
<p>Details about system requirements here.</p>
</DisclosurePanel>
</Disclosure>
<Disclosure>
<div
style={{
display: 'flex',
alignItems:
'center'
}}
>
<Heading>
<Button slot="trigger">
<svg viewBox="0 0 24 24">
<path d="m8.25 4.5 7.5 7.5-7.5 7.5" />
</svg>
System
Requirements
</Button>
</Heading>
<Button>
Click me
</Button>
</div>
<DisclosurePanel>
<p>
Details about
system
requirements
here.
</p>
</DisclosurePanel>
</Disclosure>
Props#
Disclosure#
Name | Type | Description |
id | Key | An id for the disclosure when used within a DisclosureGroup, matching the id used in expandedKeys . |
isDisabled | boolean | Whether the disclosure is disabled. |
isExpanded | boolean | Whether the disclosure is expanded (controlled). |
defaultExpanded | boolean | Whether the disclosure is expanded by default (uncontrolled). |
children | ReactNode | (
(values: DisclosureRenderProps
& & {}
)) => ReactNode | The children of the component. A function may be provided to alter the children based on component state. |
className | string | (
(values: DisclosureRenderProps
& & {}
)) => string | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (
(values: DisclosureRenderProps
& & {}
)) => CSSProperties | undefined | The inline style for the element. A function may be provided to compute the style based on component state. |
Events
Name | Type | Description |
onExpandedChange | (
(isExpanded: boolean
)) => void | Handler that is called when the disclosure's expanded state changes. |
Button#
Show props
Name | Type | Default | Description |
form | string | — | The |
formAction | string | — | The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner. |
formEncType | string | — | Indicates how to encode the form data that is submitted. |
formMethod | string | — | Indicates the HTTP method used to submit the form. |
formNoValidate | boolean | — | Indicates that the form is not to be validated when it is submitted. |
formTarget | string | — | Overrides the target attribute of the button's form owner. |
name | string | — | Submitted as a pair with the button's value as part of the form data. |
value | string | — | The value associated with the button's name when it's submitted with the form data. |
isPending | boolean | — | Whether the button is in a pending state. This disables press and hover events while retaining focusability, and announces the pending state to screen readers. |
isDisabled | boolean | — | Whether the button is disabled. |
autoFocus | boolean | — | Whether the element should receive focus on render. |
type | 'button'
| 'submit'
| 'reset' | 'button' | The behavior of the button when used in an HTML form. |
children | ReactNode | (
(values: ButtonRenderProps
& & {}
)) => ReactNode | — | The children of the component. A function may be provided to alter the children based on component state. |
className | string | (
(values: ButtonRenderProps
& & {}
)) => string | — | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (
(values: ButtonRenderProps
& & {}
)) => CSSProperties | undefined | — | The inline style for the element. A function may be provided to compute the style based on component state. |
Events
Name | Type | Description |
onPress | (
(e: PressEvent
)) => void | Handler that is called when the press is released over the target. |
onPressStart | (
(e: PressEvent
)) => void | Handler that is called when a press interaction starts. |
onPressEnd | (
(e: PressEvent
)) => void | Handler that is called when a press interaction ends, either over the target or when the pointer leaves the target. |
onPressChange | (
(isPressed: boolean
)) => void | Handler that is called when the press state changes. |
onPressUp | (
(e: PressEvent
)) => void | Handler that is called when a press is released over the target, regardless of whether it started on the target or not. |
onFocus | (
(e: FocusEvent<Target>
)) => void | Handler that is called when the element receives focus. |
onBlur | (
(e: FocusEvent<Target>
)) => void | Handler that is called when the element loses focus. |
onFocusChange | (
(isFocused: boolean
)) => void | Handler that is called when the element's focus status changes. |
onKeyDown | (
(e: KeyboardEvent
)) => void | Handler that is called when a key is pressed. |
onKeyUp | (
(e: KeyboardEvent
)) => void | Handler that is called when a key is released. |
onHoverStart | (
(e: HoverEvent
)) => void | Handler that is called when a hover interaction starts. |
onHoverEnd | (
(e: HoverEvent
)) => void | Handler that is called when a hover interaction ends. |
onHoverChange | (
(isHovering: boolean
)) => void | Handler that is called when the hover state changes. |
Layout
Name | Type | Description |
slot | string | null | A slot name for the component. Slots allow the component to receive props from a parent component.
An explicit |
Accessibility
Name | Type | Description |
id | string | The element's unique identifier. See MDN. |
excludeFromTabOrder | boolean | Whether to exclude the element from the sequential tab order. If true, the element will not be focusable via the keyboard by tabbing. This should be avoided except in rare scenarios where an alternative means of accessing the element or its functionality via the keyboard is available. |
preventFocusOnPress | boolean | Whether to prevent focus from moving to the button when pressing it. Caution, this can make the button inaccessible and should only be used when alternative keyboard interaction is provided, such as ComboBox's MenuTrigger or a NumberField's increment/decrement control. |
aria-expanded | boolean
| 'true'
| 'false' | Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed. |
aria-haspopup | boolean
| 'menu'
| 'listbox'
| 'tree'
| 'grid'
| 'dialog'
| 'true'
| 'false' | Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element. |
aria-controls | string | Identifies the element (or elements) whose contents or presence are controlled by the current element. |
aria-pressed | boolean
| 'true'
| 'false'
| 'mixed' | Indicates the current "pressed" state of toggle buttons. |
aria-label | string | Defines a string value that labels the current element. |
aria-labelledby | string | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | Identifies the element (or elements) that describes the object. |
aria-details | string | Identifies the element (or elements) that provide a detailed, extended description for the object. |
DisclosurePanel#
Show props
Name | Type | Description |
children | ReactNode | The children of the component. |
className | string | (
(values: DisclosurePanelRenderProps
& & {}
)) => string | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (
(values: DisclosurePanelRenderProps
& & {}
)) => CSSProperties | undefined | The inline style for the element. A function may be provided to compute the style based on component state. |
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.
.react-aria-Disclosure {
/* ... */
}
.react-aria-Disclosure {
/* ... */
}
.react-aria-Disclosure {
/* ... */
}
A custom className
can also be specified on any component. This overrides the default className
provided by React Aria with your own.
<Disclosure className="my-disclosure">
{/* ... */}
</Disclosure>
<Disclosure className="my-disclosure">
{/* ... */}
</Disclosure>
<Disclosure className="my-disclosure">
{/* ... */}
</Disclosure>
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:
.react-aria-Disclosure[data-expanded] {
/* ... */
}
.react-aria-Disclosure[data-expanded] {
/* ... */
}
.react-aria-Disclosure[data-expanded] {
/* ... */
}
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.
<Disclosure
className={({ isExpanded }) =>
isExpanded ? 'border-blue-500' : 'border-gray-600'}
/>
<Disclosure
className={({ isExpanded }) =>
isExpanded ? 'border-blue-500' : 'border-gray-600'}
/>
<Disclosure
className={(
{ isExpanded }
) =>
isExpanded
? 'border-blue-500'
: 'border-gray-600'}
/>
The states, selectors, and render props for each component used in a Disclosure
are documented below.
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:
Name | CSS Selector | Description |
isExpanded | [data-expanded] | Whether the disclosure is expanded. |
isFocusVisibleWithin | [data-focus-visible-within] | Whether the disclosure has keyboard focus. |
isDisabled | [data-disabled] | Whether the disclosure is disabled. |
state | — | State of the disclosure. |
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:
Name | CSS Selector | Description |
isHovered | [data-hovered] | Whether the button is currently hovered with a mouse. |
isPressed | [data-pressed] | Whether the button is currently in a pressed state. |
isFocused | [data-focused] | Whether the button is focused, either via a mouse or keyboard. |
isFocusVisible | [data-focus-visible] | Whether the button is keyboard focused. |
isDisabled | [data-disabled] | Whether the button is disabled. |
isPending | [data-pending] | Whether the button is currently in a pending state. |
DisclosurePanel#
A DisclosurePanel
can be targeted with the .react-aria-DisclosurePanel
CSS selector, or by overriding with a custom className
.
Name | CSS Selector | Description |
isFocusVisibleWithin | [data-focus-visible-within] | Whether keyboard focus is within the disclosure panel. |
Advanced Customization#
Contexts#
All React Aria Components export a corresponding context that can be used to send props to them from a parent element. This enables you to build your own compositional APIs similar to those found in React Aria Components itself. You can send any prop or ref via context that you could pass to the corresponding component. The local props and ref on the component are merged with the ones passed via context, with the local props taking precedence (following the rules documented in mergeProps).
Component | Context | Props | Ref |
Disclosure | DisclosureContext | DisclosureProps | HTMLDivElement |
This example shows a DisclosureGroup
component that renders a group of disclosures. The entire group can be marked as disabled via the isDisabled
prop, which is passed to all child disclosures via the DisclosureContext
provider.
import {DisclosureContext} from 'react-aria-components';
interface DisclosureGroupProps {
children?: React.ReactNode,
isDisabled?: boolean
}
function DisclosureGroup({children, isDisabled}: DisclosureGroupProps) {
return (
<div style={{display: 'flex', flexDirection: 'column'}}>
<DisclosureContext.Provider value={{isDisabled}}>
{children}
</DisclosureContext.Provider>
</div>
)
}
<DisclosureGroup isDisabled>
<MyDisclosure title="How to make a return" >
Details about returning items
</MyDisclosure>
<MyDisclosure title="How to cancel an order" >
Details on canceling an order
</MyDisclosure>
</DisclosureGroup>
import {DisclosureContext} from 'react-aria-components';
interface DisclosureGroupProps {
children?: React.ReactNode;
isDisabled?: boolean;
}
function DisclosureGroup(
{ children, isDisabled }: DisclosureGroupProps
) {
return (
<div
style={{ display: 'flex', flexDirection: 'column' }}
>
<DisclosureContext.Provider value={{ isDisabled }}>
{children}
</DisclosureContext.Provider>
</div>
);
}
<DisclosureGroup isDisabled>
<MyDisclosure title="How to make a return">
Details about returning items
</MyDisclosure>
<MyDisclosure title="How to cancel an order">
Details on canceling an order
</MyDisclosure>
</DisclosureGroup>
import {DisclosureContext} from 'react-aria-components';
interface DisclosureGroupProps {
children?:
React.ReactNode;
isDisabled?: boolean;
}
function DisclosureGroup(
{
children,
isDisabled
}: DisclosureGroupProps
) {
return (
<div
style={{
display: 'flex',
flexDirection:
'column'
}}
>
<DisclosureContext.Provider
value={{
isDisabled
}}
>
{children}
</DisclosureContext.Provider>
</div>
);
}
<DisclosureGroup
isDisabled
>
<MyDisclosure title="How to make a return">
Details about
returning items
</MyDisclosure>
<MyDisclosure title="How to cancel an order">
Details on
canceling an order
</MyDisclosure>
</DisclosureGroup>
State#
DisclosureGroup provides a DisclosureState
object to its children via DisclosureStateContext
. This can be used to access and manipulate the disclosure group's state.
Hooks#
If you need to customize things further, such as accessing internal state or customizing DOM structure, you can drop down to the lower level Hook-based API. See useDisclosure.