Kbd
Alignment target:@astryxdesign/core v0.7.1 · Kbd
Usage #
Kbd presents a keyboard shortcut as a small group of painted key badges, such as ⌘K or ⇧↵. It belongs next to the action it triggers — on the right of a menu item, inside a tooltip, or at the end of a line of help text — so you can mention the shortcut without writing out "hold Command and press K".
Pass the shortcut as one string and separate the keys with +:
<script setup lang="ts">
import { Kbd } from '@astryx-vue/core/Kbd'
</script>
<template>
<Kbd keys="mod+k" />
</template>Every segment becomes its own badge, so mod+k paints two keys. Whitespace around a segment is ignored and the case does not matter, which makes mod + K and mod+k the same shortcut. Anything the component does not recognize is uppercased as written, so f1 shows as F1.
The modifiers follow the platform: on Apple platforms they paint Apple's symbols, and elsewhere they paint the text printed on the keycap. A server render does not know the platform yet, so before hydration every modifier shows its text. mod is the Command key on Apple platforms and Control elsewhere, so reach for mod when you describe a shortcut that works across platforms, and use ctrl only when the shortcut really is bound to the Control key. The other named keys have a fixed glyph:
| Written as | Painted as |
|---|---|
mod | ⌘ on Apple platforms, Ctrl elsewhere |
ctrl | ⌃ on Apple platforms, Ctrl elsewhere |
alt | ⌥ on Apple platforms, Alt elsewhere |
shift | ⇧ on Apple platforms, Shift elsewhere |
meta | ⌘ on Apple platforms, Win elsewhere |
enter | ↵ |
backspace | ⌫ |
tab | ⇥ |
escape | Esc |
up / down / left / right | ↑ ↓ ← → |
plus | + |
esc is an alias for escape, and return is an alias for enter; an alias paints and is announced exactly like the key it names. Because + separates the keys, write plus for the plus key itself, as in shift+plus.
To a screen reader the whole group is one image named "Command + K"; the glyphs inside the badges are hidden because ⌘ and ⇧ are announced as nothing useful. The keys are spoken as words instead. Note that Kbd only presents a shortcut: it does not listen for keystrokes, and the shortcut still has to be implemented by the product.
Each key badge keeps the theme's height at the default text size and grows with a larger text size, so a bigger glyph stays inside its badge.
Anatomy
| Element | Description |
|---|---|
| Shortcut | Group that presents the complete keyboard shortcut and its accessible name. |
| Key badge | Painted key badge rendered once for each key in the shortcut. |
Best practices #
| Guidance | Practices |
|---|---|
| Do | Place the shortcut next to the action it triggers: on the right of a menu item, in a tooltip, or straight after the wording it belongs to. |
| Do | Describe a cross-platform shortcut with mod rather than ctrl or cmd, so macOS users read ⌘ and everyone else reads Ctrl. |
| Do | Only mention shortcuts that exist and can actually be pressed in the current context. A wrong hint is worse than no hint. |
| Do | Spell shortcuts the same way as the rest of the product, so documentation and settings never disagree. |
| Don't | Treat Kbd as the only way to discover an action. Shortcuts supplement visible controls, they do not replace them. |
| Don't | Scatter shortcut hints across a screen. They are supporting information; in bulk they drown out the content. |
| Don't | Hand-write ⌘ or ⇧ characters instead of using the component. Hand-written glyphs do not follow the platform, and screen readers cannot announce what they mean. |
Examples #
Kbd — Inline instructions
Keyboard shortcuts rendered inline within instructional text.
Kbd — Menu shortcuts
- CutCtrlX
- CopyCtrlC
- PasteCtrlV
- UndoCtrlZ
- RedoCtrlShiftZ
Menu-style list pairing action labels with their keyboard shortcuts.
Kbd — Modifier combinations
Modifier combinations and special keys rendered as shortcut badges.