How-to
Website
Web app
Desktop / phone
Add keyboard shortcuts and a command palette
An app a reader uses every day earns shortcuts: a key to save, and one key that opens a list of every command, which they filter by typing and run with Enter. This page answers Ctrl+S (⌘S on a Mac) and opens its palette on Ctrl+K (⌘K):
Needs: nothing besides the page. Paste it into a file under src/pages/ and open /notes.
type Command { id: String, label: String, keys: String }
const COMMANDS: [Command] = [
Command(id: "new", label: "New note", keys: "mod+n"),
Command(id: "save", label: "Save", keys: "mod+s"),
Command(id: "dark", label: "Switch to dark", keys: ""),
Command(id: "light", label: "Switch to light", keys: ""),
]
page Notes(path: "/notes", title: "Notes", description: "Notes, with a command palette on Ctrl+K.") {
state open = false
state search = ""
state said = "Press Ctrl+K (⌘K on a Mac) for commands."
derived shown = COMMANDS.filter(c => c.label.toLowerCase().includes(search.toLowerCase()))
action run(id: String) {
open = false
search = ""
if id == "dark" { setTheme("dark") }
if id == "light" { setTheme("light") }
said = "Ran “{COMMANDS.find(c => c.id == id)?.label ?? id}”."
}
on key("mod+k", e) {
e.preventDefault()
open = true
}
on key("mod+s", e) {
e.preventDefault()
run("save")
}
Heading("Notes").h1
Text(said)
Button("Commands") { on click { open = true } }
Modal(visible: open, title: "Commands") {
on close { open = false }
Input(bind: search, label: "Find a command", autofocus: true).search {
on keydown(e) {
if e.key == "Enter" && shown.length > 0 { run(shown[0].id) }
}
}
Stack(gap: .xs) {
for c in shown by c.id {
Button(c.label).full {
on click { run(c.id) }
if c.keys != "" { Text(keyLabel(c.keys)).muted.sm }
}
}
if shown.length == 0 { Text("No command matches.").muted }
}
}
}
How it works
Press Ctrl+K, type sav and press Enter: the palette closes and the page says "Ran “Save”." The two theme commands call setTheme, which shows a change once the project names a dark theme (Add a dark-mode switch).
on key("mod+k", e) { … }at the top of the page listens on the whole document for as long as the page shows, and stops when the reader leaves it.modis ⌘ on a Mac and Ctrl elsewhere, which is what an app's shortcuts use.eis the key event:e.preventDefault()keeps the browser's own Ctrl+K (its search bar) and Ctrl+S (save the page) from running too.- The palette is a
Modal. It traps focus while open, Escape closes it, andon closesetsopenback tofalsehowever it closed. shownfilters the commands by what is typed. Enter in the field runs the first one left; a click runs any of them.keyLabel(c.keys)writes a shortcut the way the reader's system does:⌘Son a Mac,Ctrl+Selsewhere.- Every command goes through
run, so a shortcut and the palette do the same thing, and the commands stay one list of data.
Variations
- A shortcut on one element: written inside an element's block,
on key("Enter") { … }listens on that element only. - A chord:
on key("mod+k mod+s") { … }is Ctrl+K then Ctrl+S, the second within 1.5 seconds. - A shortcut for the whole app:
on key(…)at the top ofapplistens on every page. - Check a combination held as data:
keyIs(e, c.keys)in anon keydown(e)handler says whether the event is that combination, so one handler can serve every command in the list. - Show the shortcut beside a control:
Tooltip("Save", shortcut: "mod+s")andMenu.Item(shortcut: "mod+s")write it withkeyLabelfor you.