Frontend School

Build a Reusable Single-Select Accordion Component

Machine Coding
medium

Build a Reusable Single-Select Accordion Component

Medium
UI
React
Accordion
Component
Accessibility

Overview

In ServiceNow's Now Experience, building robust and reusable UI components is crucial for delivering intuitive and efficient user interfaces. One common pattern is the Accordion, used to manage space and present categorized information cleanly. For this problem, you will implement a single-select Accordion component, focusing on reusability, state management, and accessibility, which are key considerations in enterprise-grade UI development.

What you need to do / Task

Your goal is to create a Accordion React component that displays a list of collapsible items. The component should adhere to the following requirements:

  1. Component Structure: Design a main Accordion component that accepts an array of data, and an AccordionItem sub-component to render individual collapsible sections.
  2. Single-Select Behavior: Only one accordion item can be open at any given time. Opening a new item must automatically close any currently open item.
  3. Toggle Functionality: Clicking on an item's header should toggle its content visibility. If an item is open, clicking its header should close it.
  4. Keyboard Navigation: Implement keyboard accessibility:
    • Space or Enter keys should toggle the active accordion item when its header has focus.
    • ArrowDown and ArrowUp keys should move focus between accordion headers.
    • Home key should move focus to the first accordion header.
    • End key should move focus to the last accordion header.

UI/UX expectations

  • Each accordion item should have a clearly distinguishable header and content area.
  • The header should visually indicate its current state (open/closed), e.g., using a chevron icon that rotates.
  • Content areas should expand and collapse smoothly (a simple CSS transition is sufficient).
  • Focus indicators should be visible for keyboard navigation.

Acceptance criteria

  • The Accordion component correctly renders all provided items.
  • Only one item's content is visible at a time.
  • Clicking a header toggles its content visibility and closes others if necessary.
  • Clicking an already open header closes it.
  • Space and Enter keys correctly toggle the focused header's content.
  • ArrowDown, ArrowUp, Home, and End keys correctly manage focus between headers.
  • Appropriate ARIA attributes (aria-expanded, aria-controls, role, tabIndex) are used for accessibility.
  • The component is reusable and clearly structured.

Constraints

Constraints & assumptions

  • Use React functional components and hooks.
  • TypeScript is preferred for type safety.
  • You are not allowed to use any external UI component libraries (e.g., Material UI, Ant Design). Build the component from scratch.
  • Focus on core functionality and accessibility; pixel-perfect styling is not the primary goal, but basic visual clarity is expected.
  • Assume the input data structure will always be an array of objects with title (string) and content (string or JSX.Element).

Out of scope

  • Multi-select accordion (where multiple items can be open simultaneously).
  • Nested accordions.
  • Dynamic loading of accordion item content.
  • Complex custom animations beyond basic CSS transitions.

What interviewers evaluate

  • Component Design & Reusability: How well the Accordion and AccordionItem components are structured, how props are passed, and their reusability.
  • State Management: Effectiveness and correctness of state management for handling the single-select behavior and active item tracking.
  • Accessibility (A11y): Correct implementation of ARIA attributes and keyboard navigation for an inclusive user experience.
  • Code Quality: Readability, maintainability, and adherence to best practices (e.g., proper use of hooks, error handling).

Loading editor...