Frontend School

Dynamic Context Menu for List Items

Machine Coding
easy

Dynamic Context Menu for List Items

Easy
React
UI
Context Menu
Event Handling
State Management

Overview

In modern web applications, context menus provide a quick and intuitive way for users to perform actions on specific elements without navigating away. For instance, in a file management system or a data table, right-clicking an item reveals a set of relevant operations. This problem challenges you to build a dynamic context menu for a list of items, replicating a common UI pattern found across many applications.

What you need to do / Task

Your task is to implement a React component that displays a list of items. Each item should be interactable via a custom context menu.

  1. Display a List of Items: Render a simple list of items, each having a unique ID and a name. You can use mock data provided in App.tsx.
  2. Implement Context Menu Trigger: When a user right-clicks (triggers contextmenu event) on any list item, a custom context menu should appear.
  3. Dynamic Menu Actions: The context menu should display a predefined set of actions (e.g., "Edit", "Delete", "Share"). These actions should be generic for all items.
  4. Positioning: The context menu must appear precisely at the mouse cursor's coordinates when it is triggered.
  5. Dismissal:
    • The context menu should disappear when the user clicks anywhere outside the menu itself.
    • The context menu should also disappear when the Escape key is pressed.
  6. Action Handling: When an action within the context menu is clicked, an alert should be triggered displaying the action name and the ID of the item on which the context menu was opened (e.g., "Action: Edit on Item ID: 1").

UI/UX expectations

  • List Layout: Display items as a simple vertical list (e.g., using ul/li or divs).
  • Context Menu Appearance: The context menu should be a simple, visually distinct overlay (e.g., a div with a background color and border) containing the action buttons/links.
  • Positioning: The menu should appear directly under the mouse cursor.
  • Interaction Feedback: Clicking an action should trigger the specified alert.

Acceptance criteria

  • A list of items is rendered on the screen.
  • Right-clicking an item displays a context menu.
  • The context menu appears at the exact mouse cursor position.
  • The context menu contains the specified actions ("Edit", "Delete", "Share").
  • Clicking an action in the menu triggers an alert with the action name and the ID of the associated item.
  • Clicking anywhere outside the context menu (including other list items or empty space) closes the menu.
  • Pressing the Escape key closes the context menu.

Constraints

Constraints & assumptions

  • You must use React functional components and Hooks.
  • No external UI libraries (e.g., Material-UI, Ant Design) or context menu-specific libraries are allowed. Implement the context menu logic from scratch using basic HTML, CSS, and React.
  • Basic CSS styling is acceptable to make the UI presentable, but complex animations or elaborate themes are not required.
  • The application should run in a standard browser environment.
  • The mock data for items will be provided in App.tsx.

Out of scope

  • Accessibility features beyond basic keyboard dismissal (e.g., aria attributes for screen readers, tab navigation within the menu).
  • Nested context menus or sub-menus.
  • Saving or persisting changes from actions (e.g., actually deleting an item).
  • Responsiveness for different screen sizes.

What interviewers evaluate

  • Correctness of Context Menu Logic: Proper handling of contextmenu, click, and keydown events for display, positioning, and dismissal.
  • React State Management: Effective use of useState and useEffect hooks to manage menu visibility, position, and the currently active item.
  • Component Structure: Clean and modular component design, potentially separating the context menu logic.
  • Event Handling: Correct implementation of event listeners and prevention of default browser behaviors.
  • Code Quality: Readability, maintainability, and adherence to React best practices.

Loading editor...