Kbd React component for Tailwind CSS v4
v1.0.0Added in 1.0.0
A React kbd component for keyboard shortcuts, shown in the device's notation: ⌘K on a Mac, Ctrl+K everywhere else.
Pass a combo like mod+k and it prints ⌘ K on Apple devices and Ctrl K elsewhere; screen readers hear "Command K" instead of the glyphs. In the playground, press ⌘K (Ctrl+K) to jump into the search field, use the arrow keys and Enter to move through the results, and press Esc to close the window and again to bring it back. Turn on listen and the caps sink and light up while you hold the real keys. On a phone the keyboard hints hide themselves and a tap opens a result.
Kbd is a free React component in the React data display components category, written in TypeScript for React 19 and Tailwind CSS v4. It installs as source with npx wingo-ui@latest add kbd, together with UIProvider, useShortcut, useMediaQuery and Tones.
Add to a project
Requirements: React 19 and Tailwind CSS v4, in Next.js (App Router or Pages), Vite or React Router. Installation guide · Next.js setup · Vite setup
npx wingo-ui@latest add kbdUsage
Import Kbd from @/components/ui/kbd.
import { Search } from "lucide-react"
import { Kbd } from "@/components/ui/kbd"
export function SearchHint() {
return (
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<Search className="size-4" />
<span className="flex-1">Search listings</span>
<Kbd shortcut="mod+k" hideOnTouch />
</div>
)
}
export function CloseHint() {
return (
<p className="text-sm text-muted-foreground">
Press <Kbd>Esc</Kbd> to close the window, or <Kbd keys={["G", "I"]} separator="then" /> for the Inbox.
</p>
)
}Props
childrendefault"Esc"ReactNodeA single key label, used when there is no shortcut and no keys. Glyphs like ⌘ or ↵ get a spoken name for screen readers.
shortcutdefault""stringA combo in mod+k notation: "mod+k", "shift+mod+p", "alt+n", "mod+enter", "/". mod is ⌘ on Apple devices and Ctrl elsewhere. Wins over keys and children; an unparseable value falls back to them.
keysstring[]One cap per entry, shown as written. For sequences (["G", "I"] with separator "then") and hints that are not one combo (["↑", "↓"]). Used when there is no shortcut.
separatordefault""ReactNodeRendered between caps when they are not combined, like "+" or "then". A word separator is also read out ("G then I"); a symbol is not.
srLabeldefault""stringWhat screen readers hear instead of the caps. By default the combo is spelled out in English ("Shift Command P", "Control K", "G then I"); pass a translation on localized pages.
variantdefault"soft""soft" | "outline" | "solid" | "ghost"soft is the quiet gray key for hints next to fields and buttons. outline draws a border, solid is a raised key like a real keyboard, ghost is text only for menus and inverted surfaces like tooltips.
sizedefault"md""sm" | "md" | "lg"Cap height 16 / 20 / 24px with a matching minimum width, 10 / 11 / 12px type and a radius that scales along.
tonedefault"neutral""neutral" | "brand" | "success" | "warning" | "danger" | "info"Semantic color. neutral is the gray key from the design system; the other tones tint the key, and a held key (listen) fills with the tone.
colordefault""stringAny CSS color. Replaces the tone in every variant; text on a solid key turns black or white by lightness.
radiusdefault"md""none" | "sm" | "md" | "lg" | "full"Corner shape, scaled per size. md is 4 / 5 / 6px; full makes round caps.
combineddefaultfalsebooleanThe whole combo in one cap (⌘K, Ctrl+K) instead of one cap per key. Reads well in tight rows and menus.
platformdefault"auto""auto" | "apple" | "other"Forces a notation, for docs pages that show both. auto renders the Windows and Linux form on the server and during hydration, then swaps to the device's.
hideOnTouchdefaultfalsebooleanHides the keys on touch screens, where a keyboard hint means nothing.
listendefaultfalsebooleanCaps sink and light up while the matching keys are held on a physical keyboard, and the root gets data-pressed once the whole combo is down. It only watches; it never binds or blocks a key.
classNamesPartial<Record<"key" | "separator", string>>Extra classes for every cap (also the single cap) and for the separators.
classNamestringExtra classes for the root: the <kbd> for one cap, the group span for several. Merged last.
Examples
Both notations
platform forces one: Apple glyphs next to the Windows and Linux words. auto picks the device's.
Separate caps or combined
combined puts the whole combo in one cap, handy in tight rows.
Variants and sizes
soft for hints, outline and solid for docs, ghost as plain text. 16, 20 and 24px caps.
Tones, colors and radius
The tones tint the key, color takes any CSS color, radius scales with the size.
Shortcut sheet that listens
listen: hold a combo and its caps sink, the row lights up once it is all down. A click or a tap runs it too.
Inverted and in menus
variant="ghost" plus a color class, the way tooltips and menu rows show shortcuts.
Inside buttons
As a right icon, aria-hidden with aria-keyshortcuts on the button. hideOnTouch drops the hint on phones.
Sequences
keys with a word separator ("then") for chords like G then I. The word is read out too.
Dependencies
Files
- components/ui/kbd.tsx
FAQ
Is the React Kbd component free?
Yes. The source is free and needs no account: copy it from the Code tab or install it with the CLI.
How do I install Kbd?
Run npx wingo-ui@latest add kbd. It also adds the Wingo UI items it builds on (UIProvider, useShortcut, useMediaQuery and Tones). With shadcn, run npx shadcn@latest add https://wingo-ui.com/r/kbd.json.
How does Kbd behave on phones?
On a phone the keyboard hints hide themselves and a tap opens a result.
See also
Used in
More React data display components
Articles using this component
- React Command Palette: Cmd+K, Nested Pages and Mobile
Component guides9 min read