---
title: Kbd for Astro
description: "Astro keyboard key labels for shortcuts and instructions."
seo:
    title: Kbd for Astro | Shadcn & Tailwind CSS
related:
    - /components/command
    - /components/tooltip
sidebar:
    label: "Kbd"
---

```astro
---
import { Kbd, KbdGroup } from "@/components/ui/kbd"
---

<div class="flex flex-wrap items-center gap-2">
  <Kbd>⌘</Kbd>
  <Kbd>⇧</Kbd>
  <Kbd>⌥</Kbd>
  <Kbd>⌃</Kbd>
  <KbdGroup>
    <Kbd>Ctrl</Kbd>
    <Kbd>B</Kbd>
  </KbdGroup>
</div>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/kbd
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/kbd/index.ts

```ts
export { default as Kbd } from "./kbd.astro"
export { default as KbdGroup } from "./kbd-group.astro"
```

#### src/components/ui/kbd/kbd-group.astro

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

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div data-slot="kbd-group" class={cn("inline-flex items-center gap-1", className)} {...props}>
  <slot />
</div>
```

#### src/components/ui/kbd/kbd.astro

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

import { cn } from "cn"

type Props = HTMLAttributes<"kbd">

const { class: className, ...props } = Astro.props
---

<kbd data-slot="kbd" class={cn("pointer-events-none inline-flex h-5 min-w-5 select-none items-center justify-center gap-1 rounded-[.25rem] bg-muted px-1 font-medium font-sans text-muted-foreground text-xs [&_svg:not([class*='size-'])]:size-3", className)} {...props}>
  <slot />
</kbd>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

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

```astro
<Kbd>Ctrl</Kbd>
```

## Composition

Use the following composition to build `Kbd` and `KbdGroup`:

```text
Kbd
KbdGroup
├── Kbd
└── Kbd
```

## Examples

### Group

Use the `KbdGroup` component to group keyboard keys together.

```astro
---
import { Kbd, KbdGroup } from "@/components/ui/kbd"
---

<div class="flex flex-wrap items-center gap-1 text-sm">
  <span>Use</span>
  <KbdGroup>
    <Kbd>Ctrl</Kbd>
    <Kbd>B</Kbd>
  </KbdGroup>
  <span>or</span>
  <KbdGroup>
    <Kbd>Ctrl</Kbd>
    <Kbd>K</Kbd>
  </KbdGroup>
  <span>to search the current project</span>
</div>
```

### Button

Use the `Kbd` component inside a `Button` component to display a keyboard key inside a button.

```astro
---
import { Button } from "@/components/ui/button"
import { Kbd } from "@/components/ui/kbd"
---

<Button variant="outline">
  Accept
  <Kbd>⏎</Kbd>
</Button>
```

### Tooltip

You can use the `Kbd` component inside a `Tooltip` component to display a tooltip with a keyboard key.

```astro
---
import { buttonVariants } from "@/components/ui/button"
import { Kbd, KbdGroup } from "@/components/ui/kbd"
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/components/ui/tooltip"
---

<div class="flex flex-wrap items-center gap-3">
  <Tooltip>
    <TooltipTrigger class={buttonVariants({ variant: "outline" })}>
      Save
    </TooltipTrigger>
    <TooltipContent>
      Save
      <KbdGroup>
        <Kbd>⌘</Kbd>
        <Kbd>S</Kbd>
      </KbdGroup>
    </TooltipContent>
  </Tooltip>

  <Tooltip>
    <TooltipTrigger class={buttonVariants({ variant: "outline" })}>
      Print
    </TooltipTrigger>
    <TooltipContent>
      Print
      <KbdGroup>
        <Kbd>⌘</Kbd>
        <Kbd>P</Kbd>
      </KbdGroup>
    </TooltipContent>
  </Tooltip>
</div>
```

### Input Group

You can use the `Kbd` component inside an `InputGroupAddon` component to display a keyboard key inside an input group.

```astro
---
import SearchIcon from "@lucide/astro/icons/search"

import {
  InputGroup,
  InputGroupAddon,
  InputGroupInput,
} from "@/components/ui/input-group"
import { Kbd } from "@/components/ui/kbd"
---

<div class="flex w-full max-w-xs flex-col gap-6">
  <InputGroup>
    <InputGroupInput placeholder="Search..." />
    <InputGroupAddon>
      <SearchIcon />
    </InputGroupAddon>
    <InputGroupAddon align="inline-end">
      <Kbd>⌘</Kbd>
      <Kbd>K</Kbd>
    </InputGroupAddon>
  </InputGroup>
</div>
```

## RTL

Set `dir="rtl"` on the document or the relevant region. Check text order, focus, and directional icons in the rendered Astro page. No React provider is required.

```astro
---
import { Kbd, KbdGroup } from "@/components/ui/kbd"
---

<div dir="rtl" class="flex flex-wrap items-center gap-2">
  <Kbd>⌘</Kbd>
  <Kbd>⇧</Kbd>
  <Kbd>⌥</Kbd>
  <Kbd>⌃</Kbd>
  <KbdGroup>
    <Kbd>Ctrl</Kbd>
    <Kbd>B</Kbd>
  </KbdGroup>
</div>
```

## API Reference

### Kbd

Use the `Kbd` component to display a keyboard key.

| Prop    | Type     | Default |
| ------- | -------- | ------- |
| `class` | `string` |         |

```astro
<Kbd>Ctrl</Kbd>
```

### KbdGroup

Use the `KbdGroup` component to group `Kbd` components together.

| Prop    | Type     | Default |
| ------- | -------- | ------- |
| `class` | `string` |         |

```astro
<KbdGroup>
  <Kbd>Ctrl</Kbd>
  <Kbd>B</Kbd>
</KbdGroup>
```

Read the [installed source](/r/kbd.json) for the complete prop types.

## When to use Kbd

Use Kbd to display one key and KbdGroup to arrange a key combination. Write the key names that apply to the platform your instructions describe.

## Keyboard and behavior

The component uses keyboard-text markup. It does not register shortcuts or detect the operating system. Explain the action in nearby text, and implement any actual listener in the application.

## Theme and layout

Muted borders and compact text keep key labels distinct from buttons. Check that a long key name remains readable at the small size. Do not make keyboard notation the only way to discover an action.
