Skip to content

Extras

CommandPalette

Bit.BlazorUI.ExtrasCommandMenuCmdkSpotlight

BitCommandPalette is an accessible, keyboard-first command palette: the Ctrl+K (Cmd+K on a Mac) box that fuzzy-searches every page, action and setting of an app and runs the one picked, with nested pages, prompts, multi-select, async sources, recents and global shortcuts. Matching, ranking and every decision run in C#; the browser only shows the native dialog and listens for keys.

Keyboard & shortcuts

The keys inside the palette, global shortcuts and sequences, keyboard layouts and input methods, conflicts, the shortcut syntax, platform-aware hints and the toggle shortcut.

Keyboard inside the palette

The palette is built to be driven without a mouse: inside it every key has a job. Mod below is Ctrl on Windows and Linux and ⌘ on macOS and iOS.
KeysWhat they do
Mod K and the other toggle shortcutsOpen or close the modal palette (see "The toggle shortcut" below).
↑ ↓, Ctrl N / Ctrl PMove the active row, wrapping around at either end unless Loop is off. Ctrl N / P work on every platform, also on a Mac.
Ctrl J / Ctrl KMove down / up, when VimBindings is on.
Home EndJump to the first or the last row.
PgUp PgDnMove ten rows up or down, stopping at the ends.
EnterRun the active command, open its page, flip a toggle, check a row of a multi-select page, submit a prompt, or arm and then confirm a destructive command. On a page that failed to load it retries.
Space (empty search box)Check or uncheck a row of a multi-select page.
Mod Enter (⌘ ↩)Apply a multi-select page, run the active row's first secondary action, or open a plain link in a new tab (as GitHub's palette does).
Tab, → (empty search box), a right clickOpen the action panel of the active row: its own actions, then "Pin to top" / "Unpin", "Remove from recents" and "Clear recent commands".
Backspace, ← (empty search box)Go back one page.
EscDisarm a confirmation, then go back a page, then close the modal palette. An Inline palette clears its query instead of closing.
Mod Shift PPin or unpin the active row; pinned commands are listed first on the empty query.
Shift DelRemove the active row from the recents.

These keys are claimed in the browser before Blazor sees them, so Tab never leaves the modal dialog and Backspace never deletes text it should not, even over a slow Blazor Server connection. In right-to-left text ← and → trade places: → points back to the start of the line, so it goes back a page, and ← opens the action panel. Inside the open modal palette its own chords win over a toggle bound to the same keys: with Vim bindings on, Ctrl K moves up rather than closing it, and Mod Shift P pins even when it is also a toggle shortcut.
An Inline palette is a part of the page, not a dialog, so a few keys behave as they do anywhere else on it: Tab moves the focus on (open the action panel with → or a right click there), and the page's modal palette keeps its toggle - with the default Mod K, pressing Ctrl K in the Inline palette below opens the modal one on Windows and Linux even with VimBindings on; Ctrl J and Ctrl N / P are always the Inline palette's.
Three parameters tune the navigation of one palette, and the options of the same names (BitCommandPaletteOptions.Loop, default true, and VimBindings, default false) set them for all:
  • Loop: arrowing past the last row comes back to the first. Off, the arrows stop at the ends.
  • VimBindings: adds Ctrl J / Ctrl K to the always-on Ctrl N / Ctrl P.
  • DisablePointerSelection: hovering a row no longer makes it the active one - only the keys and a click do. Without it, hover moves the active row only after the pointer actually moves, so a list that scrolls under a resting pointer never steals the selection from the keyboard. Clicking a row leaves the focus in the search box.
Flip them below and try every key: the list has a page (Folders), a multi-select page (Label the thread), a command with secondary actions (Open the inbox), a destructive one (Empty the trash) and enough rows for PgDn. Pin a row and clear the query to see it listed first; the right-to-left switch shows the arrows trading places.

Open the inbox
Compose a message
Reply to all
Forward the thread
Snooze the thread
Mute the thread
Folders
Label the thread
Print the thread
Block the sender
Report spam
Empty the trash

Nothing run yet.

Global shortcuts

A command's Shortcut also runs it while the palette is closed, from anywhere on the page - a chord such as Alt+Shift+N or a sequence such as G N (G, then N). Register such commands through IBitCommandPaletteService (or with BitCommandPaletteCommands, for the commands of one page): the modal palette on the page claims their keys, and while there is no modal palette - or only Inline ones, which claim no global keys - the shortcuts do nothing, so nothing ever runs twice.
The registered shortcuts are synced to the browser as strings and matched there, so only a completed shortcut crosses the interop boundary - typing on the page never causes a round trip, even on Blazor Server. Whatever needs the palette opens it: a command with a page (G C below opens on its colors), one with a confirmation (Alt+Shift+D), and a handler that answers with a page, a prompt or a toast that stays open. A confirmation started this way gets a page of its own, so the second Enter confirms that command and no other. Only a command that can run claims its keys: the shortcut of a disabled command, or of one its VisibleWhen predicate hides, is left to the page - call paletteService.Refresh() after the state such a predicate reads changed.
Shortcut(text, global: false) keeps a shortcut to the palette's rows: it is shown as a hint on the row (Preview the note shows Space in the modal palette), but claims no key on the page - for keys the page or a component already handles itself.
Typing is not a shortcut. A plain key pressed while an input, a text area, a select or a content-editable element has the focus is typing, so G then N in the text field below does nothing; a chord with Ctrl, Alt or ⌘ still fires there, because nobody types those by accident - unless the element binds that chord itself: the palette hears a key pressed in an editable element only once the element has had it, and leaves one it claimed (by preventing its default) alone, so Ctrl+K in a BitMarkdownEditor still inserts a link. Set BitCommandPaletteOptions.SuppressShortcutsInEditableElements (default true) to false to let plain keys fire from text fields too; the palette then hears every key ahead of the page, the editors' chords included.
The table below reads the registry right now: it lists every command with a global shortcut that this page's sections have registered, and updates through the service's CommandsChanged event. Press one with the palette closed.

ShortcutCommandKind
Alt+M Merge selected files chord
G then X Export as PDF sequence
Alt+Shift+N New note chord
G then N Go to notes sequence
G then C Change the note color sequence
Alt+Shift+D Discard the draft chord
Alt+Q Save draft chord

Last run by a shortcut: nothing yet

Keyboard layouts, held keys and input methods

Shortcuts are written once, with Latin letters, and keep working whatever the keyboard types:
  • Non-Latin layouts. A shortcut is matched on the character the key types and, when that is not an ASCII character, on the key it sits on as well (KeyboardEvent.code, as VS Code does): Ctrl K works when a Russian layout types л, G N when a Persian one types ل د, and the same holds for Arabic, Greek and every other layout whose letters are not ASCII. A Latin layout such as Dvorak or AZERTY keeps its own letters: there Mod+Q is the key that types Q.
  • Held keys. A held key's repeats are claimed - the browser does not act on them - but run nothing, so a held Ctrl K does not flicker the palette open and shut, and a held shortcut does not run its command over and over.
  • AltGr and Option. A character typed with AltGr, or with Option or Alt alone (Ω for Option+Z on a Mac, @ for AltGr+Q on a German layout), is typing when it lands in a text field, not the shortcut its physical key would make.
  • Input methods. Keys that belong to an input method's composition (isComposing, or keyCode 229) are never shortcuts, inside the palette or out, so confirming a Japanese or Chinese candidate with Enter does not run the active row.
Nothing needs to be configured for any of this. To try it, switch the operating system to a Russian, Persian, Arabic or Greek layout and press the shortcuts of the previous section with the palette closed - Alt Shift N, or G then N - or hold one down.

Last run by a shortcut: nothing yet

Sequences in progress

After the first chords of a sequence - G of G N - the modal palette shows a small status near the bottom of the page with the keys pressed so far and says the next one is awaited, as VS Code does in its status bar; screen readers announce it. It goes when the sequence completes, breaks off or times out. Press G with the palette closed (and the focus outside a text field) to see it.
  • SequenceTimeoutMs (BitCommandPaletteOptions, default 1000): how long a sequence waits for its next chord before it breaks off. It must be positive; zero or less throws an ArgumentOutOfRangeException.
  • ShowSequenceHint (default true): set it to false to show nothing - for an app that shows the pending keys itself.
  • IBitCommandPaletteService.SequencePending reports the sequence to code either way: raised with the chords pressed so far, and again with null when it completed, broke off or timed out. The status line below listens to it.
The built-in status is the bit-cpl-sequence element (data-bit-cpl="sequence", with data-pending="true" while it shows), and its words are BitCommandPaletteStrings.SequencePending ("waiting for the next key…") and ShortcutThen ("then").

Sequence: none in progress

Shortcut conflicts

When several commands claim one global shortcut, the one registered last wins - so a page's own Mod+S takes over from the layout's while the page is shown, and gives the keys back when it is disposed. The modal palette checks the shortcuts whenever the registry changes and logs a warning through ILogger (the browser's console on WebAssembly, the server's log on Blazor Server), once for each problem:
  • A duplicate: a shortcut several commands claim - the warning names them all and the one that runs.
  • Shadowed by the toggle: a command's shortcut that is also a toggle shortcut of the palette, which takes it, so the command never runs from it.
  • An unreachable sequence: one whose first chords are a shortcut of their own and fire first - G registered next to G I.
  • A browser-reserved key: one browsers keep for themselves and never hand to a page - Mod+W, Mod+T, Mod+N, their Shift variants, Mod+Q, Ctrl+Tab, Alt+F4.
Two commands registered together with the same id are reported too: the second replaces the first, so give commands built from data explicit ids with .Id(...). These configuration warnings always name the commands, even with RecordCommandIdsInTraces off. Inline palettes claim no global keys, so they report none of this.
This page registers "Save draft" on Alt Q. Register "Save and publish" on the same keys and, with the palette closed, Alt Q runs it instead - the later registration wins - while the log shows the palette's warning naming both. Dispose it and the keys are "Save draft"'s again.



Alt Q runs Save draft, last pressed: not yet

Shortcut syntax

A shortcut is a BitCommandPaletteKeyboardShortcut: one or more BitCommandPaletteKeyChords pressed in order. Write it as text: chords are separated by spaces and keys inside a chord by + - Mod+K, Ctrl+Shift+P, G I, Alt+Z, F2, Shift+?. A string converts implicitly wherever a shortcut is expected (.Shortcut("G I"), the options' ToggleShortcut, the Shortcut of BitCommandPaletteShortcutHint).
  • Mod is ⌘ on macOS and iOS and Ctrl elsewhere - the one to use for an app's own shortcuts (CmdOrCtrl and CommandOrControl mean the same). Ctrl is Ctrl (⌃) everywhere, and Meta is ⌘ on a Mac and Win elsewhere.
  • Modifiers have aliases: Control and ⌃; ⇧; Option, Opt and ⌥ for Alt; Cmd, Command, Win, Super and ⌘ for Meta.
  • So do keys: Esc, Return, Space (or Spacebar), Del, Ins, Up/Down/Left/Right, PgUp, PgDn, Plus, Minus, Comma, Period, Slash, Backslash. Any other key is the browser's KeyboardEvent.key name (F2, Home, Tab), matched case-insensitively. A shifted character also fires without its Shift: ? fires on Shift / of a US layout.
  • BitCommandPaletteKeyboardShortcut.Parse throws a FormatException for a chord without a key (Ctrl+Shift) or with two (A+B); TryParse returns false instead.
  • Two shortcuts are equal when their Normalized forms are - lower case, modifiers in a fixed order (mod+ctrl+alt+shift+meta+key) - so Ctrl+K equals control+k. IsSequence and Chords tell a sequence from a chord, ToDisplayString(isMac) spells it out ("Ctrl+Shift+K", "G then I") and DisplayTokens(isMac) gives the key caps a hint renders.
Type one, or pick a sample:

On macOS⌘⇧K
ElsewhereCtrl+Shift+K
In wordsCtrl+Shift+K
Normalizedmod+shift+k
Kinda chord

Platform-aware hints

The palette detects macOS and iOS once it starts in the browser, and every hint it draws follows: ⌘ ⇧ K there, Ctrl Shift K elsewhere - and a sequence reads G then S on both.
  • IBitCommandPaletteService.IsMac exposes the result to your own UI. It is false until the palette has started, and the service's PlatformDetected event says when it is known: subscribe to it to render again (and unsubscribe in Dispose - the service outlives the page).
  • BitCommandPaletteShortcutHint renders any shortcut as the palette does: key caps in <kbd> elements, laid out left to right even in right-to-left text, as on the keys themselves, and hidden from screen readers, which read its words instead ("Ctrl+Shift+K", "G then S"). Its MacKeys parameter picks the macOS symbols (pass paletteService.IsMac to follow the platform), and Strings the word between the chords of a sequence (ShortcutThen). Class and Style style it.
  • BitCommandPaletteTrigger follows the detected platform by itself; its MacKeys parameter forces the symbols on or off.

This browser is not a Mac.

This platformCtrl+Shift+K G then S
MacKeys⌘⇧K G then S
MacKeys="false"Ctrl+Shift+K G then S

The toggle shortcut

The toggle shortcut opens the modal palette when it is closed and closes it when it is open.
  • ToggleShortcut (BitCommandPaletteOptions, default Mod+K: ⌘ K on a Mac, Ctrl K elsewhere) is the main one, and the one BitCommandPaletteTrigger shows.
  • AdditionalToggleShortcuts adds more keys that do the same - Mod+Shift+P for people used to VS Code, or /; a sequence works too.
  • The palette's ToggleShortcut parameter overrides the option's main shortcut for that palette (ToggleShortcut="Mod+P"); a value that does not parse is ignored and the option applies. The additional shortcuts always come from the options.
  • A toggle of null: set options.ToggleShortcut = null for no main toggle shortcut - the palette then opens only from AdditionalToggleShortcuts, a BitCommandPaletteTrigger, its bound Open or the service's OpenAsync / ToggleAsync - for an app whose own editor uses Ctrl K.
Only the modal palette answers the toggle: an Inline palette, or a Disabled one, claims none. While the palette is open only single-chord toggles stay global, and its own chords win over a toggle bound to the same keys - with Vim bindings on, Ctrl K moves up inside the palette rather than closing it. A command shortcut that is also a toggle shortcut never runs, and the palette logs it (see Shortcut conflicts).
The toggle shortcuts of this site, read from the injected options:

Ctrl+K

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.