Frontend School

Interactive Command Palette

Machine Coding
easy

Interactive Command Palette

Easy
Command Palette
React
Frontend
UI
Keyboard Events

Overview

Modern web applications often provide a "command palette" — a powerful modal interface that allows users to quickly search for and execute actions using just their keyboard. This enhances productivity by reducing reliance on mouse navigation. In this problem, you will build a simplified command palette component for a hypothetical dashboard application.

This exercise assesses your ability to manage component state, handle keyboard events, and implement interactive UI elements in React.

What you need to do / Task

Your goal is to implement a CommandPalette component that fulfills the following requirements:

  1. Toggle Visibility: The command palette should initially be hidden. It must appear when the user presses Ctrl+K (or Cmd+K on macOS) anywhere on the page.
  2. Display Commands: Once open, display a list of predefined commands (mock data is provided in App.tsx).
  3. Search/Filter: As the user types into the input field, the list of commands should dynamically filter to show only those whose names match the search term (case-insensitive).
  4. Keyboard Navigation: Allow users to navigate the filtered list using the Up and Down arrow keys. The currently highlighted item should have a distinct visual style.
  5. Execute Command: When a command is highlighted and the user presses Enter, the associated action for that command should be executed (for this problem, a simple console.log is sufficient). After execution, the palette should close.
  6. Close Palette: The palette should also close if the user presses the Escape key.

UI/UX expectations

  • The command palette should appear as an overlay, centered on the screen, with a semi-transparent background behind it.
  • It should contain an input field at the top for searching and a scrollable list of commands below.
  • The highlighted command item should be clearly distinguishable (e.g., different background color).
  • The UI should be functional and responsive to user input.

Acceptance criteria

  • The command palette correctly appears and disappears with Ctrl+K / Cmd+K.
  • Typing in the input field filters the displayed commands in real-time.
  • Up and Down arrow keys correctly change the highlighted command item.
  • Pressing Enter on a highlighted command executes its action and closes the palette.
  • Pressing Escape closes the palette without executing any command.
  • The input field automatically gains focus when the palette opens.

Constraints

Constraints & assumptions

  • Use React functional components and hooks.
  • The provided mock data for commands (MOCK_COMMANDS) should be used.
  • No external UI libraries (e.g., Material-UI, Ant Design) are allowed; use plain CSS for styling.
  • Focus on core functionality; pixel-perfect design is not required.
  • The number of commands will be small (less than 20).

Out of scope (optional)

  • Complex command arguments or dynamic command generation.
  • Persistence of recent commands or user preferences.
  • Advanced accessibility features beyond basic keyboard navigation.
  • Animations or transitions for opening/closing the palette.

What interviewers evaluate

  • React Fundamentals: Correct use of state, effects, and refs to manage component behavior.
  • Event Handling: Proficiency in handling global and component-specific keyboard and input events.
  • Algorithmic Thinking: Efficient filtering and navigation logic for the command list.
  • Code Structure: Clean, modular, and readable component design and separation of concerns.

Loading editor...