Frontend School

Hierarchical Context Menu for File Explorer

Machine Coding
hard

Hierarchical Context Menu for File Explorer

Hard
React
TypeScript
UI/UX
Accessibility
Context Menu
DOM Manipulation

Overview

In modern productivity applications, intuitive and efficient interaction paradigms are crucial. A robust context menu system significantly enhances user experience by providing quick access to relevant actions based on the selected element. This problem challenges you to build a sophisticated, accessible, and dynamically positioned hierarchical context menu, simulating a file explorer interface.

What you need to do / Task

Your primary task is to implement a reusable and accessible context menu component that supports nested sub-menus and dynamic content, integrated into a simplified file explorer view.

  1. File Explorer View: Render a mock list of file and folder items. Each item should be right-clickable.
  2. Context Menu Triggering: When a user right-clicks (or Ctrl+Click on Mac) on any file or folder item, a context menu should appear near the cursor's position.
  3. Dynamic Menu Content: The menu items displayed should be dynamic based on the type of item (file vs. folder) that was right-clicked. For instance, a folder might have a "New Folder" option, while a file might not.
  4. Nested Sub-menus: Implement support for hierarchical (nested) sub-menus. For example, a "Move To" or "Copy To" action might open a sub-menu listing available target folders.
  5. Menu Actions: Clicking a menu item should execute a placeholder action (e.g., alert() for "Delete", console.log() for "Move To [Folder Name]") and close all open menus.
  6. Dismissal: The context menu (and any open sub-menus) must close when the user clicks anywhere outside the menu, or presses the Escape key.

UI/UX expectations

  • File/Folder List: Display a simple, vertically stacked list of file and folder names. Basic styling to distinguish them is sufficient.
  • Context Menu Appearance: The menu should be visually distinct (e.g., a subtle border, background color, shadow) and appear as an overlay on top of other content.
  • Positioning: The menu should appear close to where the right-click occurred. Crucially, it must intelligently position itself to remain entirely within the viewport. If opening normally would push it off-screen (e.g., too close to the right or bottom edge), it should adjust its position (e.g., open to the left or above).
  • Nested Sub-menu Display: When a sub-menu is triggered (e.g., by hovering over a parent item), it should appear adjacent to its parent item, typically to the right. Similar to the main menu, sub-menus must also respect viewport boundaries and adjust their position if necessary.
  • Accessibility (Keyboard Navigation):
    • When the context menu opens, the first visible item should receive focus.
    • Up and Down arrow keys should navigate between menu items.
    • Enter or Space key should activate the focused menu item.
    • Right arrow key should open a sub-menu for the focused item (if it has one) and focus its first item.
    • Left arrow key should close an open sub-menu and return focus to its parent item.
    • Escape key should close the currently active sub-menu, or the main menu if no sub-menu is open.

Acceptance criteria

  • Right-clicking a file/folder item correctly triggers the context menu.
  • Menu content dynamically updates based on the clicked item type.
  • Nested sub-menus open and close correctly on hover and keyboard interaction.
  • The context menu and all sub-menus remain fully visible within the viewport, adjusting their position near screen edges.
  • Clicking outside the menu or pressing Escape closes the entire context menu system.
  • Keyboard navigation (Up/Down/Left/Right arrows, Enter/Space, Esc) functions as specified in the UI/UX expectations.
  • Clicking or activating a menu item executes its placeholder action and closes all open menus.

Constraints

Constraints & assumptions

  • You should use React (functional components with Hooks) and TypeScript.
  • The solution should run in a modern browser environment (Chrome, Firefox, Edge).
  • You are expected to build the context menu component from scratch; do not use external UI libraries for the menu itself.
  • Use the provided mock file system data structure for demonstration.
  • Focus on core functionality and accessibility; pixel-perfect styling is not the primary concern within the time limit.
  • The time budget for this problem is approximately 45 minutes for an L5 candidate.

Out of scope

  • Persistence of file system changes (e.g., actually deleting or renaming files).
  • Complex drag-and-drop interactions for file management.
  • Deep integration with backend APIs.
  • Support for touch gestures beyond the standard right-click abstraction.

What interviewers evaluate

  • Component Design & Modularity: How well the context menu logic is encapsulated, reusable, and structured into logical components.
  • State Management: Effective and efficient use of React state and effects to manage menu visibility, position, active items, and focus.
  • Event Handling: Correct and robust implementation of DOM event listeners for mouse clicks, right-clicks, and keyboard interactions.
  • Accessibility: Adherence to accessibility guidelines, particularly for keyboard navigation and focus management, demonstrating a user-centric approach.
  • Problem Solving: Ability to tackle complex UI challenges such as dynamic content, nested interactions, and intelligent viewport-aware positioning.

Loading editor...