Frontend School

Advanced Command Palette with Hierarchical Navigation

Machine Coding
hard

Advanced Command Palette with Hierarchical Navigation

Hard
React
TypeScript
UI
Search
Accessibility
State Management

Overview

Meesho's internal tools are crucial for managing our vast catalog, seller operations, and customer orders. As we scale, power users need increasingly efficient ways to navigate and execute actions. A unified, intelligent command palette can significantly boost productivity by providing quick access to deep functionalities across different modules, minimizing clicks and context switching.

This problem challenges you to build a sophisticated command palette component for our internal platform. It should not only allow users to search and execute commands but also intelligently handle hierarchical command structures, enabling users to drill down into categories and perform actions within specific contexts.

What you need to do / Task

Your primary task is to implement a reusable CommandPalette React component that offers an intuitive and powerful user experience. Specifically, you must:

  1. Implement Core Palette Functionality: Create a modal-based command palette that can be toggled open and closed (e.g., via a global keyboard shortcut like Cmd+K or Ctrl+K, and by clicking outside or pressing Esc).
  2. Hierarchical Command Navigation: Support a nested structure of commands. Some commands can be 'categories' that, when selected, reveal a new set of sub-commands. Users should be able to navigate back up the hierarchy.
  3. Fuzzy Search & Filtering: Implement a robust fuzzy search algorithm that filters commands based on the user's input. The search should apply to command titles and be effective across all visible commands in the current hierarchy level.
  4. Keyboard Accessibility: Ensure full keyboard navigation support. Users must be able to:
    • Use ArrowUp and ArrowDown to navigate through the list of filtered commands.
    • Use Enter to select a command (execute an action command or drill into a category command).
    • Use Backspace or Delete to navigate up one level in the command hierarchy when inside a category (only if the search input is empty).
    • Use Escape to close the palette from any state.
  5. Command Execution: When an action command is selected and Enter is pressed, an onExecute callback should be triggered with the command's ID.

UI/UX expectations

  • The command palette should appear as an overlay (modal) centered on the screen.
  • It should feature a prominent input field at the top for search queries.
  • Below the input, a list of matching commands should be displayed.
  • The currently selected command in the list must have a clear visual highlight.
  • When navigating into categories, a 'breadcrumb' or similar visual indicator should show the current path (e.g., Catalog > Products).
  • The UI should be responsive and provide a smooth experience for keyboard users.

Acceptance criteria

  • The command palette opens and closes correctly via the keyboard shortcut (Cmd+K/Ctrl+K) and Esc key.
  • Typing into the search input accurately filters the displayed commands using fuzzy matching.
  • Navigating into a category command displays its sub-commands, and navigating back up works as expected.
  • All specified keyboard interactions (ArrowUp, ArrowDown, Enter, Backspace/Delete, Esc) function correctly for navigation, selection, and closing.
  • Selecting and 'executing' an action command triggers the onExecute prop with the correct command ID.
  • The UI provides clear visual feedback for selected items and hierarchical path.
  • The component is performant, even with a moderate number of commands (e.g., 100-200 visible).

Constraints

Constraints & assumptions

  • Data Structure: The command data will be provided as a nested array of Command objects. You can assume this structure is immutable during the component's lifecycle.
  • Command Count: The total number of unique commands will not exceed ~500, with nesting up to 3-4 levels deep.
  • Environment: The solution should be implemented using React and TypeScript within a standard browser environment.
  • Styling: Basic CSS for layout and visual feedback is sufficient. No need to integrate complex styling libraries or design systems.
  • No Backend: All command data is client-side. There is no need to fetch commands from an API.

Out of scope

  • Persistence: Storing recently used commands or user preferences.
  • Complex Arguments: Implementing forms or complex argument input for commands.
  • Advanced Accessibility: Full screen reader support beyond basic keyboard navigation.
  • Theming: Support for light/dark mode or custom themes.

What interviewers evaluate

  • Architectural Design: How well the component is structured, separation of concerns, and state management strategies for a complex interactive UI.
  • Algorithmic Proficiency: Efficiency and correctness of the fuzzy search and filtering logic, especially for hierarchical data.
  • UI/UX Implementation: Attention to detail in keyboard navigation, visual feedback, and overall user experience.
  • Code Quality & Maintainability: Readability, type safety (TypeScript usage), error handling, and adherence to best practices for React components.

Loading editor...