Frontend School

Implement a Command Palette with Keyboard Navigation

Machine Coding
medium

Implement a Command Palette with Keyboard Navigation

Medium
Command Palette
Keyboard Navigation
UI
React
Accessibility

Overview

Modern applications often feature a command palette, a powerful interface that allows users to quickly search for and execute actions without navigating through complex menus. This significantly boosts productivity and enhances the user experience, making applications more accessible and efficient. For instance, tools like VS Code or Notion heavily rely on command palettes for quick feature access.

This problem challenges you to build a foundational command palette component, focusing on core keyboard navigation and filtering capabilities, simulating a feature that would be essential in a rich client-side application like a document editor.

What you need to do / Task

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

  1. Toggle Visibility: The palette should appear as a modal overlay when activated and close when dismissed.
  2. Keyboard Shortcut: Implement a global keyboard shortcut (Cmd/Ctrl + K) to open the command palette.
  3. Command Listing: Display a predefined static list of commands within the palette.
  4. Search/Filtering: As a user types into an input field within the palette, dynamically filter the displayed commands based on the search term (case-insensitive substring match).
  5. Keyboard Navigation:
    • Allow users to navigate through the filtered list of commands using the ArrowUp and ArrowDown keys.
    • The selection should visually highlight the currently focused command.
    • Navigation should wrap around (e.g., pressing ArrowUp from the first item moves to the last, and ArrowDown from the last item moves to the first).
  6. Execute Command: Pressing the Enter key while a command is selected should "execute" it. For this problem, "execution" means logging the command's id and name to the console.
  7. Dismiss Palette: The palette should close when the Escape key is pressed or when the user clicks outside the palette modal.

UI/UX expectations

  • The command palette should appear as a translucent overlay (modal) centered on the screen, obscuring the content beneath.
  • It should contain an input field at the top for searching, followed by a scrollable list of commands.
  • The currently selected command in the list must have a clear visual highlight (e.g., a different background color).
  • The input field should automatically gain focus when the palette opens, ready for user input.

Acceptance criteria

  • The command palette opens when Cmd/Ctrl + K is pressed anywhere on the page.
  • The command palette closes when Escape is pressed or when a click occurs outside the palette modal.
  • Typing into the input field filters the displayed command list in real-time.
  • ArrowUp and ArrowDown keys correctly navigate through the filtered commands, with visual selection feedback.
  • Navigation wraps around from first to last item and vice-versa.
  • Pressing Enter on a selected command logs its id and name to the browser console.
  • The input field is focused automatically upon opening the palette.

Constraints

Constraints & assumptions

  • You should use React for this implementation.
  • The list of commands is static and provided. You do not need to fetch them dynamically.
  • Focus on the core UI and interaction logic. No complex backend integration or routing is required.
  • The problem focuses on a single command palette instance.
  • Assume a modern browser environment.

Out of scope

  • Advanced fuzzy searching algorithms (simple substring match is sufficient).
  • Persistence of user settings or command history.
  • Full ARIA accessibility attributes beyond basic keyboard navigation and focus management.
  • Complex state management libraries (e.g., Redux, Zustand) are not necessary; React's built-in hooks are sufficient.

What interviewers evaluate

  • Correctness of Keyboard Handling: Accurate implementation of global and component-specific keyboard event listeners.
  • State Management: Effective and idiomatic use of React state and hooks to manage component visibility, input, and selection.
  • UI Responsiveness: The UI updates correctly and smoothly in response to user input and keyboard navigation.
  • Code Quality: Clean, readable, well-structured, and maintainable React code following best practices.
  • Problem Decomposition: Ability to break down the problem into manageable parts and integrate them effectively.

Loading editor...