Kbd

Display keyboard keys and shortcuts as soft keycaps that read the user's platform

Primitive

Kbd renders a keyboard key as a soft keycap: a translucent fill with a one-pixel lip, both taken from the color of the text around it, so it reads on the page, in a tooltip and on a primary button without a variant per surface. Pass keys to draw a whole shortcut on one cap, and named keys like mod and shift become the right legend for the user's platform: Command plus K on a Mac, Ctrl+K everywhere else.

Preview

Open command palette
Command plus K
Save document
Command plus S
Find and replace
Command plus Option plus F
Toggle sidebar
Command plus B

Installation

Usage

Composition

Examples

Single Keys

Put the legend in children for a key you spell out yourself.

⌘KEscTab↵

Shortcuts

Use keys to draw a shortcut on one cap. Mac legends sit side by side; elsewhere they're joined with +. Set separator to change that.

Command plus KCommand plus Shift plus POption plus TabCommand plus K

Named Keys

Named keys are case-insensitive and turn into the platform's legend. Symbols carry a hidden label, so a screen reader says "Command" rather than the glyph's name. Any other single character is uppercased; longer names are drawn as written.

modCommand
ctrlControl
altOption
shiftShift
enterReturn
backspaceDelete
tabTab
escEsc
spaceSpace
upUp arrow
downDown arrow
leftLeft arrow
rightRight arrow
KeyMacWindows and Linux
mod (cmd, command)⌘Ctrl
meta⌘Win
ctrl (control)⌃Ctrl
alt (option)⌥Alt
shift⇧Shift
enter (return)↵Enter
backspace⌫Backspace
delete⌦Del
tab⇥Tab
capslock⇪Caps Lock
esc (escape), spaceEsc, SpaceEsc, Space
up, down, left, right↑ ↓ ← →↑ ↓ ← →

Platform

platform defaults to "auto", which reads the user's OS. Set "mac" or "windows" to pin one. A key rendered on the server shows Mac legends until it hydrates; keys in menus, dialogs and tooltips mount on the client and are right from their first frame.

AutoCommand plus Shift plus PMacCommand plus Shift plus PWindowsCtrl plus Shift plus P

Group

Wrap several Kbd in KbdGroup to show a shortcut as separate caps, or a sequence with words between them. The group is an outer kbd, the HTML way to mark up a combination. Put text like + or "then" in a span and it stays quiet.

CommandShiftPCtrl+Alt+DelGthenD

Variants

default is the filled keycap. outline takes the outline Button's fill and edge, for a key that sits on a muted plate; it keeps that surface's colors, so use default on a solid fill. ghost is the legend alone, for menus and dense rows.

Command plus KCommand plus KCommand plus K

Sizes

Command plus KCommand plus KCommand plus K
SizeHeightUse
sm18pxTooltips, menu rows, command footers.
md20pxRunning text and shortcut lists.
lg24pxKeyboards and anything you want to show.

In Text

Kbd sits on the text baseline, so it drops into a sentence without nudging the line.

Press Command plus K to open the command palette.

Use Up arrowDown arrow to move and Return to pick.

Press Esc to close any dialog.

In a Tooltip

The fill is mixed from the current ink, so the same key reads in the default tooltip and the inverse one.

In a Button

Pass the key as trailingIcon so it sits apart from the label. It wears the button's text color and takes a stronger fill, so it reads on a solid fill with no override. Hide the key with aria-hidden and name the shortcut with aria-keyshortcuts, so the button is still announced as "Save".

Pressed

pressed pushes the key down a pixel, drops its lip and darkens its fill. Type to try it.

QWERTYUIOP
ASDFGHJKL
ZXCVBNM
ShiftControlOptionCommandSpace

API Reference

Kbd and KbdGroup render kbd elements. Other props pass through to the element.

Props

Kbd

One key, or a whole shortcut drawn on a single cap.

KbdGroup

An outer kbd that lays out several keys as one shortcut. Takes standard kbd props.