Kbd
Show keyboard keys and shortcuts as keycaps.
import { Kbd, KbdGroup } from '@youj/react'
Installation & usage
npm i @youj/reactFor an app already set up with npx @youj/cli init, which installs the peers (@youj/core, @youj/motion, motion and Base UI) and wires the provider. New app? Start at Installation.
Use Kbd wherever you name a key: a shortcut beside a command, a hint in a footer, a step in the docs. It's the right element for keyboard input, not a styled code or span. Don't use it for arbitrary labels or counts. Those are Badges.
import { Kbd, KbdGroup } from '@youj/react'
export function Shortcut() {
return (
<KbdGroup>
<Kbd>⌘</Kbd>
<Kbd>K</Kbd>
</KbdGroup>
)
}A single key is a Kbd. A combination is a KbdGroup of Kbds, which is nested <kbd> elements, the way HTML spells keys pressed together. For a sequence, like G then S, put the word between the keys, as in the demo.
Description
A shortcut list with key combinations and sequences, and a footer of key hints.
Keycaps inside a search button, a tooltip and a Deploy button.
Keycaps sit inside buttons and tooltips at the right size, and an icon inside a keycap shrinks to 12px. In a tooltip they restyle themselves for its chip, in both modes. Hover or focus the middle button to see one.
Keycaps in a sentence of help text.
Keycaps sit on the text's baseline, so keys can go mid-sentence: in help text, onboarding tips and docs. A keycap is 20px tall, the height of a line of UI text. As a direct child of a paragraph, list item, table cell, quote or caption it trims its block margins, so a line with a key is no taller than one without; leading-6 still gives the keycaps more air.
Code
kbd-demo.tsx, the Demo story. The component itself comes from the package.
Reference
PropsOptions
Reduced motionKbd is static
Reduced motion
Kbd is static. Nothing changes under reduced motion.
AccessibilityKeyboard, ARIA
Accessibility
Kbdrenders<kbd>, HTML's element for keyboard input. It ignores the pointer, so it never steals a click from the control around it.- Symbols such as ⌘, ⌥ and ⇧ are read inconsistently. When the shortcut matters, write it out in the surrounding text, or give the icon inside a keycap an
aria-label, as the arrow keys in the preview do. - Inside a button, the keycaps are a hint, not part of the button's name. Mark them
aria-hidden. When the shortcut really works, expose it witharia-keyshortcutson the button.