Skip to content
WANCRAFT

A.155Buttons & Controls

Kbd Shortcut

Keycaps for a keyboard shortcut that light up as the keys are physically pressed, with platform-aware modifiers and sequences.

NewReactMotionTailwindintermediate

Overview

Kbd Shortcut documents a shortcut and lets people try it on the spot. Each key renders as a keycap, and while the component is listening (when focused, or across the window if you opt in) every cap dips and lights up as its physical key goes down. Modifiers follow the platform: ⌘ ⌥ ⇧ on Apple devices and Ctrl / Alt / Shift elsewhere, with a deterministic server render that refines after hydration. Sequences like “g then i” show each completed step in the accent colour and reset after a timeout, and onTrigger fires once the whole shortcut has been pressed, so the same component can document and bind a shortcut. Screen readers hear “Command K” instead of a row of glyphs.

01Installation

Method 01 · Registry

CLI verified

Uses the shadcn CLI in a project initialised with shadcn init. Installs the file to components/ui/kbd-shortcut.tsx and adds its dependencies.

npx shadcn@latest add https://wancraft.my/r/kbd-shortcut.json

Method 02 · Copy and paste

  1. 1. Install the dependencies below.
  2. 2. Copy the source from the Code tab into components/ui/kbd-shortcut.tsx.
  3. 3. Import it and adjust props.
npm install motion
Dependencies
motion
Peer
react ≥ 18, tailwindcss ≥ 4
Added
13 Oct 2026
Updated
—

02Usage

The default configuration. Change properties above to generate a tailored example in the Usage tab.

example.tsx
import { KbdShortcut } from "@/components/ui/kbd-shortcut";

export function KbdShortcutExample() {
  return (
    <KbdShortcut onTrigger={() => console.log("Shortcut pressed")} />
  );
}

03Props

PropTypeDefaultDescription
shortcutstring"mod+k"Chord keys joined with "+", sequence steps with spaces ("g i" or "g then i"). mod = ⌘ on Apple, Ctrl elsewhere; also cmd, ctrl, alt / option, shift, enter, esc, tab, space, arrows, f1–f12, symbols, plus.
listen"focus" | "window" | "none""focus"Light keys and fire onTrigger while focused, across the window (typing in inputs is ignored), or never.
onTrigger(event: KeyboardEvent) => void—Called when the full shortcut has been pressed.
platform"auto" | "mac" | "other""auto"Modifier glyphs. Auto renders Ctrl on the server and refines to ⌘ after hydration on Apple devices.
size"sm" | "md" | "lg""md"Keycap height 20 / 28 / 44px.
variant"raised" | "soft" | "outline""raised"Keycap style.
separatorbooleanfalseShow “+” between the keys of a chord.
sequenceTimeoutnumber1000Time allowed between sequence steps, in ms.
accentstring"#FF7448"Pressed keys, completed steps, trigger flash and focus ring.
...propsHTMLAttributes<HTMLSpanElement>—Forwarded to the root element.

04Features

  • 01Keycaps that dip and light up while their physical keys are held
  • 02Platform-aware modifiers (⌘ ⌥ ⇧ ⌃ vs Ctrl / Alt / Shift) with an SSR-safe default
  • 03Sequences (“g then i”) with per-step progress and a timeout
  • 04onTrigger when the whole shortcut is pressed, scoped to focus or the window
  • 05Layout-independent matching for letters and digits (KeyboardEvent.code), so ⌥ and Shift combos work
  • 06Three sizes, three keycap styles and optional plus separators
motion · tweeninput · keyboardinput · focus

05Accessibility

  • Keycaps are aria-hidden; screen readers get spoken text such as “Command K” or “G, then I” (as the group's label when focusable, otherwise as visually hidden text).
  • When listen="focus" the shortcut is a focusable group with a visible focus ring; keys that belong to the shortcut are consumed there so page-level shortcuts do not also fire, and Tab still moves focus.
  • In window mode, key presses inside inputs, textareas, selects and contenteditable elements are ignored, and only the completing key is prevented.
  • Pressed keys are shown by colour, border and position, not colour alone; with prefers-reduced-motion keys don't move and the trigger flash only fades.

06Limitations

  • Browsers reserve some shortcuts (for example Ctrl+N, Ctrl+T or Ctrl+W); those never reach the page.
  • Symbol keys are matched by the character they produce, so they follow the user's keyboard layout.