--- title: Accordion description: Component for grouping
HTML elements where only one of them can be open at the same time. ---
Component for grouping details HTML elements where only one of them can be open at the same time.
An accordion is a vertically stacked group of collapsible sections. HTML has already a native element for this, the `
` element, but it doesn't support the "only one open at a time" behavior, so we need to add some JS to make it work, and that's what this component does. If you don't need to ensure only one section is open at a time, you don't need this component at all, just use the `
` element directly. The `Accordion` is a simple wrapper plus some JS logic, so it doesn't uses any arguments and it's as accesible as the `details` element you put inside. ## Events The `Accordion` doesn't emit or listen to any events, but the `
` elements inside do. In addition to the usual events supported by HTML elements, the `
` element supports the `toggle` event, which is dispatched to the `
` element whenever its state changes between open and closed. The `Accordion` component listen to it to be able to close the other `
` elements when one is opened. The `toggle` event is sent *after* the state is changed, although if the state changes multiple times before the browser can dispatch the event, the events are coalesced so that only one is sent. ```js details.addEventListener("toggle", (event) => { if (details.open) { /* the element was toggled open */ } else { /* the element was toggled closed */ } }); ```