Note: This is an advanced version of Accordion, you should complete that question first before attempting this question.
An accordion is a vertically stacked set of interactive headings that each contain a title, content snippet, or thumbnail representing a section of content. The headings function as controls that enable users to reveal or hide their associated sections of content. Accordions are commonly used to reduce the need to scroll when presenting multiple sections of content on a single page.
Source: Accordion Pattern
In Accordion, we built a functional accordion component that can expand/collapse each section's contents. However, building good UI components goes beyond functionality and we have to ensure our components have great accessibility as well by adding the right ARIA roles, states, and properties to the DOM elements.
The ARIA Authoring Practices Guide has a long list of guidelines for the ARIA roles, states, and properties to add to the various elements of an accordion. We should implement the following (improvised) guidelines for this question:
<button> element.aria-expanded set to true. If the panel is not visible, aria-expanded is set to false.aria-controls set to the ID of the element containing the accordion panel content.region and aria-labelledby with a value that refers to the button that controls display of the panel.The skeleton code uses the solution of Accordion, but you are free to use your own solution as a starting point.
We'll build on top of Accordion's solution.
Other than adding the right ARIA roles and states, which is straightforward, we also need to link the accordion headers with the corresponding accordion contents/panels. Hence we create two functions, getAccordionHeaderId and getAccordionPanelId to do this.
getAccordionHeaderId generates a unique ID string to use as the value of the id attribute of the header element. This ID will be used as the value of the aria-labelledby attribute of the corresponding accordion panel.getAccordionPanelId generates a unique ID string to use as the value of the id attribute of accordion panel. This ID will be used as the value of the aria-controls attribute of the corresponding accordion header.