# Kbd

Shows a keyboard key or shortcut, such as ⌘ K.

Keyboard hint: one or more keys, as in "⌘ K". boxed draws each key as a small raised key (docs, palettes, empty states); plain is text in the surrounding colour (tooltips, menu rows).

Kind: component · Category: Display · Status: beta · Targets: React, Vue, Svelte

## Add

```bash
npx fndn add kbd
```

It writes the folder `kbd/` into the project's ui folder (`index.ts`, `kbd.tsx`, `Kbd.vue`, `Kbd.svelte`, `kbd.recipe.ts`). First time: [Installation](/installation.md).

## Usage

```tsx
import { Kbd } from "@/components/ui/kbd";
```

```vue
import { Kbd } from "@/components/ui/kbd";
```

```svelte
import { Kbd } from "$lib/components/ui/kbd";
```

## Parts

In the DOM the root carries `data-slot="kbd"` and each part `data-slot="kbd-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `kbd` | `kbd` |  | The whole shortcut. Nested <kbd> keys are how HTML marks up a key combination. dir="ltr": the keys keep their reading order on a right-to-left page (⌘ then K), as printed on the keyboard. |
| `key` | `kbd` | `kbd-key` | repeats | One key or one run of keys ("⌘", "K", "Ctrl+K"). |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `keys` | `string[]` | `[]` | The keys in order; each becomes one key part. |
| `variant` | `"boxed" \| "plain"` | `"boxed"` | boxed: 18px key, radius.chip, surface with shadow.btn, mono 11px ink-2. plain: 11px text in the inherited font and colour. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Keyboard and accessibility

Notes:

- Pair the hint with aria-keyshortcuts on the control it belongs to; the hint is for sight.
- Write symbols the platform shows (⌘ on Apple, Ctrl elsewhere); screen readers read the characters.

## Motion

- none

## Tokens

`color.surface`, `color.ink-2`, `shadow.btn`, `radius.chip`, `font.mono`, `text.2xs`

## Examples

### Keyboard shortcuts

React · `shortcut-list.tsx`

```tsx
import { Kbd } from "@/components/ui/kbd";

/* a shortcuts list: the action at the start, its keys at the end; a sequence reads "then" */
const shortcuts = [
  { action: "Search projects and people", keys: [["⌘", "K"]] },
  { action: "New project", keys: [["C"]] },
  { action: "Go to deploys", keys: [["G"], ["D"]] },
  { action: "Toggle the sidebar", keys: [["⌘", "B"]] },
];

export default function ShortcutList() {
  return (
    <dl className="flex w-80 flex-col gap-2 text-sm">
      {shortcuts.map((s) => (
        <div key={s.action} className="flex items-center justify-between gap-4">
          <dt className="text-ink-2">{s.action}</dt>
          <dd className="flex items-center gap-1 text-xs text-ink-2">
            {s.keys.map((k, i) => (
              <span key={i} className="contents">
                {i > 0 && "then"}
                <Kbd keys={k} />
              </span>
            ))}
          </dd>
        </div>
      ))}
    </dl>
  );
}
```

Vue · `shortcut-list.vue`

```vue
<script setup lang="ts">
import { Kbd } from "@/components/ui/kbd";

/* a shortcuts list: the action at the start, its keys at the end; a sequence reads "then" */
const shortcuts = [
  { action: "Search projects and people", keys: [["⌘", "K"]] },
  { action: "New project", keys: [["C"]] },
  { action: "Go to deploys", keys: [["G"], ["D"]] },
  { action: "Toggle the sidebar", keys: [["⌘", "B"]] },
];
</script>

<template>
  <dl class="flex w-80 flex-col gap-2 text-sm">
    <div v-for="s in shortcuts" :key="s.action" class="flex items-center justify-between gap-4">
      <dt class="text-ink-2">{{ s.action }}</dt>
      <dd class="flex items-center gap-1 text-xs text-ink-2">
        <span v-for="(k, i) in s.keys" :key="i" class="contents"><template v-if="i > 0">then</template><Kbd :keys="k" /></span>
      </dd>
    </div>
  </dl>
</template>
```

Svelte · `shortcut-list.svelte`

```svelte
<script lang="ts">
  import { Kbd } from "$lib/components/ui/kbd";

  /* a shortcuts list: the action at the start, its keys at the end; a sequence reads "then" */
  const shortcuts = [
    { action: "Search projects and people", keys: [["⌘", "K"]] },
    { action: "New project", keys: [["C"]] },
    { action: "Go to deploys", keys: [["G"], ["D"]] },
    { action: "Toggle the sidebar", keys: [["⌘", "B"]] },
  ];
</script>

<dl class="flex w-80 flex-col gap-2 text-sm">
  {#each shortcuts as s (s.action)}
    <div class="flex items-center justify-between gap-4">
      <dt class="text-ink-2">{s.action}</dt>
      <dd class="flex items-center gap-1 text-xs text-ink-2">
        {#each s.keys as k, i (i)}<span class="contents">{#if i > 0}then{/if}<Kbd keys={k} /></span>{/each}
      </dd>
    </div>
  {/each}
</dl>
```

### In a sentence

React · `in-a-sentence.tsx`

```tsx
import { Kbd } from "@/components/ui/kbd";

/* boxed keys sit on the text's line, at the text's size */
export default function InASentence() {
  return (
    <p className="max-w-80 text-sm text-ink-2">
      Press <Kbd keys={["⌘", "K"]} /> to search projects, deploys and people. <Kbd keys={["Esc"]} /> closes the search.
    </p>
  );
}
```

Vue · `in-a-sentence.vue`

```vue
<script setup lang="ts">
import { Kbd } from "@/components/ui/kbd";
</script>

<!-- boxed keys sit on the text's line, at the text's size -->
<template>
  <p class="max-w-80 text-sm text-ink-2">
    Press <Kbd :keys="['⌘', 'K']" /> to search projects, deploys and people. <Kbd :keys="['Esc']" /> closes the search.
  </p>
</template>
```

Svelte · `in-a-sentence.svelte`

```svelte
<script lang="ts">
  import { Kbd } from "$lib/components/ui/kbd";
</script>

<!-- boxed keys sit on the text's line, at the text's size -->
<p class="max-w-80 text-sm text-ink-2">
  Press <Kbd keys={["⌘", "K"]} /> to search projects, deploys and people. <Kbd keys={["Esc"]} /> closes the search.
</p>
```

### Hint under a comment box (plain)

React · `composer-hint.tsx`

```tsx
import { Kbd } from "@/components/ui/kbd";

/* plain: no box, for a quiet hint line under a field */
export default function ComposerHint() {
  return (
    <p className="flex items-center gap-1.5 text-xs text-ink-2">
      <Kbd variant="plain" keys={["↵"]} /> to comment
      <span aria-hidden="true">·</span>
      <Kbd variant="plain" keys={["⇧", "↵"]} /> for a new line
    </p>
  );
}
```

Vue · `composer-hint.vue`

```vue
<script setup lang="ts">
import { Kbd } from "@/components/ui/kbd";
</script>

<!-- plain: no box, for a quiet hint line under a field -->
<template>
  <p class="flex items-center gap-1.5 text-xs text-ink-2">
    <Kbd variant="plain" :keys="['↵']" /> to comment
    <span aria-hidden="true">·</span>
    <Kbd variant="plain" :keys="['⇧', '↵']" /> for a new line
  </p>
</template>
```

Svelte · `composer-hint.svelte`

```svelte
<script lang="ts">
  import { Kbd } from "$lib/components/ui/kbd";
</script>

<!-- plain: no box, for a quiet hint line under a field -->
<p class="flex items-center gap-1.5 text-xs text-ink-2">
  <Kbd variant="plain" keys={["↵"]} /> to comment
  <span aria-hidden="true">·</span>
  <Kbd variant="plain" keys={["⇧", "↵"]} /> for a new line
</p>
```

## Recipe

`kbd.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* kbd — keyboard hint, one or more keys. boxed: each key a small raised key (docs, palettes);
 * plain: text in the surrounding font and colour (tooltips, menu rows).
 *
 * Both parts are <kbd>, which the browser sets in monospace: the family is inherited back, so the
 * hint takes the surrounding font (a boxed key then sets its own mono family).
 * No motion. The root slot is `base`, each key is `key`. */

export const kbd = tv({
  slots: {
    base: "inline-flex shrink-0 items-center gap-0.5 [font-family:inherit] leading-none whitespace-nowrap align-middle",
    key: "",
  },
  variants: {
    variant: {
      boxed: {
        key: "inline-flex items-center justify-center min-w-[18px] h-[18px] px-[5px] rounded-chip bg-surface shadow-btn font-mono text-2xs text-ink-2",
      },
      plain: {
        base: "gap-px text-2xs font-normal text-inherit",
        key: "[font-family:inherit]",
      },
    },
  },
  defaultVariants: { variant: "boxed" },
});

export type KbdRecipeProps = VariantProps<typeof kbd>;
```
