Kbd for Astro
Astro keyboard key labels for shortcuts and instructions.
Installation
bunx --bun shadcn@latest add @gnostic/kbdnpx shadcn@latest add @gnostic/kbdpnpm dlx shadcn@latest add @gnostic/kbdComponent classes: Default Gnostic.
Complete the Installation guide first. Then copy the component files below.
No extra packages are required.
Copy the files
src/components/ui/kbd/index.ts
export { default as Kbd } from "./kbd.astro"
export { default as KbdGroup } from "./kbd-group.astro"
src/components/ui/kbd/kbd-group.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
---
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>
Usage
import { Kbd } from "@/components/ui/kbd"
<Kbd>Ctrl</Kbd>
Composition
Use the following composition to build Kbd and KbdGroup:
Kbd
KbdGroup
├── Kbd
└── Kbd
Examples
Group
Use the KbdGroup component to group keyboard keys together.
Button
Use the Kbd component inside a Button component to display a keyboard key inside a button.
Tooltip
You can use the Kbd component inside a Tooltip component to display a tooltip with a keyboard key.
Input Group
You can use the Kbd component inside an InputGroupAddon component to display a keyboard key inside an input group.
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.
API Reference
Kbd
Use the Kbd component to display a keyboard key.
| Prop | Type | Default |
|---|---|---|
class |
string |
<Kbd>Ctrl</Kbd>
KbdGroup
Use the KbdGroup component to group Kbd components together.
| Prop | Type | Default |
|---|---|---|
class |
string |
<KbdGroup>
<Kbd>Ctrl</Kbd>
<Kbd>B</Kbd>
</KbdGroup>
Read the installed source 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.