Frontend School

Build a Simple Command Palette

Machine Coding
easy

Build a Simple Command Palette

Easy
React
UI
Search
Filtering
Command Palette

Overview

A command palette is a powerful UI element that allows users to quickly find and execute actions within an application using a search-like interface. It significantly enhances productivity by providing a fast way to access features without navigating menus. In this problem, you will build a basic version of a command palette that displays a list of commands and allows users to filter them by typing.

What you need to do / Task

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

  1. Render an input field: Display a text input where users can type their search queries.
  2. Display commands: Initially, show a predefined list of commands. Each command should display its name.
  3. Filter commands: As the user types into the input field, dynamically filter the displayed commands. The filtering should be case-insensitive and match if the input string is a substring of the command's name.
  4. Handle no results: If no commands match the current search query, display a "No matching commands." message.

UI/UX expectations

  • The command palette should be a self-contained component.
  • The input field should be clearly visible at the top.
  • Below the input, a list of matching commands should be displayed.
  • Each command in the list should be presented clearly (e.g., as a list item or a simple div).
  • Basic styling is expected for readability, but complex visual design is not required. Focus on functionality.

Acceptance criteria

  • An input field is rendered and allows user input.
  • The initial list of MOCK_COMMANDS is displayed when the input is empty.
  • Typing "open" into the input field correctly filters the list to show only commands containing "open" (e.g., "Open File", "Open Project").
  • Typing "file" (or "FILE") correctly filters the list to show "Open File", "Save File As".
  • Typing a query with no matches (e.g., "xyz") displays "No matching commands."
  • Clearing the input field (empty string) restores the full list of commands.

Constraints

Constraints & assumptions

  • You must use React for this implementation.
  • All data (commands) will be provided client-side as a static array. No API calls are needed.
  • The number of commands will be small (e.g., up to 20-30).
  • Filtering should occur client-side.
  • The provided MOCK_COMMANDS array should be used.
  • Focus on functional correctness over pixel-perfect design.

Out of scope

  • Advanced keyboard navigation (e.g., up/down arrow keys to select items, Enter to activate).
  • Fuzzy matching algorithms (simple substring match is sufficient).
  • Executing commands (clicking on a command item doesn't need to do anything, though you can add a console.log for demonstration).
  • Complex state management libraries (Redux, Zustand, etc.). React's built-in hooks are sufficient.

What interviewers evaluate

  • React Fundamentals: Correct use of React components, state, and props.
  • Logical Filtering: Accuracy and efficiency of the client-side search/filter logic.
  • Dynamic UI Rendering: Ability to conditionally render UI elements based on state (e.g., filtered results, "no results" message).
  • Code Structure & Readability: Clean, well-organized, and understandable code.

Loading editor...