Extend an Accordion with Controlled Mode and Exclusive Opening
You have a working Accordion built using the compound component pattern. It renders panels that open and close when their trigger is clicked. The existing implementation is uncontrolled — it manages its own open/close state internally.
Extend the Accordion without breaking existing behavior:
- Exclusive mode — when the
exclusiveprop is set totrue, opening one panel must automatically close any other open panel. - Controlled mode — when
activePanelandonPanelChangeprops are both provided, the Accordion must defer to external state instead of managing its own. - Disabled panels —
Accordion.Panelmust accept adisabledprop. A disabled panel's trigger must be unclickable and its content must never open.
All existing tests for the default uncontrolled behavior must continue to pass.
- The existing uncontrolled behavior (click trigger → panel opens/closes) must not be broken.
- When
exclusive={true}is passed toAccordion, clicking a trigger closes any currently open panel before opening the new one. Clicking the already-open panel's trigger still closes it. - When
activePanel(string | null) andonPanelChange(function) are both provided, the Accordion reads its open/close state fromactivePaneland callsonPanelChange(id)oronPanelChange(null)on toggle — it must not callsetActivePanelinternally. Accordion.Panelmust accept adisabled?: booleanprop. Whendisabledis true, the panel's trigger must have adisabledHTML attribute and clicking it must not open the panel or call any callback.- The
data-testidattributes on existing elements must not be changed.
<Accordion exclusive> <Accordion.Panel id="a">...</Accordion.Panel> <Accordion.Panel id="b">...</Accordion.Panel> </Accordion>
Opening panel 'b' while 'a' is open → panel 'a' closes, panel 'b' opens.
Exclusive mode: at most one panel is open at any time.
const [open, setOpen] = useState('a');
<Accordion activePanel={open} onPanelChange={setOpen}>Clicking panel 'b' trigger → setOpen('b') is called → Accordion re-renders with panel 'b' open.Controlled mode: the Accordion reflects external state and delegates changes upward.
<Accordion.Panel id="c" disabled> <Accordion.Trigger panelId="c">Section C</Accordion.Trigger> ... </Accordion.Panel>
Clicking the trigger has no effect. Content for panel 'c' never renders.
Disabled panels are inert — their trigger is disabled and their content stays hidden.
- Do not modify
data-testidattributes on any existing element. - The compound component API (
Accordion,Accordion.Panel,Accordion.Trigger,Accordion.Content) must remain intact. - Controlled mode only activates when both
activePanelandonPanelChangeare provided — not just one of them.
- react environment
- react-testing-library runner
- 10000ms judge budget
The current implementation only allows one panel open at a time in exclusive mode. How would you add a `multi` prop that allows any number of panels to be open simultaneously?