Skip to content

Fragments 0.12.2 is out.Read the changelog

Components

Kbd

Draws a keyboard key on a keycap, alone or as one shortcut.

import { Kbd } from "@usefragments/ui";Source

Preview

<Kbd>Esc</Kbd>

Usage

Use when

  • Showing the keyboard shortcut for an action
  • Naming a key inside help copy
  • A shortcut hint in a menu row, tooltip or field
  • A shortcut that differs by platform: Kbd.Shortcut keys="Mod+K", and ariaKeyShortcuts(keys, platform) on the control

Avoid when

  • Inline code or a command to copy — use CodeBlock or Text type="code"
  • A status or count — use Badge
  • Anything clickable — keys are hints, not buttons

Implementation notes

  • One key per Kbd; the keys of one shortcut go in Kbd.Group
  • Glyph keys carry a label so they are spoken by name
  • Print the platform's own glyphs (⌘ on macOS, Ctrl elsewhere)

Installation

pnpm add @usefragments/ui

Import styles once from @usefragments/ui/styles. See the installation guide for framework and theme setup.

Anatomy

Import Kbd once, then compose its parts with dot notation.

  • Kbd.Group
  • Kbd.Shortcut

Examples

Shortcut

The keys of one shortcut, the glyph spoken by name.

<Kbd.Group>
  <Kbd label="Command">⌘</Kbd>
  <Kbd>K</Kbd>
</Kbd.Group>

API reference

Component props
Prop
Type
Default
Description
childrenRequiredReact.ReactNodeNot setThe key as it is printed: a glyph or a short word
Group.childrenRequiredReact.ReactNodeNot setThe keys of one shortcut, pressed together, as `Kbd` children.
labelstring | undefinedNot setThe key's spoken name for a glyph a screen reader cannot say. The glyph is then hidden from assistive tech.
Shortcut.keysRequiredstringNot setThe shortcut by key name, joined with `+`: `"Mod+K"`, `"Mod+Alt+B"`, `"Shift+Enter"`. `Mod` is ⌘ on a Mac and Ctrl elsewhere; `Alt` is ⌥ and Alt.
Shortcut.platformKbdPlatform | undefinedothermacNot setDraw for this platform instead of the one the page runs on.
Shortcut.toneKbdTone | undefinedneutralon-accentNot setEach cap's tone; `on-accent` on an accent fill.
toneKbdTone | undefinedneutralon-accentneutralon-accent draws the cap on an accent fill: the accent darkened, an 18% ring and words in its on-fill ink.

Accessibility

  • Renders native kbd elements; a shortcut is a kbd that holds kbds
  • label hides the glyph and speaks the name instead
  • A keycap never takes focus
  • TooltipcompositionA tooltip names an action and shows its shortcut as a Kbd
  • MenucompositionA menu row shows its shortcut as a Kbd on the trailing edge

Next steps

Was this page helpful?