|Kit

Kbd

Polymorphic

Keyboard-key and shortcut indicator. Shares Button's variant × color system, tuned into a display key cap.

Kbd is a non-interactive <kbd> styled with the kit's (variant × color) system. The shape and palette axes are the exact set Button exposes, so a shortcut hint speaks the same accent language as the action it labels — but without hover or click behavior.

import { Kbd } from '@42-central/ui-react/kbd';

<Kbd>⌘</Kbd>
<Kbd>⌘K</Kbd>
<Kbd variant="light" color="brand">Enter</Kbd>
<Kbd hotkey="mod+k" /> {/* [⌘] [K] on a Mac, [Ctrl] [K] elsewhere */}

Playground

Component

Loading preview

Props

Code

<Component variant="default" size="md">
  ⌘
</Component>

Full prop browser

Variants

The five shape treatments are exactly the set Button uses. The default variant is default — the neutral, bordered key cap that reads as a physical key: a translucent fill that adapts to whatever surface it sits on, a hairline border, and a 1px shade along the bottom edge for the lip of a real key.

⌘⌘⌘⌘⌘

Code

<Kbd variant="default">⌘</Kbd>
<Kbd variant="filled">⌘</Kbd>
<Kbd variant="light">⌘</Kbd>
<Kbd variant="outline">⌘</Kbd>
<Kbd variant="subtle">⌘</Kbd>

default is palette-independent — color is ignored for fill/text on this variant.

Colors

Any palette color pairs with any variant except default. Reach for a color when the key belongs to a colored action (a green confirm, a red destructive shortcut).

BNROYGTCBPP
BNROYGTCBPP

Code

<Kbd variant="light"  color="green">↵</Kbd>
<Kbd variant="filled" color="red">⌫</Kbd>
<Kbd variant="outline" color="blue">Tab</Kbd>

Sizes

Each size steps the type by one Tailwind step — sm 12px, md 14px, lg 16px — so pick the one that matches the text the key sits beside: sm inside a Button or a search field, md in body copy. Single-character keys stay square (a per-size min-w equal to the height); wider labels grow horizontally. An icon inside a key draws a 1.5px stroke at every size.

⌘Esc
⌘Esc
⌘Esc

Code

<Kbd size="sm">⌘</Kbd>
<Kbd size="md">⌘</Kbd>
<Kbd size="lg">⌘</Kbd>

In context

A Kbd slots cleanly into a Button to surface its shortcut. The button drives layout; the key cap stays neutral.

Code

<Button variant="outline" color="neutral" startSlot={<Plus />} endSlot={<Kbd size="sm">⌘N</Kbd>}>
  New file
</Button>

It keeps its own size in an input's slot too, beside the field's own icon: a search field shows its shortcut at the end.

Code

<Input
  startSlot={<Search />}
  endSlot={<Kbd hotkey="mod+k" size="sm" />}
  placeholder="Search"
/>

From a hotkey

hotkey takes the same string as useHotkey and draws it the way the visitor's platform does, one cap per key: "mod+k" is K on a Mac and Ctrl K elsewhere. Bind and label a shortcut from one string, and the hint can't drift from the key that works.

CtrlKCtrlShiftPCtrlEnter/

Code

<Kbd hotkey="mod+k" />
<Kbd hotkey="shift+mod+p" />
<Kbd hotkey="mod+enter" />
<Kbd hotkey="/" />

The demo shows your own platform's caps. A key with an honest icon is drawn with the same icon on both platforms: Shift, Enter, Tab and the arrows. Apple's own symbols (⌘ ⌥ ⌃) are icons only on Apple; elsewhere Ctrl, Alt and Meta stay words, as do Esc and Space everywhere. Backspace is an icon only on Apple too: elsewhere ⌫ reads as the Left Arrow, and the keyboard prints the word, so it stays Backspace. Modifiers come in Apple's order (⌃⌥⇧⌘) on both, whatever order the string names them in.

KeyAppleWindows & Linux
mod / metaCtrl / Meta
ctrlCtrl
altAlt
shift
enter
backspaceBackspace
tab
arrowup (and the other arrows)
escape, spaceEsc, SpaceEsc, Space

The markup is the HTML spec's for a key combination: an outer <kbd> holding one <kbd> per key, with no + between them.

<kbd class="inline-flex gap-0.5" dir="ltr">
  <kbd class="…cap…"><svg aria-hidden="true">…</svg><span class="sr-only">Command</span></kbd>
  <kbd class="…cap…">K</kbd>
</kbd>
  • Styling. variant, size and color style every cap, so a single-character cap stays square. The outer <kbd> is an unstyled row.
  • The other props (className, testId, aria-*, …) go on the outer <kbd>, the element a layout places. asChild makes its child that outer element, with the caps inside it.
  • Direction. The row is dir="ltr": a combination reads left to right on a right-to-left page too.
  • Server rendering. The server can't know the platform, so it renders the non-Apple caps and hydration corrects them on Apple devices.
  • Plain text. For a title or a tooltip, formatHotkey writes the same keys as one string (⌘K, Ctrl K), and hotkeyKeys lists them.

The caps are the content, so hotkey takes no children.

Translations

The words a hotkey cap shows (Ctrl, Esc) and the names a drawn cap is spoken by ("Command", "Up Arrow") are English by default. translations replaces them per key; the keys you leave out stay English. A visible word is read as itself, so translating it translates what a screen reader says too.

DeutschStrgShiftEntfStrgEingabeEscPfeil nach oben
FrançaisCtrlMajDelCtrlEntréeÉchapFlèche haut

Code

const german = { ctrl: "Strg", del: "Entf", enter: "Eingabe", escape: "Esc", up: "Pfeil nach oben" }
const french = { escape: "Échap", enter: "Entrée", return: "Retour", command: "Commande", shift: "Maj", up: "Flèche haut" }

const hotkeys = ["mod+shift+delete", "mod+enter", "escape", "arrowup"]

{hotkeys.map((hotkey) => <Kbd key={hotkey} hotkey={hotkey} translations={german} />)}
{hotkeys.map((hotkey) => <Kbd key={hotkey} hotkey={hotkey} translations={french} />)}

Off Apple the German row shows Strg Shift Entf. On a Mac it shows glyphs and speaks Apple's names, which german leaves in English ("Shift Command Forward Delete"): set command and forwardDelete too to cover both platforms.

Apple and the other platforms name some keys differently, so each spelling is its own key, named after the English string it replaces: ↵ is spoken "Return" on a Mac (return) and "Enter" elsewhere (enter). Apple's glyphs (⌘ ⇧ ⌦) aren't words and never change. Intl has no display names for keys, so there is nothing to derive from the locale; the kit ships these English defaults as KBD_DEFAULT_TRANSLATIONS and leaves the other languages to you.

import { KBD_DEFAULT_TRANSLATIONS, type KbdTranslations } from '@42-central/ui-react/kbd';
KeyDefaultAppleWindows & Linux
command"Command"⌘, spoken
meta"Meta"Meta, shown
control"Control"⌃, spoken
ctrl"Ctrl"Ctrl (and mod), shown
option"Option"⌥, spoken
alt"Alt"Alt, shown
shift"Shift"⇧, spoken⇧, spoken
return"Return"↵, spoken
enter"Enter"↵, spoken
delete"Delete"⌫, spoken
backspace"Backspace"Backspace, shown
forwardDelete"Forward Delete"⌦, spoken
del"Del"Del, shown
tab"Tab"⇥, spoken⇥, spoken
up, down, left, right"Up Arrow", "Down Arrow", "Left Arrow", "Right Arrow"↑ ↓ ← →, spoken↑ ↓ ← →, spoken
escape"Esc"Esc, shownEsc, shown
space"Space"Space, shownSpace, shown
pageUp, pageDown"PgUp", "PgDn"PgUp, PgDn, shownPgUp, PgDn, shown

"Spoken" is a cap drawn as an icon or a glyph, whose name is visually hidden text; "shown" is a word cap, read as itself. Letters, digits and punctuation are shown as typed. For plain text, pass the same object to formatHotkey and hotkeyKeys, and the text reads like the caps. Spotlight takes these words as its own translations.kbd and forwards them to its footer.

Composing chords

A chord is just a row of keys. Render one Kbd per key and separate them with a +:

⌘ + ⇧ + P

Code

<span className="inline-flex items-center gap-1">
  <Kbd>⌘</Kbd> + <Kbd>⇧</Kbd> + <Kbd>P</Kbd>
</span>

API

Prop

Type

All other props are forwarded to the underlying <kbd> (or asChild element).

Accessibility

Kbd renders a semantic <kbd> element, the right markup for user input / keyboard keys. With hotkey, a key drawn as an icon is aria-hidden and carries its name as visually hidden text, in the platform's own words: "Command", "Shift", "Return" on a Mac, "Shift", "Enter", "Tab" elsewhere, or the words you give translations. So "mod+k" reads "Command K", and "Ctrl K" off Apple. A key written as a word reads as itself. If you draw a symbol yourself (<Kbd>⌘</Kbd>), screen readers may read it inconsistently; prefer hotkey. It is a display element — no hover treatment, no click handling. If a key should be operable (a clickable shortcut row), use asChild with a real <button> so semantics line up; the kit's focus ring rides along, so the key is keyboard-visible without any className of your own:

<Kbd asChild>
  <button type="button" onClick={openPalette}>
    ⌘K
  </button>
</Kbd>

Changelog

v0.30.0
  • Inline code and single keys read left to right on RTL pages

    #490
v0.21.0
  • Components in an input slot keep their own icon size: a Kbd in endSlot no longer draws ⌘ at 16px, and a size-* class on a slotted icon now wins

    #427
v0.20.0
  • The footer draws its hints as <Kbd hotkey> caps with terse labels, translated through translations.kbd

    #410
  • <Kbd hotkey="mod+k" /> draws a useHotkey string for the visitor's platform as one key cap per key with icons and spoken names; formatHotkey() and hotkeyKeys() return the same keys as text

    #401
  • translations on <Kbd hotkey> replaces the words its caps show and speak ("Strg", "Flèche haut"), merged per key onto KBD_DEFAULT_TRANSLATIONS

    #410
  • A single character or lone icon is a square cap at every size

    #401
v0.18.2
  • Kbd is redesigned as a key cap: one type step larger per size (12/14/16px), regular weight, a visible fill in light mode, a softer label and a shaded bottom edge. The API is unchanged

    #302
v0.13.0
  • Reconcile the card border and withBorder dividers to one hairline

    #193
v0.9.2
  • Carry the kit's focus ring, so composing one onto a real control with asChild is keyboard-visible

    #142
v0.3.1
  • Correct dark-mode border shades on Kbd, Notification, FileUpload and ChoiceCardGroup

    #37
v0.0.1
  • Kbd: keyboard-key labels

On this page