Liking cljdoc? Tell your friends :D

x-kbd

A stateless, themeable inline element for rendering keyboard keys and shortcuts. Takes a combo string (or slotted content) and renders one styled <kbd> "cap" per key with separators between. Resolves Mod to on macOS and Ctrl elsewhere from the same authored markup.


Tag

<!-- combo, platform-aware -->
<x-kbd keys="Mod+K"></x-kbd>

<!-- explicit -->
<x-kbd keys="Ctrl+Shift+P" platform="win"></x-kbd>

<!-- slot mode (custom content as a single cap) -->
<x-kbd>Esc</x-kbd>

Attributes

AttributeValuesDefaultDescription
keysstringCombo string, e.g. "Ctrl+Shift+P". Split on separator; whitespace trimmed; empty tokens dropped. Empty/absent → slot mode.
separatorsingle character+Used to split keys. Also rendered between caps.
sizesm | md | lgmdDrives font-size and padding via data-size on the host.
platformauto | mac | win | linuxautoauto detects via navigator.userAgentData.platform (with navigator.platform fallback) at connectedCallback time.
labelstringVerbatim aria-label on the host. Overrides the auto-derived combo label.

Properties

PropertyTypeReflects attribute
keysstringkeys
separatorstringseparator
sizestringsize
platformstringplatform
labelstringlabel

No events, no public methods.


Slots

SlotDescription
(default slot)Consulted only when keys is empty/absent. Slotted content is rendered as one cap.

Parts

PartDescription
baseInline-flex container for caps and separators.
keyEach individual <kbd> cap.
separator<span> between caps in combo mode.

CSS Custom Properties

VariableDefaultDescription
--x-kbd-bgvar(--x-typography-kbd-bg, rgba(0,0,0,0.06))Cap background.
--x-kbd-colorvar(--x-color-text, inherit)Cap text colour.
--x-kbd-border-colorvar(--x-typography-kbd-border, rgba(0,0,0,0.15))Cap border colour.
--x-kbd-border-radiusvar(--x-typography-kbd-radius, 0.25rem)Cap corner radius.
--x-kbd-paddingvar(--x-typography-kbd-padding, 0.1em 0.4em)Padding inside the cap.
--x-kbd-font-familyui-monospace, "SF Mono", Menlo, Consolas, monospaceCap font family.
--x-kbd-font-size0.875em (size variant overrides)Cap font size.
--x-kbd-shadowinset 0 -1px 0 rgba(0,0,0,0.08)Cap shadow.
--x-kbd-gap0.25remGap between caps and separators.
--x-kbd-separator-colorvar(--x-color-text-muted, currentColor)Separator text colour.

The --x-typography-kbd-* variables in the defaults above are optional override hooks, not tokens that x-typography pre-defines. If a consumer sets them anywhere up the cascade (e.g. on :root), x-kbd picks them up automatically; otherwise the inner fallback applies. To customise x-kbd directly, set the --x-kbd-* variables.

Light/dark mode is handled automatically via @media (prefers-color-scheme).


Token mapping

keys tokens are matched case-insensitively. Unknown tokens (letters, F-keys, arrows, etc.) pass through verbatim.

Input token (case-insensitive)macwinlinuxaria name
ModCtrlCtrl"Command" / "Control"
Cmd | Command | Meta | SuperWinSuper"Command" / "Windows" / "Super"
Ctrl | ControlCtrlCtrl"Control"
Alt | OptionAltAlt"Option" / "Alt"
ShiftShiftShift"Shift"
Enter | ReturnReturnEnterEnter"Return" / "Enter"
anything elsepassthroughpassthroughpassthroughpassthrough

Use Mod whenever you want the canonical "primary modifier" — on macOS, Ctrl elsewhere — without authoring the markup twice.


Accessibility

  • Each cap is a real <kbd> element so AT and copy/paste behave correctly.
  • In combo mode, the host gets a derived aria-label joined with " plus " using each token's aria name (never the visible glyph). Example: keys="Mod+K" on macOS renders ⌘ K but the host's aria-label is "Command plus K".
  • Author-supplied label always overrides the derived value verbatim — including in slot mode (where there is no derived value to override).
  • In slot mode without an explicit label, the host carries no aria-label — the slotted <kbd> is already semantic.
  • Decorative only: no focus, no event listeners, no keyboard handling. The component renders shortcuts; it does not invoke them.

Examples

<!-- Cross-platform shortcut -->
Press <x-kbd keys="Mod+K"></x-kbd> to open the palette.

<!-- Pinned to a platform (e.g. in docs targeting Windows users) -->
<x-kbd keys="Ctrl+Shift+P" platform="win"></x-kbd>

<!-- Single key, slot mode -->
<x-kbd>Esc</x-kbd>

<!-- Custom separator -->
<x-kbd keys="Cmd-K" separator="-" platform="mac"></x-kbd>

<!-- Size variants -->
<x-kbd keys="Mod+S" size="sm"></x-kbd>
<x-kbd keys="Mod+S" size="lg"></x-kbd>

<!-- Author-supplied aria-label -->
<x-kbd keys="Mod+P" label="open command palette"></x-kbd>

<!-- Arrow keys (passthrough) -->
<x-kbd keys="↑+↓+←+→"></x-kbd>

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close