Close the earlier lessons and build this from the brief. Dialog.jsx and
Combobox.jsx from this day's lessons are included, with a list of commands.
The brief
- An Open command palette button, and Ctrl+K (or ⌘K) anywhere on the page, open a dialog titled Command palette.
- Inside, a combobox labelled Command lists the commands. Choosing one, with the keyboard or the mouse, closes the dialog and shows Ran: Take a break in a status message that screen readers announce.
- Escape closes the palette, and focus returns to wherever it was.
- Each time the palette opens, the combobox starts empty.
Acceptance checks:
- With the keyboard only: Ctrl+K, type br, Down, Enter. The status says Ran: Take a break, and the dialog is closed.
- After Escape, focus is back on the button that opened it.
- The status message is a live region that exists before anything is run.
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Hint 1: the shortcut
An effect that listens for keydown on window, checks
event.key.toLowerCase() === "k" && (event.ctrlKey || event.metaKey), and calls
preventDefault so the browser's own shortcut doesn't run.
Hint 2: starting empty
Render the combobox only while the palette is open: {open && <Combobox ... />}.
Closing unmounts it, so its state starts fresh next time.
Show one solution
Runs with Vite on your computer: unzip, then
npm install and npm run dev. Check yourself
- What does
showModal()do that rendering a<div>on top doesn't? - In a roving tabindex, which element has
tabIndex={0}? - Why does a combobox keep focus in the input and use
aria-activedescendant?
Completion is saved in this browser only. It does not measure mastery.