Kbd
Key cap for a single key or a combination, built from nested kbd elements. Modifier glyphs such as the command sign carry a spoken name and every cap keeps a 24px box.
How it differs. The primitive key cap that other shortcut displays compose: one kbd per key, nested in an outer kbd for a combination, with named modifiers mapped to glyphs.
Install
pnpm dlx shadcn@latest add @hazine/kbdAdds 1 file.
First time? Add the registry to components.json
{
"registries": {
"@hazine": "https://api.hazine.dev/r/{name}.json"
}
}Needed once per project. Items resolve each other through the @hazine namespace.
components/hazine/kbd.tsx, 54 lines
import * as React from "react"
import { cn } from "@/lib/utils"
const KEY_CLASS =
"inline-flex h-6 min-w-6 items-center justify-center rounded-md border border-border bg-muted px-1.5 font-mono text-xs font-medium text-foreground"
// Glyph keys are written as escapes and always carry a spoken name.
const KEY_GLYPHS: Record<string, { glyph: string; name: string }> = {
command: { glyph: "⌘", name: "Command" },
shift: { glyph: "⇧", name: "Shift" },
option: { glyph: "⌥", name: "Option" },
control: { glyph: "⌃", name: "Control" },
}
export interface KbdProps extends React.ComponentProps<"kbd"> {
/**
* Key names for a combination, for example ["command", "K"]. Renders one nested
* kbd per key. The names command, shift, option and control become glyphs.
*/
keys?: string[]
}
export function Kbd({ keys, className, children, ...props }: KbdProps) {
if (keys && keys.length > 0) {
return (
<kbd
data-slot="kbd"
className={cn("inline-flex items-center gap-1", className)}
{...props}
>
{keys.map((key) => {
const glyph = KEY_GLYPHS[key.toLowerCase()]
return (
<kbd key={key} data-slot="kbd-key" className={KEY_CLASS}>
{glyph ? (
<abbr title={glyph.name} className="no-underline">
{glyph.glyph}
</abbr>
) : (
key
)}
</kbd>
)
})}
</kbd>
)
}
return (
<kbd data-slot="kbd" className={cn(KEY_CLASS, className)} {...props}>
{children}
</kbd>
)
}
The demo in the preview, 31 lines
import { Kbd } from "@/components/hazine/kbd"
const SHORTCUTS = [
{ action: "Search invoices", keys: ["command", "K"] },
{ action: "Save draft", keys: ["command", "S"] },
{ action: "Duplicate line", keys: ["command", "shift", "D"] },
]
export default function KbdDemo() {
return (
<dl className="grid gap-3 text-sm">
{SHORTCUTS.map((shortcut) => (
<div
key={shortcut.action}
className="flex items-center justify-between gap-6"
>
<dt className="text-muted-foreground">{shortcut.action}</dt>
<dd>
<Kbd keys={shortcut.keys} />
</dd>
</div>
))}
<div className="flex items-center justify-between gap-6">
<dt className="text-muted-foreground">Close the panel</dt>
<dd>
<Kbd>Esc</Kbd>
</dd>
</div>
</dl>
)
}
Props1
- keys
- string[]
- Key names for a combination, for example ["command", "K"]. Renders one nested kbd per key. The names command, shift, option and control become glyphs.
Also accepts every prop of React.ComponentProps<"kbd">.
Accessibility
- Pattern
- Landmarks and headings
- Keyboard
- Nothing to operate. The item has no focusable parts.
- Screen reader
Native kbd elements. A glyph key is wrapped in abbr with the key name as its title, so the name is read in place of the symbol.
- Reduced motion
- No motion.
- Checked
- Not checked yet. Automated results appear here once CI has run.
Dependencies0
None. The item is one self-contained file.
Tokens shipped0
None. The item is styled by the tokens your theme already has.