When to use
- Showing a keyboard shortcut beside the action it triggers: menu items, tooltips, command palette rows, empty states ("Press / to search").
- Teaching a shortcut, where seeing the cap press as you hold the key confirms you found it.
- Replaying a shortcut in a tutorial or onboarding step with
pressed.
When not to use
Installation
pnpm dlx shadcn add https://eased.dev/r/kbd.jsonnpx shadcn add https://eased.dev/r/kbd.jsonyarn dlx shadcn add https://eased.dev/r/kbd.jsonbunx --bun shadcn add https://eased.dev/r/kbd.jsonThe command adds Kbd, everything it builds on and Eased’s tokens to your project.
Add the Eased pieces it builds on
Copy each item’s files the same way, or install them with the CLI:
Copy the source into your project
components/eased/kbd/kbd.tsxTSX "use client";import { useSyncExternalStore } from "react";/* ---------- Which keys are down: one shared listener for every keycap on the page ---------- */// code → the key it produced when pressed, so releasing ⇧ before "?" still clears "?".const down = new Map<string, string>();const listeners = new Set<() => void>();const emit = () => listeners.forEach((l) => l());const onDown = (e: KeyboardEvent) => { const key = e.key.toLowerCase(); if (down.get(e.code) === key) return; // auto-repeat down.set(e.code, key); emit();};const onUp = (e: KeyboardEvent) => { // macOS drops the keyup of any key released while ⌘ is held, so letting go of ⌘ clears them all. if (e.key === "Meta") down.clear(); else down.delete(e.code); emit();};const reset = () => { if (!down.size) return; down.clear(); emit();};function subscribe(listener: () => void) { if (!listeners.size) { window.addEventListener("keydown", onDown, true); window.addEventListener("keyup", onUp, true); window.addEventListener("blur", reset); document.addEventListener("visibilitychange", reset); } listeners.add(listener); return () => { listeners.delete(listener); if (listeners.size) return; window.removeEventListener("keydown", onDown, true); window.removeEventListener("keyup", onUp, true); window.removeEventListener("blur", reset); document.removeEventListener("visibilitychange", reset); down.clear(); };}/* ---------- What each printed glyph means ---------- */type KeySpec = { match: string[]; name: string };const NAMED: Record<string, KeySpec> = { "⌘": { match: ["key:meta"], name: "Command" }, cmd: { match: ["key:meta"], name: "Command" }, "⌥": { match: ["key:alt"], name: "Option" }, alt: { match: ["key:alt"], name: "Alt" }, opt: { match: ["key:alt"], name: "Option" }, "⇧": { match: ["key:shift"], name: "Shift" }, shift: { match: ["key:shift"], name: "Shift" }, "⌃": { match: ["key:control"], name: "Control" }, ctrl: { match: ["key:control"], name: "Control" }, "↵": { match: ["key:enter"], name: "Enter" }, "⏎": { match: ["key:enter"], name: "Enter" }, enter: { match: ["key:enter"], name: "Enter" }, return: { match: ["key:enter"], name: "Return" }, "⎋": { match: ["key:escape"], name: "Escape" }, esc: { match: ["key:escape"], name: "Escape" }, "⌫": { match: ["key:backspace"], name: "Delete" }, "⌦": { match: ["key:delete"], name: "Forward delete" }, "⇥": { match: ["key:tab"], name: "Tab" }, tab: { match: ["key:tab"], name: "Tab" }, space: { match: ["code:Space"], name: "Space" }, "␣": { match: ["code:Space"], name: "Space" }, "↑": { match: ["key:arrowup"], name: "Up arrow" }, "↓": { match: ["key:arrowdown"], name: "Down arrow" }, "←": { match: ["key:arrowleft"], name: "Left arrow" }, "→": { match: ["key:arrowright"], name: "Right arrow" },};/** Maps a printed glyph ("⌘", "K", "/", "Esc") to the keyboard events that press it and a spoken name. */export function keySpec(glyph: string): KeySpec { const named = NAMED[glyph] ?? NAMED[glyph.toLowerCase()]; if (named) return named; if (/^[a-z]$/i.test(glyph)) return { match: [`code:Key${glyph.toUpperCase()}`], name: glyph.toUpperCase() }; if (/^\d$/.test(glyph)) return { match: [`code:Digit${glyph}`, `code:Numpad${glyph}`], name: glyph }; if (/^f\d{1,2}$/i.test(glyph)) return { match: [`key:${glyph.toLowerCase()}`], name: glyph.toUpperCase() }; return { match: [`key:${glyph.toLowerCase()}`], name: glyph };}const never = () => () => {};const isDown = (spec: KeySpec) => spec.match.some((m) => (m.startsWith("code:") ? down.has(m.slice(5)) : [...down.values()].includes(m.slice(4))));/** * True while every key in `keys` is held, e.g. `useKeysHeld(["⌘", "K"])`, for lighting up the * row a shortcut belongs to. Shares the keycaps' single listener. */export function useKeysHeld(keys: string[], enabled = true) { const id = keys.join("\u0000"); return useSyncExternalStore( enabled ? subscribe : never, () => id.split("\u0000").every((k) => isDown(keySpec(k))), () => false, );}/* ---------- The keycap ---------- */export type KbdProps = { /** One key, e.g. `"K"`, `"⌘"`, `"Esc"`. Use `keys` for a chord. */ children?: string; /** A chord, e.g. `["⌘", "K"]`; each key is its own cap. */ keys?: string[]; /** Press the cap while the real key is held. Default true. */ listen?: boolean; /** Force the pressed look, e.g. to replay a shortcut in a tutorial. Overrides `listen`. */ pressed?: boolean; /** 20px (`sm`, default) or 24px (`md`) caps. */ size?: "sm" | "md"; className?: string;};const SIZES = { sm: "h-5 min-w-5 px-1 text-ui-xs", md: "h-6 min-w-6 px-1.5 text-ui-sm",} as const;/** * A keycap that presses down when the real key does: the face drops 1px onto its base and * lightens, in 100ms, and springs back on release. Glyphs are announced by name ("Command K"). */export function Kbd({ children, keys, listen = true, pressed, size = "sm", className = "" }: KbdProps) { const list = keys ?? (children !== undefined ? [children] : []); if (list.length === 1) return <Cap glyph={list[0]} listen={listen} pressed={pressed} size={size} className={className} />; return ( <kbd className={`inline-flex items-center gap-0.5 font-sans ${className}`}> {list.map((glyph, i) => ( <Cap key={`${glyph}${i}`} glyph={glyph} listen={listen} pressed={pressed} size={size} /> ))} </kbd> );}function Cap({ glyph, listen, pressed, size, className = "" }: { glyph: string; listen: boolean; pressed?: boolean; size: "sm" | "md"; className?: string }) { const spec = keySpec(glyph); const held = useSyncExternalStore( listen && pressed === undefined ? subscribe : never, () => isDown(spec), () => false, ); const on = pressed ?? held; const spoken = spec.name !== glyph; return ( // The base is the cap's lower edge: 1px of it shows below the face at rest, and the face // covers it when pressed, so the press reads as travel without moving the layout. <kbd data-pressed={on || undefined} className={`group/cap inline-flex shrink-0 rounded-control-xs bg-line pb-px align-middle font-sans ${className}`} > <span className={`inline-flex items-center justify-center rounded-control-xs border border-line-subtle bg-surface-raised font-medium text-text-secondary [transition:color_150ms_var(--eased-ease-standard),background-color_150ms_var(--eased-ease-standard),translate_var(--eased-duration-spring-snappy)_var(--eased-spring-snappy)] group-data-pressed/cap:translate-y-px group-data-pressed/cap:bg-surface-active group-data-pressed/cap:text-text group-data-pressed/cap:duration-(--eased-duration-instant) group-data-pressed/cap:ease-standard reduced:group-data-pressed/cap:translate-y-0 ${SIZES[size]}`} > {spoken ? ( <> <span aria-hidden>{glyph}</span> <span className="sr-only">{spec.name}</span> </> ) : ( glyph )} </span> </kbd> );}Keep the folder layout
The files import each other by relative path (
../lib/motion), so keep each one at the path shown, undercomponents/eased/.
Usage
import { Kbd } from "@/components/eased/kbd/kbd";export function SearchHint() { return ( <p className="text-ui-sm text-text-muted"> Press <Kbd>/</Kbd> to search, or <Kbd keys={["⌘", "K"]} /> for commands. </p> );}Examples
Lighting up a row
useKeysHeld is true while every key in the chord is down, from the same listener the caps use.
import { Kbd, useKeysHeld } from "@/components/eased/kbd/kbd";function ShortcutRow({ label, keys }: { label: string; keys: string[] }) { const held = useKeysHeld(keys); return ( <div className={`flex justify-between rounded-control-sm px-2 py-1 ${held ? "bg-surface-hover" : ""}`}> {label} <Kbd keys={keys} /> </div> );}A replayed shortcut
pressed forces the look and stops the cap listening.
<Kbd keys={["⇧", "⌘", "P"]} pressed={step === "press"} size="md" />API reference
Kbd
| Prop | Type | Default | Description |
|---|---|---|---|
children | string | One key: "K", "⌘", "Esc", "↑". | |
keys | string[] | A chord, one cap per key. Takes precedence over children. | |
listen | boolean | true | Press the cap while the real key is held. |
pressed | boolean | Force the pressed look. Overrides listen. | |
size | "sm" | "md" | "sm" | 20px or 24px caps. |
className | string | "" | On the cap, or on the wrapper of a chord. |
Glyphs map to keys: ⌘/cmd Meta, ⌥/opt/alt Alt, ⇧/shift Shift, ⌃/ctrl Control, ↵/⏎/enter/return Enter, ⎋/esc Escape, ⌫ Backspace, ⌦ Delete, ⇥/tab Tab, ␣/space Space, the four arrows, and F1 to F12. Letters and digits match by physical key, so ⌥K still presses the K cap; any other glyph matches the key it types.
useKeysHeld
useKeysHeld(keys: string[], enabled = true): boolean. True while every key in keys is down. false on the server.
Accessibility
- Each cap is a
kbdelement; a chord is akbdofkbds. - Symbols and abbreviations are read by name:
⌘is announced as "Command",Escas "Escape",←as "Left arrow". Letters and digits are read as written. - The caps listen only; they never take focus or handle a key, so they cannot clash with the shortcut they show.
Motion
When the key goes down the face drops 1px onto its base, and its fill and text shift to the active colours, in 100ms. When it comes up the face springs back on the snappy spring and the colours return in 150ms. The press never moves the layout: the base shows 1px below the face at rest and the face covers it. Reduced motion keeps only the colour change.
Notes for AI
Use Kbd for every displayed shortcut, with keys for chords and the platform glyphs (⌘ on macOS, ctrl elsewhere). Use useKeysHeld to highlight the row, menu item or button a shortcut belongs to; it shares the caps' single window listener, so it is cheap to call many times.
Do not add your own keydown listeners to animate the caps, and do not use Kbd to handle the shortcut itself: it only displays it. One listener serves every cap; it clears all keys when ⌘ is released, the window loses focus or the tab is hidden, because macOS drops key-ups while ⌘ is held.
Related components
- Cmd+K Command Palette: a list of actions with their shortcuts.
- Animated Dropdown Menu: menu items that show a shortcut.
- Animated Tooltip: a hint that can carry a shortcut.