Kbd
Monospace key cap built on the kbd element with a thick bottom edge; pass several keys to show a combination like Ctrl + K.
pnpm dlx shadcn@latest add @haze/kbd---
import { cn } from "../../../lib/haze/utils";
interface Props {
/** Key labels. One key renders one cap; several render one cap each inside an outer kbd. */
keys?: string[];
/** Cap size: sm for tight rows, md for body text. */
size?: "sm" | "md";
class?: string;
}
const { keys = [], size = "md", class: className } = Astro.props;
const hasSlot = Astro.slots.has("default");
const sizes = {
sm: "h-5 min-w-5 px-1.5 text-[0.6875rem]/none",
md: "h-6 min-w-6 px-2 text-xs/none",
};
const cap = cn(
"inline-flex items-center justify-center rounded-md border border-frame-foreground/14 border-b-2 border-b-frame-foreground/24 bg-muted font-mono font-medium tracking-tight text-foreground select-none whitespace-nowrap",
sizes[size],
);
---
{
keys.length > 1 ? (
<kbd class={cn("inline-flex items-center gap-1 align-middle", className)}>
{keys.map((key) => (
<kbd class={cap}>{key}</kbd>
))}
</kbd>
) : keys.length === 1 ? (
<kbd class={cn(cap, "align-middle", className)}>{keys[0]}</kbd>
) : hasSlot ? (
<kbd class={cn(cap, "align-middle", className)}><slot /></kbd>
) : null
}When to use
- Showing a shortcut hint beside a menu item or search field, such as Ctrl K
- Documenting a key press inside help text or onboarding copy
- Listing a table of shortcuts where each row has a key combination
When not to use
- The text is a code snippet or command; use a code element instead
- The key cap should be clickable; it is display only, use a button
Installation
Add the Haze registry to components.json once, then install Kbd with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/kbdnpx shadcn@latest add @haze/kbdyarn dlx shadcn@latest add @haze/kbdbunx --bun shadcn@latest add @haze/kbdFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| keys | string[] | [] | Key labels. One key renders a single cap; several render one cap each inside an outer kbd. |
| size | "sm" | "md" | "md" | Cap size: sm (20 px) for tight rows and inputs, md (24 px) for body text and shortcut lists. |
| class | string | – | Extra classes on the outermost kbd. |
| default slot | slot | – | Content for a single key when keys is empty, for example an icon. |
Accessibility
- Uses the semantic kbd element, so assistive tech announces the text as keyboard input.
- A combination is one outer kbd wrapping one kbd per key, matching the HTML spec for key sequences.
- Spell out symbol-only keys in the label (use "Esc" or "Shift", not an arrow glyph alone) so screen readers read them.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Kbd" with `npx shadcn@latest add @haze/kbd`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Kbd from "@/components/haze/ui/kbd.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/kbd.md
Customize with real content
Adapt the Haze kbd ui for our product. Replace the demo values of these props with our real content: keys, size, class, default slot. Keep the layout, the heading level rules and these accessibility details: Uses the semantic kbd element, so assistive tech announces the text as keyboard input. A combination is one outer kbd wrapping one kbd per key, matching the HTML spec for key sequences. Spell out symbol-only keys in the label (use "Esc" or "Shift", not an arrow glyph alone) so screen readers read them. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using kbd, check it fits: use it when showing a shortcut hint beside a menu item or search field, such as ctrl k; documenting a key press inside help text or onboarding copy; listing a table of shortcuts where each row has a key combination. Avoid it when the text is a code snippet or command; use a code element instead; the key cap should be clickable; it is display only, use a button. Alternatives: @haze/badge.
Dependencies
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.