Frontend School

Interactive Context Menu Component

Machine Coding
easy

Interactive Context Menu Component

Easy
UI
React
Context Menu
Event Handling
Component Design

Overview

Context menus are fundamental UI elements across many applications, providing quick access to relevant actions based on user interaction with a specific element. This problem evaluates your ability to build a reusable, accessible context menu component in React, demonstrating core UI development skills including state management, event handling, and basic positioning.

What you need to do / Task

Your goal is to implement a custom context menu that appears when a user right-clicks on a designated target area. Specifically, you need to:

  1. Create a ContextMenu component: This component should be responsible for rendering the menu items and managing its own visibility and position.
  2. Trigger on Right-Click: Implement logic in the parent App component to show the ContextMenu when the onContextMenu event fires on the target-area div.
  3. Position the Menu: The context menu should appear precisely at the mouse cursor's location when it's opened.
  4. Display Menu Items: The ContextMenu component should accept a list of menu items (each with a label and an action) and render them.
  5. Handle Menu Item Clicks: When a user clicks on a menu item, its associated action should be executed (e.g., an alert or console.log).
  6. Close Menu Logic: The menu should automatically close when the user clicks anywhere outside the menu or presses the Escape key.

UI/UX expectations

  • The context menu should appear directly at the mouse cursor's position when a right-click occurs on the target area.
  • The menu should visually overlay other content, suggesting it's an ephemeral interaction.
  • Menu items should be vertically stacked and visually distinguishable.
  • Clicking a menu item should trigger its associated action function.
  • The menu must disappear when the user clicks anywhere outside the menu boundaries or presses the Escape key.
  • The browser's default context menu must be prevented from appearing when right-clicking the target area.

Acceptance criteria

  • Right-clicking on the target-area successfully opens the custom context menu.
  • The custom context menu appears at the exact coordinates of the mouse cursor.
  • Clicking on any menu item within the context menu executes its predefined action (e.g., shows an alert).
  • Clicking anywhere on the document outside the open context menu causes it to close.
  • Pressing the Escape key while the context menu is open causes it to close.
  • The browser's default context menu does not appear when right-clicking the target-area.

Constraints

Constraints & assumptions

  • You must build the solution using React (functional components and hooks).
  • No external UI component libraries (e.g., Material-UI, Ant Design) are allowed; all UI elements must be built from scratch.
  • The menu items will be simple objects with id, label, and action properties, as provided in the starter code.
  • The target element for the context menu will be a simple div.
  • Focus on core functionality; perfect pixel-perfect positioning across all browser edge cases is not required, but general proximity to the cursor is.

Out of scope

  • Nested context menus or sub-menus.
  • Advanced keyboard navigation within the context menu items (beyond Escape to close).
  • Complex styling or animations (basic functional styling is sufficient).
  • Accessibility features beyond preventing default browser behavior and basic semantic HTML.

What interviewers evaluate

  • Component Design: Your ability to break down the problem into logical, reusable components and manage their state effectively.
  • Event Handling: Correct and efficient implementation of DOM event listeners for contextmenu, click (for outside clicks), and keydown (for Escape).
  • State Management: Appropriate use of React state and hooks to control component visibility and dynamic positioning.
  • Clean Code & React Best Practices: Readability, maintainability, and adherence to common React patterns and principles.

Loading editor...