Kbd

Semantic keyboard shortcuts rendered as keycaps, with a group wrapper.

Preview

kbd-demo
K
Ctrl+Shift+P

Installation

pnpm dlx shadcn@latest add http://localhost:3000/r/kbd.json

Source

kbd.tsx
import * as React from "react";

import { cn } from "@/lib/utils";

function Kbd({ className, ...props }: React.ComponentProps<"kbd">) {
  return (
    <kbd
      data-slot="kbd"
      className={cn(
        "pointer-events-none inline-flex h-5 w-fit min-w-5 items-center justify-center gap-1 rounded-sm bg-muted px-1 font-sans text-xs font-medium text-muted-foreground select-none 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 };

Props

PropTypeDefaultDescription
classNamestring-Adds custom classes to the keycap.

Use KbdGroup to lay out several keycaps with consistent spacing.