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.
- File Explorer View: Render a mock list of file and folder items. Each item should be right-clickable.
- 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.
- 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.
- 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.
- 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.
- 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.