Kbd
Keyboard key example: an inline key label in a small muted rounded box, plus a group wrapper for key combinations. Adapts its colors inside an input group or a tooltip.
import { cn } from "cn"
function Kbd({ className, ...props }: React.ComponentProps<"kbd">) {
return (
<kbd
data-slot="kbd"
className={cn(
"pointer-events-none inline-flex h-5.5 w-fit min-w-5.5 items-center justify-center gap-1 rounded-lg bg-muted px-1.5 font-sans text-xs font-medium text-muted-foreground select-none in-data-[slot=input-group]:bg-input in-data-[slot=tooltip-content]:bg-background/20 in-data-[slot=tooltip-content]:text-background dark:in-data-[slot=tooltip-content]:bg-background/10 [&_svg:not([class*='size-'])]:size-3",
className
)}
{...props}
/>
)
}
function KbdGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<kbd
data-slot="kbd-group"
className={cn("inline-flex items-center gap-1", className)}
{...props}
/>
)
}
export { Kbd, KbdGroup }
import { Kbd, KbdGroup } from "@/components/ui/kbd"
// The copy: change text here, not in the markup.
const COPY = {
modifier: "⌘",
key: "K",
} as const
/** Kbd group showing a two-key shortcut. */
export default function KbdDemo() {
return (
<KbdGroup>
<Kbd>{COPY.modifier}</Kbd>
<Kbd>{COPY.key}</Kbd>
</KbdGroup>
)
}
Installation
pnpm dlx shadcn@latest add @01orbital/kbdnpx shadcn@latest add @01orbital/kbdyarn dlx shadcn@latest add @01orbital/kbdbunx --bun shadcn@latest add @01orbital/kbdExamples
Editing Shortcutskbd-editing-shortcuts
Keyboard key example: Kbd keys beside a transcript textarea, explaining native editing shortcuts. Shows shortcut hints next to a text field.
import { Eyebrow } from "@/components/eyebrow/eyebrow"
import {
Card,
CardContent,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import { InputGroup, InputGroupTextarea } from "@/components/ui/input-group"
import { Kbd, KbdGroup } from "@/components/ui/kbd"
import { Label } from "@/components/ui/label"
// The copy: change text here, not in the markup.
const COPY = {
eyebrow: "Thekla Radio · Episode 28",
title: "Transcript corrections",
label: "03:42 · Speaker: Sally Ride",
transcript:
"We recorded the dawn chorus at 5:10, just before the first ferry crossed the bay.",
modifier: "⌘",
footnote: "Shortcuts for macOS. Use Ctrl on Windows or Linux.",
} as const
const SHORTCUTS = [
{ key: "A", label: "Select all" },
{ key: "Z", label: "Undo edit" },
] as const
/** Kbd keys beside a transcript textarea, explaining native editing shortcuts. */
export default function KbdEditingShortcuts() {
return (
<div className="flex w-full items-center justify-center rounded-xl border bg-background px-3 py-8 text-foreground [--radius:0.625rem] dark:[--radius:0.625rem]">
<Card className="w-full max-w-md rounded-xl border shadow-none ring-0 sm:mx-3">
<CardHeader className="gap-2">
<Eyebrow variant="mono">{COPY.eyebrow}</Eyebrow>
<CardTitle className="text-2xl font-medium tracking-tight">
{COPY.title}
</CardTitle>
</CardHeader>
<CardContent className="flex flex-col gap-2">
<Label
htmlFor="thekla-transcript"
className="block text-xs leading-4 font-normal text-muted-foreground"
>
{COPY.label}
</Label>
<InputGroup className="rounded-lg">
<InputGroupTextarea
id="thekla-transcript"
aria-describedby="thekla-shortcuts"
defaultValue={COPY.transcript}
className="min-h-28"
/>
</InputGroup>
<div
id="thekla-shortcuts"
className="mt-3 flex flex-wrap gap-x-6 gap-y-3 text-xs"
>
{SHORTCUTS.map((shortcut) => (
<span key={shortcut.key} className="flex items-center gap-2">
<KbdGroup>
<Kbd>{COPY.modifier}</Kbd>
<Kbd>{shortcut.key}</Kbd>
</KbdGroup>
{shortcut.label}
</span>
))}
</div>
</CardContent>
<CardFooter>
<p className="text-xs text-muted-foreground">{COPY.footnote}</p>
</CardFooter>
</Card>
</div>
)
}
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
const eyebrowVariants = cva("text-eyebrow", {
variants: {
/**
* `default` is a 14px/20px label. `icon` adds a row with a 16px icon
* and a 12px gap; put the icon first and give it `aria-hidden`.
* `mono` and `caps` are 12px/20px uppercase labels with 0.12em
* tracking, in the mono and inherited faces respectively.
*/
variant: {
default: "text-sm/5 font-medium text-eyebrow",
icon: "flex items-center gap-3 text-sm/5 font-medium [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
mono: "font-mono text-xs/5 font-medium tracking-[0.12em] uppercase",
caps: "text-xs/5 font-medium tracking-[0.12em] uppercase",
},
},
defaultVariants: { variant: "default" },
})
/**
* A short label above a heading, rendered as a `<p>` at weight 500.
* The default is 14px/20px in the kit's eyebrow color. The surrounding
* layout controls the space to the heading. Use `className` to customize
* the typography or color, and `render` to change the element.
*/
function Eyebrow({
className,
variant = "default",
render,
...props
}: useRender.ComponentProps<"p"> & VariantProps<typeof eyebrowVariants>) {
return useRender({
defaultTagName: "p",
render,
state: { slot: "eyebrow", variant },
props: mergeProps<"p">(
{ className: cn(eyebrowVariants({ variant }), className) },
props
),
})
}
export { Eyebrow, eyebrowVariants }
pnpm dlx shadcn@latest add @01orbital/kbd-editing-shortcutsnpx shadcn@latest add @01orbital/kbd-editing-shortcutsyarn dlx shadcn@latest add @01orbital/kbd-editing-shortcutsbunx --bun shadcn@latest add @01orbital/kbd-editing-shortcutsDependencies
npm: cn. The CLI installs it for you.
Used in blocks
3 blocks are built with Kbd:
Last updated on