Frontend School

Dynamic and Accessible Context Menu

Machine Coding
medium

Dynamic and Accessible Context Menu

Medium
React
TypeScript
UI
Context Menu
Accessibility
Event Handling

Overview

In Livspace's intuitive design platform, users frequently interact with various elements—be it furniture pieces, wall sections, or design components. Providing quick, context-sensitive actions for these elements is paramount for an efficient workflow. A robust and accessible context menu significantly enhances user productivity by offering relevant operations directly at the point of interaction. This problem challenges you to build a reusable and accessible ContextMenu component that mirrors this critical platform functionality.

What you need to do / Task

Your primary task is to develop a functional and accessible ContextMenu component in React/TypeScript. Specifically, you must:

  1. Create a ContextMenu component: This component should be a standalone, reusable UI element.
  2. Trigger Mechanism: The menu should appear when a designated trigger element (e.g., a div) is right-clicked.
  3. Dynamic Content: The ContextMenu component must accept an array of menu item objects, each specifying a label and an action to be performed.
  4. Positioning: The menu should appear at the exact (x, y) coordinates of the mouse click that triggered it.
  5. Dismissal Logic: The menu must close when:
    • A menu item is clicked.
    • The user clicks anywhere outside the menu.
    • The Escape key is pressed.
  6. Keyboard Navigation: Implement basic keyboard accessibility:
    • Users should be able to navigate between menu items using the ArrowUp and ArrowDown keys.
    • Pressing Enter when an item is focused should trigger its associated action and close the menu.

UI/UX expectations

  • The context menu should be visually distinct (e.g., a simple vertical list of buttons or divs with a border/shadow).
  • Menu items should be clearly clickable and provide visual feedback on hover/focus.
  • The menu should overlay other content without disrupting the layout.
  • Ensure basic accessibility: The menu and its items should have appropriate ARIA roles (role="menu", role="menuitem") to be perceivable and operable by assistive technologies.

Acceptance criteria

  • Right-clicking the designated target-area successfully opens the context menu.
  • The opened menu is positioned accurately at the mouse click coordinates.
  • All provided menu items are rendered correctly within the menu.
  • Clicking any menu item executes its associated action and closes the menu.
  • Clicking outside the context menu area correctly closes the menu.
  • Pressing the Escape key closes the context menu when it is open.
  • ArrowUp and ArrowDown keys allow navigation between menu items, visually indicating the focused item.
  • Pressing Enter on a focused menu item triggers its action and closes the menu.
  • The ContextMenu component is modular and reusable, accepting items, x, y, onClose, and onItemClick as props.

Constraints

Constraints & assumptions

  • You must use React with TypeScript.
  • No external UI component libraries (e.g., Material UI, Ant Design) are allowed. You should implement the context menu using native HTML elements and CSS.
  • Assume a single trigger element on the page for simplicity. The menu will always open relative to this trigger.
  • Menu items will be simple text labels. No support for icons or nested sub-menus is required.
  • The actions associated with menu items can be simple console logs for demonstration purposes.

Out of scope

  • Complex positioning logic (e.g., ensuring the menu stays within viewport boundaries, handling scroll).
  • Advanced menu types (e.g., cascading sub-menus, disabled items, dividers).
  • Dynamic menu items based on the specific element right-clicked (assume static items for this problem).

What interviewers evaluate

  • Component Design & Reusability: How well the ContextMenu component is structured and its reusability across different parts of an application.
  • State Management & Event Handling: Proficiency in managing UI state (e.g., menu visibility, position) and handling various DOM events (contextmenu, click, keydown).
  • Accessibility Implementation: Attention to keyboard navigation and ARIA attributes for an inclusive user experience.
  • Clean Code & TypeScript Usage: Adherence to best practices in code organization, readability, and effective use of TypeScript for type safety.

Loading editor...