Skip to the content

Controls and surfaces

kbd

A key cap for showing a keyboard key or a shortcut.

Display

When to use it

Naming a key the reader can press: in a menu item, a tooltip, a hint under a field. Wrap a combination in KbdGroup, one Kbd per key.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Display page.

Card

Usage

Example figures, this month

62% of the plan used

Stat

Visitors

12,480

+8.2%than last month

Errors

31

−4than last week

Badge, avatar, kbd

SolidSoftOutlineLiveReviewFailed
AKHMRW
CtrlKLoading
or

Alert and empty state

Heads up
Billing moves to the first of the month.

No projects yet

Create one to see it here.

Table

Example rows, not real customers.
NamePlanSeatsStatus
Example StudioTeam12Active
Example BakerySolo1Trial
Example TravelCompany48Active

Add it

npx shadcn@latest add @rivier-ui/kbd

The registry is named once in components.json: see Installation.

Example

Import it from @/components/ui/kbd.

<Kbd>Esc</Kbd>
<KbdGroup>
  <Kbd>Ctrl</Kbd>
  <Kbd>K</Kbd>
</KbdGroup>

What it needs

Plain markup: it can stay a server component. No packages beyond React.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/ui/kbd.tsx
/**
 * @name kbd
 * @category ui
 * @description A key cap for showing a keyboard key or a shortcut.
 * @when Naming a key the reader can press: in a menu item, a tooltip, a hint under a field. Wrap a combination in KbdGroup, one Kbd per key.
 * @example
 * <Kbd>Esc</Kbd>
 * <KbdGroup>
 *   <Kbd>Ctrl</Kbd>
 *   <Kbd>K</Kbd>
 * </KbdGroup>
 */
import type { ComponentProps } from "react";
import { cn } from "@/lib/cn";

export function Kbd({ className, ...props }: ComponentProps<"kbd">) {
  return (
    <kbd
      data-slot="kbd"
      className={cn(
        "inline-flex h-5 min-w-5 items-center justify-center rounded-control border border-border-strong bg-muted px-1 font-mono text-xs font-medium whitespace-nowrap text-foreground [&_svg]:size-3",
        className,
      )}
      {...props}
    />
  );
}

/** Keys pressed together. Renders an outer kbd, which is the correct markup for a combination. */
export function KbdGroup({ className, ...props }: ComponentProps<"kbd">) {
  return <kbd data-slot="kbd-group" className={cn("inline-flex items-center gap-1 font-mono", className)} {...props} />;
}