Kbd

A key on the keyboard.

Group keys pressed together with KbdGroup. Inside a tooltip, a key adapts to the inverted surface.

Preview
basic.tsx
import { Kbd, KbdGroup } from "~/components/ui/kbd"
import { Text } from "~/components/ui/text"

export default function Example() {
  return (
    <Text size="sm" tone="muted">
      Press{" "}
      <KbdGroup>
        <Kbd>⌘</Kbd>
        <Kbd>K</Kbd>
      </KbdGroup>{" "}
      to search, or <Kbd>Esc</Kbd> to close.
    </Text>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesTextprimitive
Used byMenu
Packages None.

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/kbd.json

Manually

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/kbd.tsx
import type * as React from "react"

import { textVariants } from "~/components/ui/text"
import { cn } from "~/lib/utils"

/** A key on the keyboard, like ⌘ or K. Readable inside tooltips and on inverted surfaces too. */
function Kbd({ className, ...props }: React.ComponentProps<"kbd">) {
  return (
    <kbd
      data-slot="kbd"
      className={cn(
        textVariants({ size: "xs", weight: "medium", family: "body", tone: "muted" }),
        "pointer-events-none inline-flex h-5 w-fit min-w-5 items-center justify-center gap-2xs rounded-sm bg-muted px-2xs select-none",
        "[&_svg:not([class*='size-'])]:size-3",
        "in-data-[slot=tooltip-content]:bg-background/20 in-data-[slot=tooltip-content]:text-background",
        className,
      )}
      {...props}
    />
  )
}

/** Keys pressed together, like ⌘ K. */
function KbdGroup({ className, ...props }: React.ComponentProps<"kbd">) {
  return (
    <kbd
      data-slot="kbd-group"
      className={cn("inline-flex items-center gap-2xs", className)}
      {...props}
    />
  )
}

export { Kbd, KbdGroup }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Kbd

A key on the keyboard, like ⌘ or K. Readable inside tooltips and on inverted surfaces too.

Takes the props of a native div.

KbdGroup

Keys pressed together, like ⌘ K.

Takes the props of a native div.