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.jsonSearch anything
CtrlK
Press it from anywhere.
KbdKbdShortcutuseShortcutPlatform- +1 more
Keys and shortcuts
Esc↵TabCtrlKControl KCtrlShiftPControl Shift PCtrlKControl K
Press CtrlKControl K to search, ? for every shortcut.
import { Kbd, KbdShortcut } from "@/components/sheenui/kbd/kbd";
// Package build (after the first npm release, or a packed tarball): import { Kbd, KbdShortcut } from "@sheenui/theme";
export function Example() {
return (
<>
<Kbd>Esc</Kbd>
<Kbd>↵</Kbd>
<KbdShortcut keys="mod+k" />
<KbdShortcut keys="shift+mod+p" />
<KbdShortcut keys="mod+k" size="md" />
<p>Press <KbdShortcut keys="mod+k" /> to search.</p>
</>
);
}Files to copy
3 folders · 1 file · 2 packages
- From
theme/src/ components/kbd/lib/cn.tsstyles/
- 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.
| Action | Chips · inline |
|---|---|
| Search | CtrlKControl KCtrl+K |
| Command palette | CtrlShiftPControl Shift PCtrl+Shift+P |
| Move line up | Alt↑Alt Up ArrowAlt+↑ |
| Delete | CtrlBackspaceControl BackspaceCtrl+Backspace |
| Show shortcuts | ?Question mark? |
import { useEffect } from "react";
import { KbdShortcut } from "@/components/sheenui/kbd/kbd";
import { formatShortcut, matchesShortcut } from "@/components/sheenui/kbd/shortcut";
// Package build (after the first npm release, or a packed tarball): import { KbdShortcut, formatShortcut, matchesShortcut } from "@sheenui/theme";
export function Example() {
// Helpers
const parts = formatShortcut("shift+mod+p", "other"); // { keys: ["Ctrl", "Shift", "P"], spoken: "Control Shift P", aria: "Control+Shift+P" }
useEffect(() => {
const onKeyDown = (event: KeyboardEvent) => {
if (matchesShortcut(event, "mod+k")) console.log("palette"); // in a keydown handler
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, []);
return (
<>
<p>{parts.spoken}</p>
<KbdShortcut keys="shift+mod+p" /> {/* chips */}
<KbdShortcut keys="shift+mod+p" variant="inline" /> {/* menu text */}
<KbdShortcut keys="shift+mod+p" platform="mac" /> {/* always ⇧ ⌘ P */}
</>
);
}API
KbdShortcut
KbdLabels keys and English defaults (4)
Kbd and helpers
Keyboard
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
Related pages
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .