Design a controlled accordion
Define a controlled component contract
A controlled component receives the value that determines a behavior and a callback for requesting changes. The owner chooses how to respond. An accordion is a stack of headings that expand or collapse content. Here the owner stores one panel ID or null, which prevents contradictory open flags. The example allows zero or one open panel.
import { useState } from 'react';
function nextOpenId(currentId, requestedId) { return currentId === requestedId ? null : requestedId;}const sections = [ { id: 'materials', title: 'Materials', text: 'Paper and pencils are provided.' }, { id: 'arrival', title: 'Arrival', text: 'Arrive ten minutes early.' }];function Accordion({ prefix, openId, onToggle }) { return <div>{sections.map(section => { const expanded = openId === section.id; const panelId = `${prefix}-${section.id}-panel`; return <section key={section.id}> <h2><button type="button" aria-expanded={expanded} aria-controls={panelId} onClick={() => onToggle(section.id)}>{section.title}</button></h2> <div id={panelId} hidden={!expanded}><p>{section.text}</p></div> </section>; })}</div>;}export default function App() { const [openId, setOpenId] = useState(null); return <main><h1>Workshop guide</h1> <Accordion prefix="guide" openId={openId} onToggle={id => setOpenId(current => nextOpenId(current, id))} /> <button type="button" onClick={() => setOpenId(null)}>Close all</button> </main>;}Activate Materials, then Arrival. Arrival becomes visible and Materials becomes hidden. Activate Arrival again to close both. The heading wraps a native button, which supports Enter and Space without a custom keyboard handler. aria-expanded describes visibility, aria-controls points to a persistent panel ID, and hidden hides its content. Each accordion instance must receive a unique prefix. This is a source-level accessibility design, not verified conformance.
Keep instances independent when requested
import { useState } from 'react';
function Guide({ prefix, title, expanded, onToggle }) { return <section><h2><button type="button" aria-expanded={expanded} aria-controls={`${prefix}-info`} onClick={onToggle}>{title}</button></h2> <p id={`${prefix}-info`} hidden={!expanded}>Bring a notebook.</p></section>;}function Workshop({ prefix, title }) { const [expanded, setExpanded] = useState(false); return <Guide prefix={prefix} title={title} expanded={expanded} onToggle={() => setExpanded(old => !old)} />;}export default function App() { return <main><h1>Independent workshop guides</h1> <Workshop prefix="drawing" title="Drawing guide" /> <Workshop prefix="pottery" title="Pottery guide" /> </main>;}Both guides can remain open at once because each Workshop owns a separate boolean. The Guide itself is still controlled. To make the two guides mutually exclusive, move the open ID to App and pass each guide its own computed boolean. Controlled does not mean global.
Distinguish a default from a current value
import { useState } from 'react';
function Disclosure({ initiallyOpen }) { const [open, setOpen] = useState(initiallyOpen); return <section><h2><button type="button" aria-expanded={open} aria-controls="details" onClick={() => setOpen(v => !v)}>Materials</button></h2> <p id="details" hidden={!open}>Bring paper.</p></section>;}export default function App() { const [initiallyOpen, setInitiallyOpen] = useState(false); return <main><h1>Default versus current state</h1> <button type="button" onClick={() => setInitiallyOpen(v => !v)}> Change initial preference</button><p>Preference: {String(initiallyOpen)}</p> <Disclosure initiallyOpen={initiallyOpen} /> </main>;}The disclosure initially closes. Changing the parent preference to true does not open the existing disclosure: the initial value is used when the component state is first created. This is a valid uncontrolled contract when the prop is explicitly a default. If the owner needs to change current visibility, use expanded and onToggle as in the previous example. This fixture has just one Disclosure; reusable multiple instances would need distinct panel IDs.
Practice
Extend the first accordion with a third section called Cleanup. Preserve the rule of at most one open section, provide unique panel IDs, and check keyboard activation and the Close all button in a browser.