Frontend School

Interactive Command Palette

Machine Coding
medium

Interactive Command Palette

Medium
React
TypeScript
UI
Keyboard Events
Filtering

Overview

A command palette is a powerful UI pattern, popularized by tools like VS Code and Figma, that allows users to quickly access features and navigate an application using keyboard commands and fuzzy search. In complex developer tools like Browserstack, providing an efficient way for users to trigger actions or find specific settings can significantly enhance productivity. This problem challenges you to build a functional command palette component.

What you need to do / Task

Your goal is to implement a reusable CommandPalette component that offers a streamlined user experience for executing predefined actions.

  1. Triggering the Palette: The command palette should appear as an overlay when the user presses Cmd + K (on Mac) or Ctrl + K (on Windows/Linux) anywhere on the page.
  2. Display Commands: Upon opening, the palette should display a list of all available commands. You'll use provided mock data for these commands.
  3. Search and Filter: As the user types into an input field within the palette, the displayed list of commands should dynamically filter to show only those matching the input. The search should be case-insensitive.
  4. Keyboard Navigation:
    • Users should be able to navigate through the filtered list of commands using the ArrowUp and ArrowDown keys.
    • The currently selected command should be visually highlighted.
    • Navigation should wrap around (e.g., pressing ArrowUp from the first item goes to the last, and ArrowDown from the last item goes to the first).
  5. Execute Command: Pressing the Enter key while a command is selected should "execute" that command. For this exercise, execution means logging the command's name to the console and then closing the palette.
  6. Close Palette: The palette should close when:
    • The user presses the Escape key.
    • A command is successfully executed.

UI/UX expectations

  • The command palette should appear as a modal or overlay, centrally positioned on the screen, obscuring the background content slightly (e.g., with a semi-transparent overlay).
  • It should contain a prominent input field at the top for searching.
  • Below the input, a scrollable list of commands should be displayed.
  • The currently selected command (via keyboard navigation) must have a clear visual highlight (e.g., different background color).
  • Basic styling to ensure readability and a clean appearance is expected.
  • Consider basic accessibility for keyboard users.

Acceptance criteria

  • The command palette opens on Cmd/Ctrl + K.
  • The command palette closes on Escape or after command execution.
  • Typing in the input field filters the displayed command list dynamically and case-insensitively.
  • ArrowUp and ArrowDown keys correctly navigate and highlight commands in the filtered list, with wrapping behavior.
  • Pressing Enter on a highlighted command logs its name to the console and closes the palette.
  • The UI is responsive to user input and keyboard actions.
  • The component is well-structured and uses React state effectively.

Constraints

Constraints & assumptions

  • You are required to use React with TypeScript.
  • The list of commands will be provided as a static array within App.tsx (mock data). Assume this list is relatively small (e.g., < 50 commands).
  • Filtering should be a simple case-insensitive substring match.
  • No external UI libraries (e.g., Material UI, Ant Design) are allowed; use plain CSS or CSS modules.
  • The application does not need to handle complex routing or backend interactions for command execution.

Out of scope

  • Implementing a complex fuzzy search algorithm (simple substring match is sufficient).
  • Persistence of recent commands or user preferences.
  • Dynamic loading of commands from an API.
  • Full accessibility audit with ARIA roles beyond basic keyboard navigation.
  • Complex animations or transitions.
  • Clicking outside the palette to close it.

What interviewers evaluate

  • Component Design & State Management: How well the CommandPalette component is structured, and how effectively React state and props are managed.
  • Event Handling: Correct and robust implementation of keyboard event listeners for opening, closing, navigation, and execution.
  • Filtering Logic: Accuracy and efficiency of the command filtering mechanism.
  • UI Responsiveness & Interactivity: The UI updates correctly and fluidly in response to user input and keyboard actions.
  • Code Quality: Readability, maintainability, adherence to TypeScript best practices, and error handling.

Loading editor...