Kbd
FreeNewKeys and multi-key shortcuts, with a spoken label so symbols such as the Command key are read as words.
Installation
pnpm dlx shadcn@latest add @viewport/kbd
First time? Add the @viewport registry to components.json. Setup takes one minute.
Usage
The code behind the preview above.
import { Kbd, KbdGroup } from "@/components/viewport/kbd";
const shortcuts = [
{ action: "Open command menu", keys: ["⌘", "K"], label: "Command K" },
{ action: "Search in page", keys: ["⌘", "F"], label: "Command F" },
{ action: "New record", keys: ["C"], label: "C" },
{ action: "Go to contacts", keys: ["G", "C"], label: "G then C" },
{ action: "Undo", keys: ["⌘", "Z"], label: "Command Z" },
];
export default function KbdDemo() {
return (
<div className="w-full max-w-sm rounded-2xl border border-border bg-card">
<p className="border-b border-border px-4 py-3 text-sm font-medium">Keyboard shortcuts</p>
<dl className="p-1.5">
{shortcuts.map(({ action, keys, label }) => (
<div key={action} className="flex items-center justify-between gap-4 rounded-lg px-2.5 py-2">
<dt className="text-sm text-muted-foreground">{action}</dt>
<dd>
<KbdGroup keys={keys} label={label} />
</dd>
</div>
))}
</dl>
<p className="border-t border-border px-4 py-3 text-xs text-muted-foreground">
Press <Kbd>?</Kbd> anywhere to see this list.
</p>
</div>
);
}Props
| Prop | Type | Default |
|---|---|---|
| keys* | string[]The keys of one shortcut, in order, e.g. `["⌘", "K"]`. | — |
| label | stringWhat the shortcut is read as, e.g. "Command K". Symbols such as ⌘ are not read reliably. | — |
Accessibility
- Renders kbd elements.
- A group can carry a text label and hides the symbols from screen readers.
- Server component with no client JavaScript.