Skip to the content
human-kit design
52 — Data display

Kbd

A key to press, written in a line of text, alone or as a combination.

Press CtrlK to find a patient.

Installation

pnpm dlx @human-kit/design add kbd
npx @human-kit/design add kbd
yarn dlx @human-kit/design add kbd
bunx @human-kit/design add kbd

The command copies the files to src/lib/components/ui/kbd. It also installs the packages that the files import.

Usage

<script lang="ts">
	import * as Kbd from '$lib/components/ui/kbd';
</script>

<p>Press <Kbd.Root keys={['Ctrl', 'K']} /> to search, or <Kbd.Root>Esc</Kbd.Root> to close.</p>

Examples

Beside an action

The ghost keys sit quietly at the end of a button that has the same shortcut. Put the shortcut in aria-keyshortcuts too: the chips are only drawn.

Guidelines

  • Use outline in a line of text, where the edge makes the key read as something on the keyboard.
  • Use ghost beside other words or on a surface that has edges of its own: a button, a menu row, a tooltip.
  • Write a combination with keys, one chip per key. Give a single key as children.
  • Write the keys as the keyboard prints them: "Ctrl", "Shift", "Esc", "⌘". On a site used on Mac and Windows, show the one of the platform.
  • Show a shortcut where it is learned, beside the action or in its tooltip, not in a list nobody reads.

Accessibility

  • It renders <kbd>, the element for keyboard input. A combination is one <kbd> holding a <kbd> per key, as the HTML spec writes it.
  • A screen reader reads the text of the keys. Spell them as words where a symbol would be read poorly or not at all.
  • It is only a picture of the keys. The element that has the shortcut says so with aria-keyshortcuts.

API reference

PropTypeDefault
variant'outline' | 'ghost'

A chip with an edge, or the quieter hover grey.

'outline'
keysstring[]

The keys of a combination, each in its own chip: ['Ctrl', 'K'].

—
childrenSnippet

One key, in place of keys.

—

Plus every attribute of a <kbd>, on the outer element.