Accordion

MediumAccordionReact20 mins

by Pratik Rai

Read the full write-up

Build an accordion: a list of headers where clicking one reveals its panel, and clicking it again hides it.

Sections open independently — opening one does not close the others.

Requirements

  1. All sections start closed.

  2. Clicking a header opens its panel; clicking the same header again closes it.

  3. Several sections can be open at the same time.

  4. The chevron for an open section is rotated (the .chevron-open class already does this).

  5. Each header carries aria-expanded reflecting its state, and aria-controls pointing at the id of the panel it opens.

Notes

  • The markup and styling are already there — only the open/closed behaviour is missing.

  • A closed panel should not be in the DOM at all, rather than hidden with CSS.

Hints

Loading editor…