Dojo NG is being built in the open, one section at a time — follow along on Heptapod.

GUIDE

Keyboard shortcuts and commands

Large apps end up with a list of actions and a list of keys that run them. @dojo-ng/commands keeps both in one registry, and <dj-command-palette> lets people search that registry and run what they find. You can use the first without the second.

List panel: focus the button and press E.

Editor panel: focus the button and press Ctrl/Cmd+B.

Last command

Nothing yet. Press Ctrl+K (Cmd+K on a Mac).
The real palette, with four commands registered. Open it from each panel's button and press ? to see the shortcut list change with focus.

Register commands

A command is an id, a label, and a function. Everything else is optional. The same function runs from the shortcut and from the palette.

import { commands } from "@dojo-ng/commands";

commands.register({
  id: "save",
  label: "Save document",
  group: "File",
  shortcut: "Mod+S",
  run: () => saveDocument(),
});

commands.register({
  id: "go-inbox",
  label: "Go to inbox",
  group: "Go to",
  shortcut: "G I",                  // press G, then I within one second
  keywords: "mail messages",        // words the palette also matches
  when: () => signedIn(),           // hidden, and the key does nothing, while false
  run: () => navigate("/inbox"),
});
  • commands.register() returns a function that removes the command. Registering an id again replaces the first.
  • group is the heading in the palette. keywords are extra words it matches on.
  • when says whether the command applies now. When it returns false the palette hides the command and its key does nothing, and the key is not swallowed.

Writing a shortcut

  • Mod is Cmd on a Mac and Ctrl everywhere else, so "Mod+K" is right on both.
  • Keys are matched by position on the keyboard (event.code), not by the character they type. On a Mac, Option changes the character, so "Alt+Mod+1" would never match if it read event.key.
  • Letters and digits can be written as is. Other keys use names: Enter, Escape, Space, Tab, Delete, the arrows, F1 to F12, and punctuation such as . and /.
  • A sequence is several keys separated by spaces: "G I". The next key has to come within one second.
  • A typo, such as "Mod+Foo", throws when you register, not when someone presses the key.

Scopes

A scoped command works only while focus is inside its element, shadow roots included. The innermost scope wins, then global commands. That lets a grid and an editor both use Delete.

// Delete means "delete rows" in the grid and "delete selection" in the editor.
const gridScope = commands.scope(document.querySelector("dj-data-grid"));
gridScope.register({ id: "grid.delete", label: "Delete rows", group: "Grid", shortcut: "Delete", run: removeRows });

const editorScope = commands.scope(document.querySelector("#editor"));
editorScope.register({ id: "editor.delete", label: "Delete selection", group: "Editor", shortcut: "Delete", run: removeSelection });

// When the grid is taken off the page:
gridScope.dispose();

Conflicts and reserved keys

  • Two commands on the same shortcut in the same scope log one console warning naming both ids. The first one registered wins. A shortcut that is the start of another sequence ("G" and "G I") counts as a conflict.
  • The browser keeps Mod+W, Mod+T, Mod+N and Mod+Q. A command that asks for one is registered without a key and the console says so. It still shows in the palette.

Typing in a field

Shortcuts do not fire while the person is typing in a text field, a text area, a select, or an element with contenteditable, because Delete, the arrows and single letters belong to the field. A command can opt in with allowInText. The palette's own key does, so it opens and closes from inside a form.

commands.register({
  id: "focus-mode",
  label: "Focus mode",
  shortcut: "Mod+.",
  allowInText: true,   // still works while the author types in the editor
  run: toggleFocusMode,
});

The palette

Put one <dj-command-palette> on the page. Mod+K opens it. Type to narrow the list: every word typed must appear in the command's group, label, or keywords, in any order, and a label that starts with the first word comes first. The arrow keys move, Enter runs, and Escape closes and puts focus back where it was.

<dj-command-palette></dj-command-palette>
<script type="module">
  import "@dojo-ng/command-palette";
</script>
  • With nothing typed, the commands you ran last come first, then the rest under their group headings. Recent commands are kept in memory only. Use dj-command-run to store them yourself.
  • Each row shows its shortcut at the end, in the reader's own form.
  • The palette lists what applies where focus was when it opened: global commands, and the commands of any scope that holds that focus.
  • A screen reader hears the highlighted command read out, for example "Save document, 2 of 7", as the arrows move.

The shortcut list

Press ? in the empty palette, or call showHelp(), to list every active shortcut by group. It follows the focus the palette opened from, so a grid's keys appear while the grid has focus. Typing returns to searching.

<dj-command-palette shortcut="Mod+P" label="Go anywhere"></dj-command-palette>
<dj-button id="help">Keyboard shortcuts</dj-button>
<script type="module">
  import "@dojo-ng/command-palette";
  const palette = document.querySelector("dj-command-palette");
  document.getElementById("help").addEventListener("click", () => palette.showHelp());
  palette.addEventListener("dj-command-run", (e) => console.log("ran", e.detail.id));
</script>

Showing a shortcut

Use formatShortcut() anywhere you print a key, such as a menu item or a tooltip.

import { formatShortcut } from "@dojo-ng/commands";

formatShortcut("Mod+K");        // "⌘K" on a Mac, "Ctrl+K" elsewhere
formatShortcut("Alt+Mod+1");    // "⌥⌘1" on a Mac, "Ctrl+Alt+1" elsewhere
formatShortcut("G I");          // "G I"

Other Dojo NG keys

dj-toaster registers its Alt+T (move focus to the newest toast) as the command dj-toaster.focus, so it appears in the shortcut list too.

Next: Writing plugins Extend the grid, the editor, and the chart without forking them.