A component that displays keyboard key combinations with visual feedback when keys are pressed. Supports platform-specific overrides and multiple display modes.
Display a simple keyboard shortcut:
<KeyboardShortcut keys={["command", "k"]} label="Open command palette" />Display shortcuts with multiple modifier keys and combinations:
<KeyboardShortcut keys={["command", "shift", "p"]} label="Command palette" />
<KeyboardShortcut keys={["ctrl", "alt", "delete"]} label="Task manager" />
<KeyboardShortcut keys={["shift", "enter"]} label="New line" />Provide different key combinations for different platforms. The component automatically detects the user's platform:
<KeyboardShortcut
keys={["command", "k"]}
mac={["command", "k"]}
win={["ctrl", "k"]}
linux={["ctrl", "k"]}
label="Open command palette"
/>Enable the listen prop to show visual feedback when the shortcut is pressed. Try pressing the shortcut below:
<KeyboardShortcut
keys={["shift", "a"]}
listen
label="Example with animation"
/>Use KeyboardShortcutProvider to configure global defaults for all shortcuts:
The display prop controls how modifier keys are rendered. Use "symbol" for icons or "text" for labels:
Symbol (default):
⌘+⇧+KText:
Cmd+Shift+K// Symbol display (default)
<KeyboardShortcutProvider display="symbol">
<KeyboardShortcut keys={["command", "shift", "k"]} label="Symbol display" />
</KeyboardShortcutProvider>
// Text display
<KeyboardShortcutProvider display="text">
<KeyboardShortcut keys={["command", "shift", "k"]} label="Text display" />
</KeyboardShortcutProvider>Force a specific platform instead of auto-detection with the platform prop:
Mac:
⌘+CWindows:
⌃+C<KeyboardShortcutProvider platform="mac">
<KeyboardShortcut
keys={["command", "c"]}
mac={["command", "c"]}
win={["ctrl", "c"]}
label="Copy"
/>
</KeyboardShortcutProvider>Enable key press animation for all shortcuts within the provider with the listen prop:
<KeyboardShortcutProvider listen>
<KeyboardShortcut keys={["command", "k"]} label="All shortcuts animate" />
<KeyboardShortcut keys={["command", "j"]} label="Including this one" />
</KeyboardShortcutProvider>The matchShortcut function helps you handle keyboard events by checking if they match a key combination:
import { matchShortcut } from "@228-co/reef/components";
function handleKeyDown(event: KeyboardEvent) {
if (matchShortcut(event, ["command", "k"])) {
event.preventDefault();
openCommandPalette();
}
if (matchShortcut(event, ["ctrl", "shift", "p"])) {
event.preventDefault();
openQuickOpen();
}
}
// Add to your component
window.addEventListener("keydown", handleKeyDown);The function handles modifier key normalization automatically, so you can use the same key names as in the component props.
Keyboard shortcuts can be embedded in SmartStrings using the reef/kbd tag:
Press ⌘+K to open the command palette.
import type { SmartString } from "@228-co/reef/smart-string";
const content: SmartString = [
"Press ",
{ _tag: "reef/kbd", keys: ["command", "k"], label: "Open command palette" },
" to open the command palette.",
];
<Text>{content}</Text>See the
| Name | Type | Default | Description |
|---|---|---|---|
keys | Key[] | - | Base key combination to display |
mac | Key[] | undefined | Mac-specific key override |
win | Key[] | undefined | Windows-specific key override |
linux | Key[] | undefined | Linux-specific key override |
listen | boolean | false (from provider) | Enable key press animation when shortcut is pressed |
label | string | - | Accessibility label (aria-label) |
styles | StyleXStyles | undefined | Additional StyleX styles |
| Name | Type | Default | Description |
|---|---|---|---|
platform | "mac" | "win" | "linux" | auto-detected | Explicit platform setting for key display |
display | "symbol" | "text" | "symbol" | How to render modifier keys |
listen | boolean | false | Default listen behavior for all shortcuts |
Keys are case-insensitive. The Key type enforces valid key strings at compile time.
| Key String | Symbol | Text |
|---|---|---|
command | ⌘ | Cmd |
shift | ⇧ | Shift |
option | ⌥ | Option |
control / ctrl | ⌃ | Ctrl |
win | ⊞ | Win |
fn | Fn | Fn |
alt | ⌥ | Alt |
| Key String | Symbol | Text |
|---|---|---|
enter / return | ↵ | Enter |
tab | ⇥ | Tab |
escape / esc | ⎋ | Esc |
backspace | ⌫ | Backspace |
delete | ⌦ | Delete |
up | ↑ | Up |
down | ↓ | Down |
left | ← | Left |
right | → | Right |
space | ␣ | Space |
capslock | ⇪ | Caps |
Letters (a-z) display as uppercase. Numbers (0-9) display as-is. Invalid keys render as-is without error.
The component uses theme tokens for consistent styling. You can customize the appearance by overriding these tokens in your theme:
// Theme tokens used by KeyboardShortcut
themeDetails: {
kbdBackground: themeColors.neutral300, // Background color
kbdForeground: themeColors.primary500, // Text color
kbdPaddingInline: themeSizing.spaceXS, // Horizontal padding
kbdPaddingBlock: "...", // Vertical padding
kbdBorderRadius: "...", // Corner radius
kbdFontSize: themeSizing.font0, // Font size
kbdGap: "...", // Gap between keys
}The label prop is required and sets the aria-label attribute, ensuring screen readers can announce the shortcut's purpose.
Each key is rendered as a <kbd> element, which is the semantic HTML element for keyboard input.