Accordion with show/hide toggles per item and ARIA support

Implement an Accordion component. An accordion is a vertically stacked set of expandable items. Each item has a title and can be toggled to show or hide its content. You are given an array of accordion items, each with a unique id, a title, a collapsed boolean, and a content function returning the item’s content (a React node in React, an HTML string in vanilla JS). The ARIA accordion pattern describes the keyboard behavior users expect.
title and a “Show” or “Hide” button depending on its collapsed state.collapsed) of that item’s content only.content() inside the expanded region when not collapsed. When collapsed, the content should not be visible but should remain mounted.aria-expanded, aria-controls, role="region", etc).
accordionItems: Array<{
id: string,
title: string,
collapsed: boolean,
content: () => React.ReactNode // vanilla JS: () => string (HTML)
}>
// Renders an accessible accordion UI. Each item displays its title, a toggle button, and content that is conditionally visible based on collapsed state.
Given:
accordionItems = [
{
title: 'Accordion Item 1',
id: 'accordion-item-1',
collapsed: true,
content: () => <p>Accordion Item 1 Content</p>
},
{
title: 'Accordion Item 2',
id: 'accordion-item-2',
collapsed: true,
content: () => <p>Accordion Item 2 Content</p>
},
{
title: 'Accordion Item 3',
id: 'accordion-item-3',
collapsed: true,
content: () => <p>Accordion Item 3 Content</p>
}
]
– The page displays three vertically stacked items, each with a title and a button labeled “Show”.
– Clicking “Show” on “Accordion Item 1” expands the first item, changes the button label to “Hide”, and reveals its content below the title.
– Clicking “Hide” collapses the item again.
– Each item is controlled independently.
The tests use the three items above (titles “Accordion Item 1” to “Accordion Item 3”, contents starting with “Accordion Item N Content”). They click your buttons and read the text on the screen, like a real user would. You don’t need test ids, specific class names, or an exact HTML structure. They only check that:
<button> whose text is exactly “Show” (collapsed) or “Hide” (expanded), in item order.display: none, the hidden attribute, or aria-hidden="true".aria-expanded="true" when its item is open and "false" when it is closed.