Kbd
Keycaps and keycap groups.
Kbd is one keycap and KbdGroup holds the keys of one shortcut. On a touch-only tablet (a coarse pointer, 768px or wider, no mouse or trackpad) the item's stylesheet hides every kbd element on the page, this component or not. Never let a sentence depend on a keycap.
Keycaps
⌘⇧KEscEnterSpace
import { Kbd } from "@/components/ui/kbd"
export default function KbdKeycaps() {
return (
<div className="flex flex-wrap items-center justify-center gap-2">
<Kbd>⌘</Kbd>
<Kbd>⇧</Kbd>
<Kbd>K</Kbd>
<Kbd>Esc</Kbd>
<Kbd>Enter</Kbd>
<Kbd>Space</Kbd>
</div>
)
}
Key combinations
- Search
- ⌘K
- New task
- C
- Assign to me
- ⇧A
- Close the panel
- Esc
import { Kbd, KbdGroup } from "@/components/ui/kbd"
const shortcuts = [
{ action: "Search", keys: ["⌘", "K"] },
{ action: "New task", keys: ["C"] },
{ action: "Assign to me", keys: ["⇧", "A"] },
{ action: "Close the panel", keys: ["Esc"] },
]
export default function KbdKeyCombinations() {
return (
<dl className="grid w-full max-w-64 grid-cols-[1fr_auto] items-center gap-x-6 gap-y-2 text-sm">
{shortcuts.map(({ action, keys }) => (
<div key={action} className="contents">
<dt>{action}</dt>
<dd>
<KbdGroup>
{keys.map((key) => (
<Kbd key={key}>{key}</Kbd>
))}
</KbdGroup>
</dd>
</div>
))}
</dl>
)
}
In a button
import { SearchIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { Kbd, KbdGroup } from "@/components/ui/kbd"
export default function KbdInAButton() {
return (
<Button variant="outline" aria-keyshortcuts="Meta+K">
<SearchIcon data-icon="inline-start" />
Search tasks
<KbdGroup aria-hidden="true">
<Kbd>⌘</Kbd>
<Kbd>K</Kbd>
</KbdGroup>
</Button>
)
}
In a tooltip
import { ArchiveIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { Kbd } from "@/components/ui/kbd"
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"
export default function KbdInATooltip() {
return (
<Tooltip>
<TooltipTrigger
render={<Button variant="outline" size="icon" aria-label="Archive the task" aria-keyshortcuts="E" />}
>
<ArchiveIcon />
</TooltipTrigger>
<TooltipContent>
Archive
<Kbd>E</Kbd>
</TooltipContent>
</Tooltip>
)
}
Installation
npx shadcn@latest add @khaeli/kbdbunx --bun shadcn@latest add @khaeli/kbdInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
Accessible name
- A keycap is read as its text, and it joins the name of the control that holds it: a button with the keys ⌘ and K is named Search tasks ⌘ K. To keep the name to the action, hide the keys with
aria-hiddenand declare the shortcut on the control witharia-keyshortcuts. - A tooltip is not tied to its trigger for assistive technology, so a shortcut shown only in a tooltip needs
aria-keyshortcutson the trigger too. - Write a key as a word (Esc, Enter) unless its symbol is the usual form.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/kbd.json