Skip to content

Kbd

Faux keycap rendered as a native <kbd> with auto-swapped glyphs for modifier keys, arrows, return, and backspace.

  • stable
  • since v0.1.0
  • 3.88 kB
  • typography
  • shortcut

Uses the native <kbd> element so keyboard input is announced distinctly; swapped-in glyph icons are aria-hidden so the keycap text remains the spoken label.

Preview
Open
tsx

Three sizes — sm, md (default), and lg — scale padding, font-size, and icon glyphs together so a keycap sits cleanly next to body text of the matching size.

Preview
Open
tsx

Pass macOS-style glyphs (⌘, ⇧, ⌥, ⌃, ⌫, ↵, arrows) and Kbd auto-swaps them for rendered Lucide icons inside the keycap — never a bare Unicode character.

Preview
Open
tsx

Compose multi-key shortcuts by chaining <Kbd> elements with a literal + separator so the combination reads naturally when styling is unavailable.

Preview
Open
tsx

Kbd slots into a Text paragraph for shortcut hints in onboarding or help copy.

Preview
Open
tsx
PropTypeDefaultDescription
size
"sm"|"md"|"lg"
"md"
Keycap scale — adjusts padding, font-size, and inline-swapped icon size together so caps sit naturally alongside body text at the same step.
className
string
—
Additional class names appended after Cynosure's base classes.
style
CSSProperties
—
Inline style overrides merged last.
children
ReactNode
—
Keycap label. Strings are walked grapheme-by-grapheme so combined shortcuts (e.g. `"⌘R"`) swap known modifier glyphs for `lucide` icons.
padding
—
Padding on all four sides. Token from the spacing scale.
paddingX
—
Horizontal padding (left + right). Overrides `padding` on the X axis.
paddingY
—
Vertical padding (top + bottom). Overrides `padding` on the Y axis.
paddingTop
—
Padding on the top edge. Beats `padding` / `paddingY`.
paddingRight
—
Padding on the right (inline-end in RTL) edge.
paddingBottom
—
Padding on the bottom edge.
paddingLeft
—
Padding on the left (inline-start in RTL) edge.
margin
—
Margin on all four sides. Accepts the spacing scale or `"auto"`.
marginX
—
Horizontal margin (left + right). Use `"auto"` to centre.
marginY
—
Vertical margin (top + bottom).
marginTop
—
Margin on the top edge.
marginRight
—
Margin on the right edge.
marginBottom
—
Margin on the bottom edge.
marginLeft
—
Margin on the left edge.
width
—
Element width. Accepts a `SpaceToken`, `LengthValue`, or named alias.
height
—
Element height. Accepts a `SpaceToken`, `LengthValue`, or named alias.
minWidth
—
Minimum width — useful for preventing flex children from collapsing.
maxWidth
—
Maximum width — `"prose"` caps to a comfortable reading measure.
minHeight
—
Minimum height.
maxHeight
—
Maximum height.
background
—
Background colour. Use a `ColorToken` like `"bg.surface"` for theme awareness.
color
—
Text colour. Inherits unless set.
borderColor
—
Border colour. Pair with `borderWidth` to make the border visible.
borderWidth
—
Border thickness in scale steps.
borderStyle
—
Border line style.
borderRadius
—
Corner rounding token.
boxShadow
—
Drop-shadow token from the elevation scale.
opacity
Responsive<number | `${number}`>
—
Opacity 0–1. Use sparingly — prefer surface tokens over translucency.
overflow
—
How content that exceeds the box is handled.
overflowX
—
Horizontal-axis overflow control. Overrides `overflow` for the X axis.
overflowY
—
Vertical-axis overflow control.
display
—
CSS `display` value. `"contents"` removes the element's box from layout.
position
—
CSS `position`. Pair with `top`/`right`/`bottom`/`left` for placement.
top
—
Top inset (when positioned). Accepts a `SpaceToken`, `"auto"`, `"0"`, or `LengthValue`.
right
—
Right inset (when positioned).
bottom
—
Bottom inset (when positioned).
left
—
Left inset (when positioned).
zIndex
—
Stacking-context layer token (`"modal"`, `"tooltip"`, …).
gridColumn
Responsive<string>
—
Grid-column shorthand (e.g. `"1 / 3"`, `"span 2"`).
gridRow
Responsive<string>
—
Grid-row shorthand.
gridArea
Responsive<string>
—
Named grid area.
flex
Responsive<"1" | "none" | "auto" | "initial" | (string & {})>
—
Shorthand: `1 | auto | none | initial | <css>`. Resolves to `flex` on the child.
flexGrow
Responsive<number | `${number}`>
—
Grow factor inside a flex container.
flexShrink
Responsive<number | `${number}`>
—
Shrink factor inside a flex container.
flexBasis
Responsive<"content" | SizeValue>
—
Initial main-axis size before grow/shrink.
alignSelf
—
Override `align-items` for a single child.
justifySelf
—
Override `justify-items` for a single child (grid).
order
Responsive<number | `${number}`>
—
Flex/grid child reorder index.
asChild
boolean
—
When `true`, renders the primitive's single React child via `Slot`, forwarding className/style/ref/event handlers onto it.
  • Renders the semantic <kbd> element so keyboard input is announced distinctly from surrounding prose.
  • Swapped-in glyph icons (⌘, ⇧, arrows, etc.) are marked aria-hidden — the keycap’s text content is what assistive tech reads, so prefer including a readable label like <Kbd>Cmd</Kbd> over <Kbd>⌘</Kbd> when the audience may use a non-Mac keyboard.
  • Use the casing the user sees on their keyboard so the rendered hint matches the physical key.
  • Pair shortcut hints with the action they trigger; never rely on the keycap alone to convey the available command.
  • Pair with Text for inline hints: Press <Kbd>Esc</Kbd> to close.
  • Group Kbd elements inside command menus and tooltips to teach shortcuts contextually.
  • Use size="sm" next to a Text size="sm" line so keycaps don’t dominate dense rows.
  • Keep keycap content short — single keys or modifier names work best; combinations should be split across multiple <Kbd> elements.