Skip to content
Nana UI

Data Display & Media

Kbd

A keyboard key, or a shortcut spelled as one key per press, shown with the right modifier for the reader's platform.

View .md Download

Search with

Open the command list with

Close with Esc

Search with ControlK

Open the command list with ControlShiftP

Close with Esc, or press ? for help

Install the package for a versioned dependency:

npm install @nana-tec/ui-components

Or copy the source in and own it:

npx @nana-tec/ui add kbd --framework web-components
npx @nana-tec/ui add kbd --framework svelte

Lands in src/lib/components/ui/kbd/ as NanaKbd.svelte, styled with Tailwind classes that resolve to the design tokens. Requires Tailwind v4.

Import the autoloader once, in your app's entry point. Every <nana-*> tag then works anywhere, and each component downloads only the first time a page uses it:

import "@nana-tec/ui-components/autoloader";
import "@nana-tec/ui-components/theme";

The second line gives every component the Nana look and the design tokens behind it. Leave it out to style the components entirely yourself.

Import a component directly when it must be ready at once: on screen at first paint, or when you call its methods straight away. The two mix freely:

import "@nana-tec/ui-components/kbd";

Copied in with npx @nana-tec/ui add kbd? Import your project's own @/lib/nana-ui/autoloader.js once instead; the CLI keeps it up to date.

<script lang="ts">
import { NanaKbd } from '$lib/components/ui/kbd';
</script>
<NanaKbd keys="mod+k" />
<NanaKbd>Esc</NanaKbd>

Set keys to a shortcut written with + between the keys. Each key is drawn on its own, inside one <kbd> for the whole combination, which is how HTML marks up a key combination.

mod is the main modifier: ⌘ on Apple devices and Ctrl elsewhere, so one shortcut reads correctly on both. On Apple devices ctrl, alt, shift, enter, backspace and tab show their symbols too.

WriteAppleElsewhere
mod+k⌘ KCtrl K
mod+shift+p⌘ ⇧ PCtrl Shift P
alt+enter⌥ ↵Alt Enter
escEscEsc

Any other key is shown as written, with its first letter capitalised.

Without keys, the content is shown as one key.

<nana-kbd>Esc</nana-kbd>
<nana-kbd size="sm">?</nana-kbd>
<NanaKbd>Esc</NanaKbd>
<NanaKbd size="sm">?</NanaKbd>

A symbol is hidden from screen readers and followed by the key’s name, so ⌘ is read as “Command” rather than as an unnamed character. Kbd only shows a shortcut; the shortcut itself is up to your app.

Properties

PropertyAttributeTypeDefaultDescription
keyskeysstring''A shortcut with + between keys, such as mod+k. mod is ⌘ on Apple devices and Ctrl elsewhere. Without it, the content is shown as one key.
sizesizeKbdSize'md'Size preset.

Slots

SlotDescription
(default)The key's text, used when keys is not set

CSS parts

PartDescription
::part(keys)The combination around the keys
::part(key)Each key

CSS custom properties

PropertyDescription
--nana-kbd-bgKey background
--nana-kbd-borderKey border and bottom edge
--nana-kbd-colorKey text colour

NanaKbd

PropTypeDefault
sizeKbdSize'md'
keysstring
childrenSnippet

Also accepts Omit<HTMLAttributes<HTMLElement>, 'class'>.