30
CMD+K Search Modal Tutorial (Part 1)
Preview the live demo here.
In this tutorial, we will build a ⌘K Search Modal, which is what it sounds like - a modal for searching content that can be opened by the ⌘K keyboard shortcut. The tutorial will be comprised of two parts:
Part 2 will be released to the public on December 31st, one week from when Part 1 (this article) is published.
If you are reading this after both have been released already but want to practice building from scratch according to a list of requirements, try to force yourself to spend at least a week on Part 1 before looking at Part 2.
Otherwise, feel free to skip ahead to Part 2 to read the walkthrough of how I built this ⌘K Search Modal.
For reference, here are some ⌘K Search Modals built by others:
We're going to be building a simple version of the ⌘K Search Modal with the following requirements:
⌘K
keyboard shortcut opens the modalThe end goal is a module that exports components SearchModalProvider and SearchModalToggle that clients can use to render a ⌘K Search Modal. Internally, it will render a component called SearchModal.
Clients (apps that depend this module) can use these components by:
Clone or fork the Github repo for the skeleton code.
git clone https://github.com/rasreee/cmdk-search-tutorial-skeleton.git
From project root, install the dependencies, and run the app.
yarn install && yarn start
Given the skeleton code, implement SearchModalProvider, SearchModalToggle, and SearchModal.
Edit SearchModalProvider.tsx to implement SearchModalProvider according to the following requirements:
Edit SearchModalToggle.tsx to implement the toggle button component according to the following requirements:
-
⌘K
keyboard shortcut is pressed - Button is clicked
For the left-most icon, you can use any icon you like. Otherwise, use the
search
icon from heroicons.com.Edit SearchModal.tsx to implement the search modal according to the following requirements:
If you run into any issues with the tutorial, please feel free to open an issue against the Github repo.
30