Bloom Logo

Kbd

Display key bindings and keyboard shortcuts with sleek typography, variants, and size scales.

importimport { Kbd } from "@/components/ui/kbd/kbd";
$npx @bloomui-react/cli add kbd
kbd.tsx
import { cva, type VariantProps } from "class-variance-authority";
import * as React from "react";
import { cn } from "@/lib/utils";

const kbdVariants = cva(
  "inline-flex items-center justify-center font-mono font-medium select-none rounded-lg border transition-colors shadow-xs",
  {
    variants: {
      variant: {
        flat: "bg-zinc-100 dark:bg-zinc-800 text-zinc-700 dark:text-zinc-300 border-zinc-200/80 dark:border-zinc-700/80",
        bordered:
          "bg-transparent text-zinc-800 dark:text-zinc-200 border-zinc-300 dark:border-zinc-700",
        solid:
          "bg-zinc-900 dark:bg-zinc-100 text-zinc-100 dark:text-zinc-900 border-transparent",
      },
      size: {
        sm: "h-5 min-w-[20px] px-1.5 text-[10px]",
        md: "h-6 min-w-[24px] px-2 text-xs",
        lg: "h-7 min-w-[28px] px-2.5 text-sm",
      },
    },
    defaultVariants: {
      variant: "flat",
      size: "md",
    },
  },
);

export type KbdKey =
  | "command"
  | "cmd"
  | "shift"
  | "ctrl"
  | "option"
  | "alt"
  | "enter"
  | "delete"
  | "escape"
  | "tab"
  | "space text"
  | string;

const keySymbolMap: Record<string, string> = {
  command: "⌘",
  cmd: "⌘",
  shift: "⇧",
  ctrl: "⌃",
  option: "⌥",
  alt: "⌥",
  enter: "↵",
  delete: "⌫",
  escape: "Esc",
  tab: "⇥",
};

export interface KbdProps
  extends React.HTMLAttributes<HTMLElement>,
    VariantProps<typeof kbdVariants> {
  keys?: KbdKey[];
}

const Kbd = React.forwardRef<HTMLElement, KbdProps>(
  ({ className, variant, size = "md", keys, children, ...props }, ref) => {
    const renderKeys = () => {
      if (keys && keys.length > 0) {
        return keys
          .map((k) => keySymbolMap[k.toLowerCase()] || k.toUpperCase())
          .join("");
      }
      return children;
    };

    return (
      <kbd
        ref={ref}
        className={cn(kbdVariants({ variant, size, className }))}
        {...props}
      >
        {renderKeys()}
      </kbd>
    );
  },
);
Kbd.displayName = "Kbd";

export { Kbd, kbdVariants };

Default

Standard key binding badge.

K

Variants

Style variants using the variant prop: flat, bordered, or solid.

variant: flat | bordered | solid
Flat:CtrlC
Bordered:AltF4
Solid:ShiftEnter

Sizes

Scale badge dimensions using the size prop: sm, md, or lg.

size: sm | md | lg
⌘ Shift P⌘ Shift P⌘ Shift P

Keyboard Shortcuts List

Combining Kbd badges into a clean user interface reference list.

flex: anyflex-col: anygap-2: anymax-w-sm: anyborder: anyborder-zinc-200: anydark: anyborder-zinc-800: anybg-white: anybg-zinc-900: anyp-4: anyrounded-2xl: anyitems-center: anyjustify-between: anygap-1: anysize: anysm: any
Command Palette
K
Copy Code
CtrlC
Quick Search
/

Key Combination Sequence Formatter

Pass an array of key names via keys to automatically render symbols like ⌘, ⇧, ⌃, ⌥, ↵, and Esc.

keys: KbdKey[]
Shortcut:⌘⇧K
Navigation:⌥↵
Cancel:Esc

Props — Kbd

Supported properties for the Kbd component.

PropTypeDefaultDescription
keysKbdKey[]Array of key names formatted with mac/windows shortcut symbols.
variant'flat' | 'bordered' | 'solid''flat'Visual surface style variant.
size'sm' | 'md' | 'lg''md'Dimensional scale for key badge.