Skip to content

Kbd

Shows a key or a shortcut, so people can read which keys to press.

⌘KEscCtrlShiftP

Installation

pnpm dlx shadcn@latest add @silkware/kbd

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn

Then copy the source into components/ui/kbd.tsx:

components/ui/kbd.tsx
"use client"
import { cn } from "cn"import { Keyboard as KbdPrimitive } from "react-aria-components"
function Kbd({ className, ...props }: React.ComponentProps<"kbd">) {  return (    <KbdPrimitive      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 (    <KbdPrimitive      data-slot="kbd-group"      className={cn("inline-flex items-center gap-1", className)}      {...props}    />  )}
export { Kbd, KbdGroup }

Usage

import { Kbd, KbdGroup } from "@/components/ui/kbd"
<KbdGroup>  <Kbd>⌘</Kbd>  <Kbd>K</Kbd></KbdGroup>

Kbd shows a shortcut, it does not make one. Handling the keys is up to you, or to the component that owns the shortcut, such as a Command palette. A Kbd ignores the pointer, so it never takes a click away from the control around it.

Examples

In a button

Put a Kbd after the label of a button to show its shortcut. It sits at the end of the row and keeps its size.

In a tooltip

Inside a Tooltip the keys switch to the tooltip's inverted colours, so they stay readable.

In text

Kbd fits in a sentence. Write the sentence so it still reads without the keys.

Press Enter to approve the order or Esc to go back to the list.

Accessibility

KeyAction
TabDoes nothing. A Kbd is text and is not focusable.

React Aria provides:

  • The Keyboard primitive, which renders a kbd element, the HTML element for keyboard input.

You provide:

  • The shortcut itself, wired to the action. A Kbd that shows a key that does nothing is worse than none.
  • A sentence or a label next to the keys when they are the only mention of the action.
  • Keys that exist for your reader. Show the right modifier for the platform: ⌘ on macOS, Ctrl elsewhere.
  • Separators between keys that are not read aloud: mark a plus sign with aria-hidden.

Guidelines

When to use

  • To show the shortcut of an action in a button, a menu item or a tooltip.
  • To tell people which key to press in help text.

When not to use

  • For code, a command or a file name. Use inline code, as in Typography.
  • To make a control. A Kbd cannot be pressed. Use a Button.
  • For long sequences. Past three keys, describe the action in words.

Content

  • Write the key as it is printed: "Esc", "Enter", "Ctrl", "Shift", a letter or a symbol such as ⌘.
  • One key per Kbd, and a KbdGroup around keys pressed together.
  • Capitalise names, not letters: "Shift", "K".

Key names

Open the menu with CtrlK

Do

Name each key in its own Kbd and group the keys pressed together.

Open the menu with Press Control and then the letter K

Don’t

Don’t put a sentence in a Kbd. It is a key, not a label.

API reference

Kbd

Renders a kbd through the React Aria Keyboard. It is 20px high, with a 20px minimum width, a small radius and a muted fill. It accepts every kbd prop.

KbdGroup

Renders a kbd element as an inline flex container with a 4px gap, to hold several keys pressed together.