Skip to main content

Kbd

Display one keyboard key as shortcut notation.

Press⌘Kto open command menu
Props
Slots

Anatomy#

Kbd [component; slot=root; <kbd>]

Usage#

Use Kbd to display one key in a shortcut hint. Combine keys with KbdGroup. Showing a shortcut does not register a keyboard handler.

import { Kbd } from 'moraine'

export function Example() {
  return <Kbd value="K" />
}

Key aliases#

Known aliases such as meta, command, shift, escape, and arrowup resolve to a symbol or short label (⌘, ⇧, Esc, ↑), matched case-insensitively. Any other value renders as written. Pass symbol={false} to render the value as written instead. An alias also gets a spoken name from the locale messages; translations overrides it per instance. Set aria-label when assistive technology needs a different name than the visible keycap. It takes precedence over the alias name.

<Kbd value="meta" />
<Kbd value="escape" symbol={false} />
<Kbd value="K" aria-label="Search" />

Root styling#

Kbd renders one <kbd> root, so use class and style for an individual keycap. Configure kbd.base.root in your Theme to share keycap presentation. variant (default, outline, invert) and size (sm, md, lg) select the built-in treatments. root is the only slot, so classes and styles only duplicate class and style.

<Kbd value="K" class="text-blue-600" />

Examples#

Hint search and cancel shortcuts#

Place keycaps in instructional copy. Registration of those shortcuts stays in application code.

import { Kbd } from 'moraine'

export function KbdUsage() {
  return (
    <div class="flex flex-wrap gap-3 items-center">
      <span class="text-sm text-muted-foreground flex gap-1.5 items-center">
        Press <Kbd value="command" /> <Kbd value="k" /> to search
      </span>
      <span class="text-sm text-muted-foreground flex gap-1.5 items-center">
        Press <Kbd value="escape" /> to cancel
      </span>
    </div>
  )
}

Show how to close a dialog#

Use a single Kbd when the hint is one key rather than a chord.

import { Kbd } from 'moraine'

export function ShortcutComposition() {
  return (
    <p class="text-sm text-foreground flex flex-wrap gap-2 items-center">
      Close dialogs with
      <Kbd value="escape" />
    </p>
  )
}

API Reference#

Props#

Props for the Kbd component. Renders a <kbd> element by default.

Prop