Kbd
Shows a keyboard key or a shortcut. Use it in text, tooltips, menu rows, and footer hints.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/kbd.jsonCopies Kbd into your app, installs the packages, and adds the styles. Then import it from @/components/ui/kbd. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-mergeAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Copy and paste the following code into your project.
import type { ComponentProps } from 'react'; import { cn } from '@/lib/sprout/classes'; export const kbdVariantNames = ['default', 'outline'] as const; export type KbdVariant = (typeof kbdVariantNames)[number]; export interface KbdProps extends ComponentProps<'kbd'> { variant?: KbdVariant; } export function Kbd({ variant = 'default', className, ...props }: KbdProps) { return <kbd data-slot="kbd" data-variant={variant} className={cn('sprout-kbd', className)} {...props} />; } export type KbdGroupProps = ComponentProps<'kbd'>; export function KbdGroup({ className, ...props }: KbdGroupProps) { return <kbd data-slot="kbd-group" className={cn('sprout-kbd-group', className)} {...props} />; }Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-kbd { box-sizing: border-box; border-radius: calc(var(--sprout-radius-control) - 2px); background: var(--sprout-secondary); width: fit-content; min-width: 20px; height: 20px; color: var(--sprout-muted); font-family: var(--sprout-font); font-size: var(--sprout-text-xs); white-space: nowrap; vertical-align: middle; user-select: none; pointer-events: none; border: 1px solid #0000; flex-shrink: 0; justify-content: center; align-items: center; gap: 4px; padding-inline: 4px; font-style: normal; font-weight: 500; line-height: 1; display: inline-flex; } .sprout-kbd[data-variant="outline"] { border-color: var(--sprout-line); background: var(--sprout-surface); } .sprout-kbd :where(svg) { flex-shrink: 0; width: 12px; height: 12px; } .sprout-kbd-group { font: inherit; vertical-align: middle; align-items: center; gap: 4px; display: inline-flex; } @media (forced-colors: active) { .sprout-kbd { border-color: canvastext; } } @media (prefers-reduced-motion: no-preference) { .sprout-kbd { transition: background-color var(--sprout-motion-fast) var(--sprout-motion-ease), border-color var(--sprout-motion-fast) var(--sprout-motion-ease), color var(--sprout-motion-fast) var(--sprout-motion-ease); } } }Import it from
@/components/ui/kbd. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/kbd.
Variants
One 20px size. Keys grow with their label, so a single symbol stays square and a word gets room.
variant="default"Muted fill. Standalone keys and keys inside text.variant="outline"Bordered surface. Hints on tinted strips and selected rows.Keys and key groups
KbdGroup holds keys pressed together. Icons inside a key are 12px.
- Open search
- ⌘K
- Send message
- ⌘Enter
- Move between results
- Up arrowDown arrow
- Delete draft
- CmdBackspace
Press Esc to close.
Press ⌘K anywhere to search notes, projects, and people.
Footer hints
Outline keys on a tinted strip, each followed by what it does. The strip wraps instead of overflowing.
Selected row
A hidden ↵ key appears on the highlighted row and replaces its shortcut. Use the arrow keys in the field, then Enter.
Nothing chosen yet.
In a tooltip
Keys sit beside the tooltip label and keep their fill on the tooltip surface.
Usage
import { Kbd, KbdGroup } from "@tulipfarm/sprout/kbd";
<Kbd>Enter</Kbd>
<KbdGroup>
<Kbd>⌘</Kbd>
<Kbd>K</Kbd>
</KbdGroup>
<p>Press <Kbd>Esc</Kbd> to close.</p>
<span className="flex items-center gap-1.5">
<Kbd variant="outline">↵</Kbd>
Open
</span>Kbd renders a kbd element. It is 20px tall and at least 20px wide, so one symbol is square and a word gets padding.variant is "default", a muted fill, or "outline", a border on the surface color. Use outline on tinted strips, such as a search footer, and on highlighted rows. There is one size.
KbdGroup holds keys pressed together and spaces them 4px apart. It is also a kbd, which is how HTML marks a key combination. An svg inside a key is 12px unless you give it a size class.
Keys ignore the pointer and cannot be selected, so a click on a row or button with a key inside still reaches that row or button.
Selected row
<CommandItem className="result">
{title}
<Kbd variant="outline" aria-hidden="true" className="result-enter">↵</Kbd>
</CommandItem>
/* CSS */
.result-enter { display: none; }
.result[data-selected] > .result-enter { display: inline-flex; }Hide the key until its row is highlighted. Mark it aria-hidden because the row already says it can be chosen.
Accessibility
Kbd is not focusable and has no role. Screen readers read its text. Some symbols, such as ⌘, are read in odd ways, so put the action in words next to the key. For a key that shows only an icon, add visually hidden text inside it. Do not put aria-label on the key.
Color changes follow the theme in 120ms and stop when reduced motion is requested. In forced colors mode every key gets a visible border.
Theme tokens
The default key uses --sprout-secondary and --sprout-muted. The outline key uses --sprout-surface and --sprout-line. Radius follows --sprout-radius-control.