Components · Data Display
Kbd
A keyboard key in Space Mono inside a cap, one key per cap: sm as a hint in menus, tooltips, the search trigger and inputs, md where the keys are the content, as in a list of shortcuts.
import { Kbd } from "@botiverse/raft-ui";Kbd shows a keyboard key in a cap, one key per cap. At sm, the default, it is quiet metadata beside something else: a menu item, a tooltip, the search trigger. At md the keys are the content, as in a list of shortcuts. KbdShortcut draws a whole shortcut from key names, for the platform the page runs on. Kbd is never a control of its own.
When to use
Anatomy
Kbd: one key in a cap. It renders akbd.KbdGroup: holds severalKbd, 4px apart, for a shortcut written key by key, such as one that exists on one platform only. Hide it (aria-hidden) and say the shortcut in words in a visually hidden span beside it.KbdShortcut: a whole shortcut from key names,keys={["mod", "shift", "enter"]}. It renders aKbdGroupof caps, one per key, and the shortcut in words for a screen reader. It is exported from@botiverse/raft-ui/wipwhile its API settles.
Sizes
md uses radius-sticker, the radius of inline marks (Shape). sm keeps the 3px radius it has always had, so the hints already in menus, tooltips and search don't change. The line is 1px, not the ink border: Kbd is not a control.
Shortcuts from key names
Write the keys by name and let KbdShortcut draw them. It reads the platform once on the client (useKeyboardPlatform) and draws other on the server and while hydrating, so a server-rendered page shows Ctrl on a Mac until it hydrates. Pass platform="mac" or platform="other" where the platform is known, and in docs and tests.
- Write
modfor the platform's command key, ⌘ on a Mac and Ctrl elsewhere. Writectrlonly for the literal Control key, andmetaonly for the literal Command key, which is the Windows key on Windows and Linux. - Names are case-insensitive, and
KeyboardEvent.keyvalues work as names:ArrowUp,Control,Meta,Escape," "for Space. An alias sorts as the modifier it names. - Two names for one key on a platform draw one cap, and log a warning in development:
["mod", "ctrl", "k"]is Ctrl K on Windows and Linux. An empty name is skipped, with a warning too. - Modifiers are drawn in the platform's order, whatever order you write them in: ⌃ ⌥ ⇧ ⌘ on macOS (Apple's order), so
["mod", "shift", "enter"]draws ⇧ ⌘ ↩; Ctrl, Alt, Shift on Windows and Linux. The other keys follow in the order written. - On macOS, modifiers, Return, Delete and the arrows are Lucide icons, at 14px in
mdand 12px insm, centered in the cap. The fallback fonts that draw ⌘ ⇧ ↩ give each a different size and baseline. - Esc is the word Esc on every platform: Lucide has no Escape icon, and ⎋ is not widely recognized.
- ASCII letters are uppercase; other characters stay as written (ß stays ß, which uppercases to two letters). Letters and words are one font size, trimmed to cap height (
text-box: trim-both cap alphabetic), so they center on the same line as the icons. Firefox has notext-boxyet: there the text centers on its line box, about 1px off the icons.
Spacing and shape
- One key per cap, never a "+" inside a cap. Caps in a group sit 4px apart.
- In a
KbdShortcut, every cap at a size is one height, 24px atmdand 16px atsm, and at least as wide, so a single key is square. An icon cap has no side padding for that reason. A baresmKbd keeps the height of its line and its side padding. - In a list row, put the shortcut at the trailing edge, centered on the row, at least 16px from the text.
Show alternatives as two KbdShortcut joined by a "/" in foreground-hint, 6px on each side at md and 4px at sm, with a visually hidden "or" for a screen reader. KbdShortcut takes one shortcut, so you write the separator:
Behavior
- Kbd ignores the pointer and can't be selected: a click goes to the row or the button it sits in.
- Inside a tooltip,
smcaps switch to the tooltip'sforeground-on-hudcolors. The tooltip sets the 6px gap before a Kbd or aKbdGroup, not before each cap in a group. Never putmdin a tooltip: the tooltip forces thesmmetrics but keepsmd's fill.
Menus
A menu draws its shortcut with no cap, as native menus do: DropdownMenuShortcut and ContextMenuShortcut are a bare sm Kbd. They take the shortcut as a string; write it the platform's way (⌘⇧C on a Mac, Ctrl+Shift+C elsewhere), where a "+" is fine because there is no cap to put it in.
Content
- Let the label beside the shortcut say what it does. Kbd is a hint, not the label.
Accessibility
- Kbd renders the native
kbdelement and adds no role. KbdShortcuthides its caps (aria-hidden) and says the shortcut in words in a visually hidden span, in the order the caps are drawn: "Shift Command Return" on a Mac, "Control Shift Enter" elsewhere. The words are English; passlabelwith the product's own words in another language.- Never rely on
aria-labelon akbdor aspan: they have no role, so screen readers ignore the name. A Kbd orKbdGroupyou write by hand with symbols is hidden (aria-hidden) and followed by a visually hidden span (sr-only) that says it in words: Command K. Symbols are read inconsistently by screen readers. - Between alternatives, hide the "/" and add a visually hidden "or".
Rules
- Never make Kbd clickable, and never use it as the label of a button. A shortcut people can change goes inside a KbdButton, which is the control; the caps in it stay Kbd.
- Put Kbd at the end of what it belongs to: after a menu item's label, after a tooltip's text.
- Write key names, never glyphs or "+", when you use
KbdShortcut: the platform picks the drawing. - Use
mdonly where the keys are the content. Beside a label in a tooltip, a menu or a field,mdoutweighs the label it serves.
API
Kbd
Also takes the attributes of its element.
KbdGroup
Takes the attributes of its element.
KbdShortcut
Also takes the attributes of its element.
Related
Changes
- 0.67.0 · AddedKbdButton, KbdButtonPlaceholder and KbdButtonCaret (
@botiverse/raft-ui/wip): the button around a shortcut people can change, with aKbdShortcutatmdin it. At rest it is the caps alone, its 32px box 4px around them and taken back by its margin, so the caps sit where a read-only shortcut sits; hover lays the medium tier and a hairline, keyboard focus the ring.recordingmakes it a field the control height, with the focus ring after keyboard input (data-recording), for the held keys, a prompt and a caret;invalidtints the caps and the field's border danger (data-invalid). Name it witharia-labeland passaria-keyshortcuts. - 0.60.0 · FixedTooltip: a shortcut written as a
KbdGroupsits 6px after the text like a singleKbd, and its caps are no longer taken for the tooltip's leading icon.