# Kbd

Keycap for keyboard shortcuts using the native kbd element, in two sizes, with key combinations rendered as separate keys.

- Category: Data
- Status: stable
- Registry: https://markas.jenderalcorp.com/r/kbd.json
- CLI: `npx markasui@latest add kbd`

## When to use

Showing a keyboard shortcut in help text, menus, tooltips, buttons, or a shortcut reference list.

- Use keys for combinations instead of writing "+" by hand
- Write key names the way they appear on the keyboard, such as Ctrl, Shift, Enter, and Esc
- Do not make a keycap clickable; use Button for actions

## Code

### Blade

Save at `resources/views/components/markas/kbd.blade.php`. No extra dependencies.

```blade
@props([
    'size' => 'md',
    'keys' => [],
    'separator' => '+',
])

@php
    $keycap = 'inline-flex items-center justify-center rounded-md bg-surface font-sans font-medium text-muted ring-1 ring-line';
    $sizes = [
        'sm' => 'h-5 min-w-5 px-1 text-[11px]',
        'md' => 'h-6 min-w-6 px-1.5 text-xs',
    ];
    $group = 'inline-flex items-center gap-1 font-sans';
    $joiner = 'text-xs text-muted';
@endphp

@if (count($keys) > 0)
    <kbd {{ $attributes->class([$group]) }}>@foreach ($keys as $key)@if (! $loop->first)<span class="{{ $joiner }}">{{ $separator }}</span>@endif<kbd class="{{ $keycap }} {{ $sizes[$size] }}">{{ $key }}</kbd>@endforeach</kbd>
@else
    <kbd {{ $attributes->class([$keycap, $sizes[$size]]) }}>{{ $slot }}</kbd>
@endif
```

```blade
<x-markas.kbd>Esc</x-markas.kbd>
<x-markas.kbd :keys="['Ctrl', 'K']" />
<x-markas.kbd size="sm" :keys="['G', 'N']" separator="then" />
```

### React

Save at `src/components/markas/Kbd.tsx`. No extra dependencies.

```tsx
import { Fragment, type ComponentProps } from "react";

const keycap =
  "inline-flex items-center justify-center rounded-md bg-surface font-sans font-medium text-muted ring-1 ring-line";
const sizes = {
  sm: "h-5 min-w-5 px-1 text-[11px]",
  md: "h-6 min-w-6 px-1.5 text-xs",
};
const group = "inline-flex items-center gap-1 font-sans";
const joiner = "text-xs text-muted";
const cx = (...c: (string | false | null | undefined)[]) =>
  c.filter(Boolean).join(" ");

export type KbdProps = ComponentProps<"kbd"> & {
  size?: keyof typeof sizes;
  /** Kombinasi tombol, dirender sebagai keycap terpisah. Menggantikan children. */
  keys?: string[];
  /** Teks di antara tombol pada kombinasi. */
  separator?: string;
};

export function Kbd({
  size = "md",
  keys = [],
  separator = "+",
  className,
  children,
  ...props
}: KbdProps) {
  if (keys.length === 0)
    return (
      <kbd className={cx(keycap, sizes[size], className)} {...props}>
        {children}
      </kbd>
    );

  return (
    <kbd className={cx(group, className)} {...props}>
      {keys.map((key, index) => (
        <Fragment key={index}>
          {index > 0 && <span className={joiner}>{separator}</span>}
          <kbd className={cx(keycap, sizes[size])}>{key}</kbd>
        </Fragment>
      ))}
    </kbd>
  );
}
```

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

<Kbd>Esc</Kbd>
<Kbd keys={["Ctrl", "K"]} />
<Kbd size="sm" keys={["G", "N"]} separator="then" />
```

### Vue

Save at `src/components/markas/Kbd.vue`. No extra dependencies.

```vue
<script setup lang="ts">
const props = withDefaults(
  defineProps<{
    size?: "sm" | "md";
    keys?: string[];
    separator?: string;
  }>(),
  { size: "md", keys: () => [], separator: "+" },
);

const keycap =
  "inline-flex items-center justify-center rounded-md bg-surface font-sans font-medium text-muted ring-1 ring-line";
const sizes = {
  sm: "h-5 min-w-5 px-1 text-[11px]",
  md: "h-6 min-w-6 px-1.5 text-xs",
};
const group = "inline-flex items-center gap-1 font-sans";
const joiner = "text-xs text-muted";
</script>

<template>
  <kbd v-if="props.keys.length > 0" :class="group"
    ><template v-for="(key, index) in props.keys" :key="index"
      ><span v-if="index > 0" :class="joiner">{{ props.separator }}</span
      ><kbd :class="[keycap, sizes[props.size]]">{{ key }}</kbd></template
    ></kbd
  >
  <kbd v-else :class="[keycap, sizes[props.size]]"><slot /></kbd>
</template>
```

```vue
<template>
  <Kbd>Esc</Kbd>
  <Kbd :keys="['Ctrl', 'K']" />
  <Kbd size="sm" :keys="['G', 'N']" separator="then" />
</template>
```

### Svelte

Save at `src/lib/components/markas/Kbd.svelte`. No extra dependencies.

```svelte
<script lang="ts">
  import type { Snippet } from "svelte";
  import type { HTMLAttributes } from "svelte/elements";

  const keycap =
    "inline-flex items-center justify-center rounded-md bg-surface font-sans font-medium text-muted ring-1 ring-line";
  const sizes = {
    sm: "h-5 min-w-5 px-1 text-[11px]",
    md: "h-6 min-w-6 px-1.5 text-xs",
  };
  const group = "inline-flex items-center gap-1 font-sans";
  const joiner = "text-xs text-muted";

  type Props = HTMLAttributes<HTMLElement> & {
    size?: keyof typeof sizes;
    /** Key combination, rendered as separate keycaps. Replaces children. */
    keys?: string[];
    /** Text between keys in a combination. */
    separator?: string;
    children?: Snippet;
  };

  let {
    size = "md",
    keys = [],
    separator = "+",
    class: className,
    children,
    ...rest
  }: Props = $props();
</script>

{#if keys.length === 0}
  <kbd class={[keycap, sizes[size], className]} {...rest}
    >{@render children?.()}</kbd
  >
{:else}
  <kbd class={[group, className]} {...rest}
    >{#each keys as key, index (index)}{#if index > 0}<span class={joiner}
          >{separator}</span
        >{/if}<kbd class={[keycap, sizes[size]]}>{key}</kbd>{/each}</kbd
  >
{/if}
```

```svelte
<script lang="ts">
  import Kbd from "$lib/components/markas/Kbd.svelte";
</script>

<Kbd>Esc</Kbd>
<Kbd keys={["Ctrl", "K"]} />
<Kbd size="sm" keys={["G", "N"]} separator="then" />
```

### Astro

Save at `src/components/markas/Kbd.astro`. No extra dependencies.

```astro
---
import type { HTMLAttributes } from "astro/types";

const keycap =
  "inline-flex items-center justify-center rounded-md bg-surface font-sans font-medium text-muted ring-1 ring-line";
const sizes = {
  sm: "h-5 min-w-5 px-1 text-[11px]",
  md: "h-6 min-w-6 px-1.5 text-xs",
};
const group = "inline-flex items-center gap-1 font-sans";
const joiner = "text-xs text-muted";

type Props = HTMLAttributes<"kbd"> & {
  size?: keyof typeof sizes;
  /** Key combination, rendered as separate keycaps. Replaces the slot. */
  keys?: string[];
  /** Text between keys in a combination. */
  separator?: string;
};

const { size = "md", keys = [], separator = "+", class: className, ...rest } =
  Astro.props;
---

{
  keys.length === 0 ? (
    <kbd class:list={[keycap, sizes[size], className]} {...rest}><slot /></kbd>
  ) : (
    <kbd class:list={[group, className]} {...rest}>
      {keys.map((key, index) => (
        <Fragment>
          {index > 0 && <span class={joiner}>{separator}</span>}
          <kbd class:list={[keycap, sizes[size]]}>{key}</kbd>
        </Fragment>
      ))}
    </kbd>
  )
}
```

```astro
---
import Kbd from "@/components/markas/Kbd.astro";
---

<Kbd>Esc</Kbd>
<Kbd keys={["Ctrl", "K"]} />
<Kbd size="sm" keys={["G", "N"]} separator="then" />
```

### Angular

Save at `src/app/markas/kbd.component.ts`. No extra dependencies.

```ts
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  input,
} from "@angular/core";

const keycap =
  "inline-flex items-center justify-center rounded-md bg-surface font-sans font-medium text-muted ring-1 ring-line";
const sizes = {
  sm: "h-5 min-w-5 px-1 text-[11px]",
  md: "h-6 min-w-6 px-1.5 text-xs",
};
const group = "inline-flex items-center gap-1 font-sans";
const joiner = "text-xs text-muted";

export type KbdSize = keyof typeof sizes;

/**
 * Attribute selector on the native kbd: aria-*, title, and the user's class stay on it.
 * With `keys` the host becomes the group and every key is a nested kbd.
 */
@Component({
  selector: "kbd[markas-kbd]",
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { "[class]": "classes()" },
  template: `@if (keys().length === 0) {
      <ng-content />
    } @else {
      @for (key of keys(); track $index) {
        @if ($index > 0) {
          <span [class]="joiner">{{ separator() }}</span>
        }
        <kbd [class]="keyClass()">{{ key }}</kbd>
      }
    }`,
})
export class Kbd {
  readonly size = input<KbdSize>("md");
  /** Key combination, rendered as separate keycaps. Replaces the content. */
  readonly keys = input<string[]>([]);
  /** Text between keys in a combination. */
  readonly separator = input("+");

  protected readonly joiner = joiner;
  protected readonly keyClass = computed(
    () => `${keycap} ${sizes[this.size()]}`,
  );
  protected readonly classes = computed(() =>
    this.keys().length === 0 ? this.keyClass() : group,
  );
}
```

```ts
import { Component } from "@angular/core";
import { Kbd } from "./markas/kbd.component";

@Component({
  selector: "app-example",
  imports: [Kbd],
  template: `
    <kbd markas-kbd>Esc</kbd>
    <kbd markas-kbd [keys]="['Ctrl', 'K']"></kbd>
    <kbd markas-kbd size="sm" [keys]="['G', 'N']" separator="then"></kbd>
  `,
})
export class Example {}
```

Angular uses an attribute selector on the native `kbd`, so `class` and `aria-hidden` stay on it.

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `'sm' \| 'md'` | md | Keycap height and text size. Use sm inside buttons and inputs. |
| `keys` | `string[]` | [] | Key combination. Each key renders as its own keycap inside an outer kbd. Replaces the slot or children. |
| `separator` | `string` | + | Muted text between the keys of a combination, such as "then" for a key sequence. |
| `children / slot` | `ReactNode / slot / snippet` | - | Content of a single keycap when keys is empty. |

## Accessibility

- Uses the native kbd element; a combination nests one kbd per key inside an outer kbd, as the HTML spec recommends.
- The separator is plain text, so screen readers announce combinations such as "Ctrl + K".
- Set aria-hidden on the keycap when the shortcut is only a visual hint inside a control that already has a name.

## Notes

- No JavaScript needed. The Blade version does not need Alpine.js.
- `keys` renders `<kbd><kbd>Ctrl</kbd><span>+</span><kbd>K</kbd></kbd>`. Without `keys`, the slot or children fill a single keycap.
- Inside a button that already has a name, add `aria-hidden="true"` so the shortcut is not read twice.
- The default separator is "+". Change it with `separator`, for example "then" for key sequences.
