skip to content
mpkcn

~/components/shortcuts-dialog

Shortcuts Dialog

A keyboard-shortcut cheat sheet in a terminal-style dialog that opens with "?".

or press ? anywhere on this page

Installation

terminal
npx mpkcn add shortcuts-dialog
deps:none beyond react· tested with react@19.3.0

Usage

Render it once and list your shortcuts. Pressing ? anywhere (outside a text field) toggles it.

tsx
import { ShortcutsDialog } from "@/components/mpkcn/shortcuts-dialog";

export function Help() {
  return (
    <ShortcutsDialog
      shortcuts={[
        { keys: ["j", "k"], description: "next / previous section", group: "navigate" },
        { keys: "g", description: "back to top", group: "navigate" },
        { keys: ["Ctrl", "K"], description: "command palette", group: "general" },
        { keys: "?", description: "this help", group: "general" },
      ]}
    />
  );
}

keys is one string (one badge) or an array (one badge per key). Items with a group are listed under it, in the order groups first appear.

Examples

Open it from a button

Control it with open and onOpenChange; the ? key keeps working alongside.

tsx
const [open, setOpen] = useState(false);

<button onClick={() => setOpen(true)}>shortcuts</button>
<ShortcutsDialog shortcuts={shortcuts} open={open} onOpenChange={setOpen} />

title is the command in the header (and the dialog's accessible name).

tsx
<ShortcutsDialog title="man my-app" footer="tip: shortcuts work on every page" shortcuts={shortcuts} />

Props

PropTypeDefaultDescription
shortcuts{ keys: string | string[]; description: ReactNode; group?: string }[]"?" and "Esc"The shortcuts to list.
titlestring"man shortcuts"Command in the header; also the accessible name.
openboolean—Controlled open state.
defaultOpenbooleanfalseInitial open state when uncontrolled.
onOpenChange(open: boolean) => void—Called when it wants to open or close.
hotkeystring | false"?"Key that toggles it, ignored while typing in a field. false turns it off.
footerReactNode | false"press esc to close"Text under the list; false hides it.
classNamestring—Extra classes for the panel.

Accessibility

  • A role="dialog" with aria-modal, labelled by its header, rendered in a portal.
  • Focus moves to the close button when it opens, Tab and Shift+Tab stay inside, and focus returns to where it was when it closes.
  • Esc, the close button and a click on the backdrop all close it. Page scroll is locked while it is open.
  • The ? key is ignored while typing in an input, textarea, select or editable element, and when Ctrl, Cmd or Alt is held.
  • Shortcuts are a description list (dl), with each key in a kbd.