Frontend School

Dynamic Context Menu for Product Categories

Machine Coding
medium

Dynamic Context Menu for Product Categories

Medium
React
TypeScript
UI
Event Handling
Context Menu

Overview

At Blinkit, operations teams frequently interact with various entities like product categories, SKUs, and orders. Quick, contextual actions are crucial for efficient workflows. This problem focuses on building a dynamic context menu for a list of product categories, allowing users to perform actions directly relevant to the selected item. This exercise will test your ability to handle UI events, manage component state, and create reusable components.

What you need to do / Task

Your primary goal is to implement a React component that displays a list of product categories and provides a dynamic context menu when a category item is right-clicked.

  1. Render Product Categories: Display a list of mock product categories.
  2. Implement Context Menu Trigger: When a user right-clicks on any product category item, a context menu should appear.
  3. Dynamic Actions: The context menu should display a set of actions (e.g., "Edit Category", "Delete Category", "View Products") relevant to the clicked category.
  4. Positioning: The context menu must appear precisely at the mouse cursor's (x, y) coordinates where the right-click occurred.
  5. Menu Interaction:
    • Clicking any action within the context menu should perform a mock action (e.g., console.log the action and the category it was performed on) and then close the menu.
    • Clicking anywhere outside the context menu should close it without performing any action.
  6. Prevent Default: Ensure the browser's native context menu does not appear when right-clicking on a category item.

UI/UX expectations

  • Category List: A simple vertical list of category names. Basic styling is sufficient to differentiate items.
  • Context Menu: A small, visually distinct pop-up container (e.g., a div or ul) containing a list of actions. Each action should be clickable.
  • Visibility: The context menu should only be visible when triggered by a right-click.
  • Accessibility (Basic): Focus on mouse interactions. Keyboard navigation for the menu itself is out of scope.

Acceptance criteria

  • [ ] A list of mock product categories is rendered on the screen.
  • [ ] Right-clicking any category item successfully opens a context menu.
  • [ ] The context menu's top-left corner is positioned at the exact clientX and clientY of the mouse click.
  • [ ] The context menu displays different actions (e.g., "Edit", "Delete", "View") for the selected category.
  • [ ] Clicking an action within the menu logs the action and the associated category to the console, and then closes the menu.
  • [ ] Clicking anywhere on the document outside the context menu closes it.
  • [ ] The browser's default context menu is suppressed when right-clicking a category item.

Constraints

Constraints & assumptions

  • You must use React (TypeScript) for this implementation.
  • You are not allowed to use any external UI component libraries (e.g., Material UI, Ant Design). Build all UI elements from scratch.
  • Focus on functional components and React Hooks (useState, useEffect, useRef).
  • Assume a flat list of categories; no nested categories or complex data structures.
  • The actions displayed in the context menu can be hardcoded for simplicity, or dynamically generated based on a simple logic.

Out of scope

  • Complex state management solutions (e.g., Redux, Zustand).
  • Backend integration or API calls.
  • Advanced accessibility features (e.g., full keyboard navigation for the context menu).
  • Extensive styling or responsive design for various screen sizes.

What interviewers evaluate

  • Event Handling: Correct and robust implementation of DOM event listeners, including onContextMenu and global click handlers for dismissal.
  • State Management: Effective use of React state and refs to manage component visibility, position, and data flow.
  • Component Design: Ability to create modular, reusable, and well-structured React components (e.g., a dedicated ContextMenu component).
  • Code Quality: Clean, readable, and idiomatic TypeScript code adhering to React best practices.

Loading editor...