KatInterview

Build a Accessible, Controllable Select Menu in React

Data Structure DesignModel answer
HardModel answerData Structure Designreactfrontendaccessibility

Last updated

Design and implement a reusable select-style menu in React with controlled and uncontrolled state, keyboard support, ARIA semantics, and a plan for production hardening.

Build a Reusable Select Menu

You are asked to build a select-style menu component in React (TypeScript preferred) that several product teams will share. Implement the core behavior in code, then describe what you would add for production use.

Required behavior

  • Show a trigger button and a popup list of selectable items.
  • The caller supplies the items, including each item's display label, its value, and whether it is disabled, along with an onChange callback.
  • Both the selected value and the open/closed state must work in controlled and uncontrolled modes.
  • The menu must close when the user picks an item, clicks anywhere outside the component, or presses Escape.
  • Provide basic keyboard navigation.
  • Make it accessible enough for real use: sensible roles, focus handling, and ARIA attributes.

Discussion part

Explain how you would extend the component for:

  • Positioning near viewport edges
  • Scrollable ancestors and clipping
  • Portals
  • Very long lists (virtualization)
  • Theming and design-system integration
  • Testing

What a strong answer covers

  • A clear props contract with sensible defaults
  • Correct rules for who owns each piece of state
  • Disabled items that cannot be reached or chosen
  • Predictable focus on open and close
  • Honest trade-offs and edge cases

Hints

Hint 1

Write one small reusable hook for 'value owned by the caller or by the component' and use it for both the selected value and the open state. Decide how you tell 'controlled but empty' apart from 'uncontrolled'.

Hint 2

Follow the WAI-ARIA listbox pattern: a button with aria-haspopup and aria-expanded, a container with role=listbox, and items with role=option plus aria-selected and aria-disabled. Consider tracking the highlighted item by index and exposing it with aria-activedescendant.

Hint 3

Write a helper that finds the next enabled index in a direction with wrapping, and reuse it for ArrowUp, ArrowDown, Home and End. For outside clicks, listen for pointerdown on the document and check refs for both the trigger and the list.