React Reference ยท Built-in DOM Components
The details element in React
Reference for the HTML details and summary elements in React: the open attribute, the onToggle event, exclusive accordions with name, and controlled state.
<details> is the browser's built-in disclosure widget: a <summary> line the user can click to show or hide the rest of the content. It is keyboard accessible, announces its expanded state to screen readers and needs no JavaScript. In React you only add code when your component needs to know whether the widget is open, or wants to open it from state.
The open attribute is the state: present means expanded. React exposes it as the boolean prop open, and the browser fires a toggle event after the state changes, which React exposes as onToggle.
<details onToggle={(event) => console.log(event.currentTarget.open)}> <summary>Shipping details</summary> <p>Orders leave the warehouse within two days.</p> </details>
Try it
Syntax
<details open={isOpen} name="group" onToggle={handler}> <summary>Label</summary> {content} </details>
| Name | Description |
|---|---|
open |
Boolean. true renders the element expanded, false collapsed. |
name |
A group name. Only one <details> in the same group (and the same tree) can be open at a time; opening one closes the others. |
onToggle |
Called after the open state changed, whether by the user, by the open prop or by a group closing it. Read event.currentTarget.open. |
<summary> |
The clickable label. It must be the first child of <details>. If it is missing, the browser shows a default label. |
Returns: a <details> element. Only direct <summary> and the rest of the content are meaningful children.
Usage notes
- Uncontrolled by default. Without
open, the browser owns the state and React never touches it. That is the simplest choice when you do not need the value. open={true}is a starting point, not a lock. When the user clicks the summary the DOM changes, and React only writes the attribute again if the prop value changes in a later render. If your state and the DOM disagree, update the state inonToggleas in the first example.toggledoes not bubble.onToggleon a wrapper around several<details>will not hear them. Attach it to each<details>.- Initial
toggleevent. A<details>that starts open may dispatch a toggle event when it is inserted, so write handlers that only copycurrentTarget.openinto state and nothing more. - Do not put interactive controls inside
<summary>unless you want clicks on them to toggle the widget too. Links and buttons inside it lead to confusing behavior and poor accessibility. - Hidden content is still rendered. Children of a closed
<details>exist in the DOM (the browser hides them), so effects and state inside them still run. Render heavy content conditionally onopen. - Search and anchors. Some browsers can open a closed
<details>when the user finds text inside it with find-in-page or follows a link to an element inside, andonTogglefires then. - Styling. The marker is a
::markeronsummary; you can restyle it or hide the default one with CSS. Animating height needs extra care because closed content has no height.