Frontend School

Dynamic and Accessible Multi-Expand Accordion

Machine Coding
hard

Dynamic and Accessible Multi-Expand Accordion

Hard
React
TypeScript
Frontend
UI
Accessibility

Overview

At OYO, providing detailed and well-structured information about properties is crucial for guest satisfaction and booking conversion. We often need to display various sections of information (e.g., amenities, house rules, location details) in a compact and user-friendly manner. This problem challenges you to build a flexible and accessible Accordion component that can handle multiple expandable sections, enhancing the user experience on our property detail pages.

What you need to do / Task

Your primary task is to develop a reusable and controlled Accordion component in React/TypeScript.

  1. Implement the Accordion component: This component should accept an array of AccordionItemData (defined below) and manage the rendering of individual accordion items.
  2. Implement the AccordionItem component: A sub-component responsible for rendering a single accordion section, including its header and expandable content.
  3. Controlled Component Design: The Accordion component should be a controlled component, meaning its parent (App in this case) dictates which items are open via props and receives callbacks when an item is toggled. It should support having multiple items open simultaneously.
  4. Dynamic Content: The header and content for each accordion item should be dynamically rendered based on the AccordionItemData provided.
  5. Accessibility (A11y): Ensure the Accordion is accessible. Implement basic keyboard navigation (e.g., Space or Enter key to toggle an item when its header is focused) and appropriate ARIA attributes (aria-expanded, aria-controls, role="heading", role="region").

UI/UX expectations

  • Each accordion item should have a clearly distinguishable header and a content area.
  • Clicking on an item's header should toggle its content visibility (expand/collapse).
  • When an item is expanded, its content should be visible. When collapsed, its content should be hidden.
  • There should be a visual indicator (e.g., an arrow icon or text change) in the header to show whether the item is currently expanded or collapsed.
  • The overall layout should be clean and responsive, resembling standard accordion patterns. Minimal styling is provided; focus on functionality.

Acceptance criteria

  • The Accordion component renders all provided items.
  • Clicking an accordion header expands its content if collapsed, and collapses it if expanded.
  • Multiple accordion items can be open at the same time.
  • The App component successfully controls the open/closed state of the accordion items.
  • Keyboard users can navigate between accordion headers using Tab and toggle items using Space or Enter.
  • Appropriate ARIA attributes (aria-expanded, aria-controls, role="heading", role="region") are correctly applied to the accordion elements.
  • The component is written in TypeScript, with clear interfaces for props and data structures.

Constraints

Constraints & assumptions

  • You should use React with TypeScript.
  • The total time budget for this problem is approximately 45 minutes. Focus on core functionality and accessibility.
  • The AccordionItemData structure will be provided in /App.tsx.
  • Styling (CSS) can be minimal; focus on semantic HTML and functional correctness.
  • The number of accordion items will be relatively small (e.g., 5-10 items).

Out of scope

  • Complex animations for expanding/collapsing.
  • Support for single-item expansion only (i.e., "one at a time" mode). The requirement is for multiple items to be open.
  • Performance optimizations for thousands of items (e.g., virtualization).
  • Internationalization (i18n) or localization (l10n).

What interviewers evaluate

  • Component Design & Reusability: How well the Accordion and AccordionItem components are structured, how reusable they are, and how effectively state and props are managed for controlled behavior.
  • Correctness & Functionality: Whether all functional requirements, including multiple open items and dynamic content, are met accurately.
  • Accessibility (A11y): Proper implementation of ARIA attributes and keyboard navigation.
  • TypeScript Usage: Effective use of types and interfaces for clarity and robustness.
  • Code Quality: Readability, maintainability, and adherence to best practices.

Loading editor...