Kbd
A keycap for showing a key or a shortcut: a small raised key with a darker lower edge. It is the same cap Command and the menus use for their shortcuts, so a shortcut looks the same everywhere.
Install
npx shadcn@latest add @nelumbo/kbdKeys and groups
Kbd is one key. KbdGroup sets several side by side with a small gap.
Shortcut strings
KbdShortcut turns a string into separate caps: "⌘⇧P", "⌘ K" or "Ctrl+Shift+K". Screen readers hear the whole string once, not each key.
In text and buttons
Keycaps sit on the text baseline's center, so they work inline and inside a button.
Press / to focus the search, or ⌘K⌘K to open the command palette.
On dark surfaces
Inside a tooltip (hover or focus the button) the cap switches to the inverse look and gets smaller by itself. Anywhere else on a dark surface, use variant="inverse": it is a translucent tint of the text color, so it works on black and on white.
Props
| Prop | Type | Default | Notes |
|---|---|---|---|
Kbd variant | "default" | "inverse" | "default" | Inverse tints the key with the text color, for dark surfaces. |
Kbd ...props | ComponentProps<'kbd'> | Rendered as a <kbd> with data-slot=kbd. | |
KbdGroup ...props | ComponentProps<'span'> | Inline-flex row with a 4px gap. | |
KbdShortcut children | string | A shortcut such as ⌘⇧P or Ctrl+Shift+K. Anything else is drawn as one cap. | |
KbdShortcut variant | "default" | "inverse" | "default" | Passed to every key. |
splitKeys(shortcut) | (string) => string[] | The parser behind KbdShortcut. |