Frontend School

Implement a Dynamic Command Palette with Fuzzy Search

Machine Coding
hard

Implement a Dynamic Command Palette with Fuzzy Search

Hard
React
TypeScript
UI/UX
Keyboard Navigation
Fuzzy Search
Modal

Overview

In a modern design and e-commerce platform like Livspace, efficient workflows are paramount. Users often need to quickly access features, navigate sections, or trigger actions without lifting their hands from the keyboard. A robust command palette significantly enhances productivity by providing a central, searchable interface for all available operations. This problem challenges you to build a dynamic command palette component that allows users to quickly find and execute commands using fuzzy search and keyboard navigation.

What you need to do / Task

Your goal is to build a CommandPalette React component with the following functionalities:

  1. Toggle Visibility: The command palette should appear as a modal overlay when Cmd+K (macOS) or Ctrl+K (Windows/Linux) is pressed, and disappear when Escape is pressed, or when clicking outside the palette, or when Cmd/Ctrl+K is pressed again.
  2. Search Functionality: Implement an input field within the palette that allows users to search for commands. The search should use a "fuzzy matching" algorithm, meaning it should match commands even if the query string doesn't perfectly align with the command title (e.g., "del proj" should match "Delete Project"). An initial fuzzyMatch helper is provided, but you are free to enhance it.
  3. Dynamic Command List: Display a list of commands that updates in real-time as the user types. Initially, all commands should be visible.
  4. Keyboard Navigation: Users must be able to navigate the filtered command list using the ArrowUp and ArrowDown keys. The currently selected command should be visually highlighted.
  5. Command Execution:
    • Pressing Enter when a command is selected should execute that command.
    • Clicking on a command with the mouse should also execute it.
    • Commands can be of two types: action (executes a predefined JavaScript function) or navigate (changes the current URL).
  6. Focus Management: When the palette opens, the search input should automatically gain focus. When it closes, focus should ideally return to the element that had focus before the palette opened.

UI/UX expectations

  • The command palette should appear as a modal centered towards the top of the screen, overlaying existing content with a semi-transparent backdrop.
  • It should have a clear search input field at the top.
  • Search results should be displayed in a scrollable list below the input.
  • The active (keyboard-selected) command should have a distinct visual highlight.
  • Basic styling to make it look functional is expected, and a minimal /styles.css is provided to get you started.

Acceptance criteria

  • Pressing Cmd+K (or Ctrl+K) correctly toggles the command palette's visibility.
  • Pressing Escape key closes the command palette.
  • Clicking anywhere outside the command palette modal closes it.
  • Typing into the search input dynamically filters the command list using fuzzy matching.
  • ArrowUp and ArrowDown keys navigate through the filtered list, and the active item is visually highlighted.
  • Pressing Enter on a highlighted command executes its associated action or navigates to the specified URL.
  • Clicking on a command with the mouse also executes it.
  • When the palette opens, the search input automatically receives focus.
  • No console errors or warnings are present during normal operation.

Constraints

Constraints & assumptions

  • The solution must be built using React and TypeScript.
  • All commands will be provided as an in-memory array (no API calls for command data).
  • Performance should be reasonable for up to 100-200 commands.
  • The provided fuzzyMatch utility function can be used as-is or improved upon. The primary focus is on integrating it correctly.
  • Assume a modern browser environment.

Out of scope

  • Implementing a full client-side routing solution (simple window.location.href is sufficient for navigation commands).
  • Deep accessibility features (e.g., full ARIA attribute support beyond basic keyboard navigation).
  • Saving or persisting recent commands.
  • Nested commands or command groups.

What interviewers evaluate

  • Code Structure and Modularity: Clarity, organization, separation of concerns, and effective use of React components and hooks.
  • Type Safety and Robustness: Correct and consistent use of TypeScript for data structures, props, and state.
  • Algorithmic Thinking: Correctness and application of the fuzzy search implementation and efficient list filtering.
  • UI/UX and Interaction Handling: Responsiveness, keyboard navigation, focus management, and adherence to specified interactions.
  • Problem-Solving Approach: How the candidate breaks down the problem, handles edge cases, and designs the solution.

Loading editor...