Skip to content

Kbd Component for React

The Kbd component renders keyboard shortcuts as glass key caps in React, showing ⌘K on a Mac and Ctrl+K on Windows and Linux from one definition.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/kbd.json

Search anything

CtrlK

Press it from anywhere.

  • Kbd
  • KbdShortcut
  • useShortcutPlatform
  • +1 more

Keys and shortcuts

Esc↵TabControl KControl Shift PControl K

Press Control K to search, ? for every shortcut.

Files to copy

3 folders · 1 file · 2 packages
From theme/src/
  • components/kbd/
  • lib/cn.ts
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/kbd.jsonRegistry item: kbd. The styles need the Tailwind v4 glass UI setup.
Packages
pnpm add clsx tailwind-merge

Every platform

Chips for hints in text; inline for menus (⇧⌘P on a Mac, Ctrl+Shift+P elsewhere), in each platform's own modifier order. Pin a platform to document both.

Shortcuts as chips and as inline menu text
ActionChips · inline
SearchControl K
Command paletteControl Shift P
Move line upAlt Up Arrow
DeleteControl Backspace
Show shortcutsQuestion mark

API

KbdShortcut

PropTypeDefaultDescription
keys (required)string | readonly string[]NoneThe shortcut: "mod+k", "shift+mod+p", "?" or an array of keys. mod = ⌘ on Apple, Ctrl elsewhere.
variant"chips" | "inline""chips""chips": one key cap per key. "inline": plain text as in menus (⇧⌘P, Ctrl+Shift+P).
platformShortcutPlatform | "auto""auto"Force a platform, e.g. to document both.
size"sm" | "md""sm"Size of the key caps in the "chips" variant.
labelsPartial<KbdLabels>defaultKbdLabelsOverride the visible or spoken key names (i18n). Every other string, for translation: labels={{ mac: "…" }}; omitted keys keep the English of defaultKbdLabels. All 4 keys of KbdLabels are listed in the key table under the props table.
announcebooleantrueRead the shortcut to screen readers ("Command K"). Turn off when the parent already exposes it, e.g. a menu item with aria-keyshortcuts.
KbdLabels keys and English defaults (4)
Pass any key in labels; the rest keep the defaults of defaultKbdLabels.
KeyEnglish default
mac{ mod: "⌘", meta: "⌘", ctrl: "⌃", alt: "⌥", shift: "⇧", enter: "↵", escape: "Esc", backspace: "⌫", delete: ...
other{ mod: "Ctrl", meta: "Win", ctrl: "Ctrl", alt: "Alt", shift: "Shift", enter: "Enter", escape: "Esc", backsp...
macSpoken{ mod: "Command", meta: "Command", ctrl: "Control", alt: "Option", shift: "Shift", enter: "Return", escape:...
otherSpoken{ mod: "Control", meta: "Windows", ctrl: "Control", alt: "Alt", shift: "Shift", enter: "Enter", escape: "Es...

Kbd and helpers

PropTypeDefaultDescription
size"sm" | "md""sm"Height of the key cap: "sm" (default, 20px) or "md" (24px).
<Kbd size>"sm" | "md""sm"One key cap; any text or symbol inside.
formatShortcut(keys, platform, labels?){ keys, spoken, aria }NoneVisible keys, words for screen readers and an aria-keyshortcuts value.
matchesShortcut(event, keys, platform?)booleanNoneDoes a keydown event match? Modifiers must match exactly.
useShortcutPlatform(platform?)"mac" | "other"NoneReturns the visitor's platform ("other" during server rendering).

Keyboard

KeysAction
TabNot focusable: Kbd and KbdShortcut only display keys.

Accessibility

  • Keys render in <kbd> elements. KbdShortcut adds a visually hidden spoken form ("Control Shift P") unless announce is false.
  • Pass platform to force Mac or other symbols; by default the visitor's platform decides, so "mod" shows Cmd on a Mac and Ctrl elsewhere.
  • A shortcut shown in a menu item is only a hint: wire the handler yourself (see useCommandShortcut and matchesShortcut).

When to use the Kbd component

  • Use it as a keyboard shortcut component in help text and docs to show a single key, such as pressing Esc to close a panel, as a native kbd element with a sm or md size.
  • Use KbdShortcut next to a search button or a menu item to show a combination like mod+k, which reads as the Command symbol and K on a Mac and Ctrl K elsewhere.
  • Use it inside an onboarding checklist or a shortcuts cheat sheet, where variant="inline" writes plain text like Ctrl+Shift+P and the chips variant draws one cap per key.
  • Skip it when the element is clickable or needs behavior; Kbd is a label and not a control, so Button fits an action, and binding the key still needs your own handler.
  • Pair it with Dropdown menu or Command palette items, which already show shortcuts, Tooltip to reveal a key when hovering an icon button, and the useShortcutPlatform hook for custom text.

Frequently asked questions

67 components pages in all: browse them on All components.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .