Frontend School

Robust and Accessible Accordion Component

Machine Coding
hard

Robust and Accessible Accordion Component

Hard
React
Component
Accessibility
State Management
UI

Overview

Modern web applications frequently need to organize large amounts of content efficiently, presenting information in a structured, user-friendly manner without overwhelming the user. An Accordion component is a fundamental UI element that addresses this challenge by allowing users to toggle the visibility of content sections. This problem focuses on building a highly flexible, accessible, and performant Accordion component capable of managing diverse content and interactions, a critical skill for L5 engineers.

What you need to do / Task

Your primary task is to implement a reusable Accordion component in React.

  1. Core Functionality: Create an Accordion component that accepts an array of item data. Each item should have a clickable header that toggles the visibility of its corresponding content panel.
  2. Controlled Component: The Accordion must operate as a controlled component. It should accept openItemIds (an array of IDs of currently open items) and an onToggle callback prop. The onToggle callback should be invoked with the updated openItemIds whenever an item's header is clicked.
  3. Single vs. Multiple Open: The component should support a multipleOpen boolean prop.
    • If multipleOpen is true, multiple accordion items can be open simultaneously.
    • If multipleOpen is false (default), only one item can be open at a time. Clicking a new item's header should close the currently open item.
  4. Accessibility (A11y): Ensure the Accordion is fully accessible:
    • Keyboard Navigation: Users must be able to navigate between accordion headers using Tab and Shift+Tab. Space or Enter keys on a focused header should toggle its content.
    • ARIA Attributes: Properly apply ARIA roles and attributes (e.g., role="heading", aria-expanded, aria-controls, aria-labelledby, role="region") to ensure screen reader compatibility.
  5. Transitions: Implement a smooth visual transition for the expansion and collapse of content panels.

UI/UX expectations

  • Each accordion item should clearly delineate its header from its content panel.
  • The header should visually indicate its interactive nature (e.g., pointer cursor on hover).
  • When an item is open, its content panel should be visible below its header. When closed, the content panel should be hidden.
  • A visual indicator (e.g., an arrow icon) should indicate the open/closed state of an item.
  • Transitions should be subtle and smooth, enhancing the user experience without causing jarring visual changes.

Acceptance criteria

  • The Accordion component is correctly implemented and renders with the provided mock data.
  • Clicking an accordion header correctly toggles its content panel's visibility.
  • The openItemIds and onToggle props are correctly handled for controlled behavior.
  • The multipleOpen prop correctly enforces single or multiple open items.
  • Keyboard navigation (Tab, Shift+Tab, Space, Enter) works as expected for headers.
  • All necessary ARIA attributes are applied correctly for accessibility.
  • Content panels expand and collapse with a smooth visual transition.

Constraints

Constraints & assumptions

  • You must use React functional components and hooks.
  • No external UI libraries (e.g., Material UI, Ant Design) or state management libraries (e.g., Redux, Zustand) are allowed. Standard React features are sufficient.
  • The component should be robust enough to handle up to 100 accordion items efficiently.
  • Focus on the Accordion component itself; the App component will primarily manage the openItemIds state.
  • Browser support: Modern evergreen browsers (Chrome, Firefox, Edge, Safari).

Out of scope

  • Complex styling beyond basic layout and transitions.
  • Deep linking to specific accordion items.
  • Lazy loading content for individual accordion panels (assume content is readily available).
  • Persisting open states across page refreshes.

What interviewers evaluate

  • Correctness and Completeness: Does the component meet all functional and accessibility requirements?
  • Component Design & API: Is the component's API intuitive and flexible (e.g., controlled mode, multipleOpen prop)?
  • Code Quality: Is the code well-structured, readable, maintainable, and idiomatic React?
  • Performance & Efficiency: Does the solution avoid unnecessary re-renders or expensive computations, especially with a moderate number of items?
  • Accessibility: Proper implementation of keyboard navigation and ARIA attributes.

Loading editor...