Kbd

Displays a keyboard key or a combination of keys.

⌘⇧⌥⌃Ctrl+B
import { Kbd, KbdGroup } from "@/components/ui/kbd"
import { VStack } from "@/components/ui/stack"
import { Text } from "@/components/ui/text"

export default function KbdDemo() {
  return (
    <VStack gap={4} align="center">
      <KbdGroup>
        <Kbd>⌘</Kbd>
        <Kbd>⇧</Kbd>
        <Kbd>⌥</Kbd>
        <Kbd>⌃</Kbd>
      </KbdGroup>
      <KbdGroup>
        <Kbd>Ctrl</Kbd>
        <Text size="xs" tone="muted">
          +
        </Text>
        <Kbd>B</Kbd>
      </KbdGroup>
    </VStack>
  )
}

Installation

pnpm dlx shadcn@latest add @arene/kbd

Needs the arene setup first.

Usage

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

Group

Wrap keys pressed together in KbdGroup. It renders a kbd of kbds, which is how HTML marks up a combination, and sits inline in running text.

Use ⌘K to open the command palette, or Esc to close it.

import { Kbd, KbdGroup } from "@/components/ui/kbd"
import { Text } from "@/components/ui/text"

export default function KbdGroupDemo() {
  return (
    <Text render={<p />} size="sm" tone="muted">
      Use{" "}
      <KbdGroup>
        <Kbd>⌘</Kbd>
        <Kbd>K</Kbd>
      </KbdGroup>{" "}
      to open the command palette, or <Kbd>Esc</Kbd> to close it.
    </Text>
  )
}

Input group

Put a shortcut at the end of a search field with an InputGroupAddon.

⌘K
import { Input } from "@/components/ui/input"
import { InputGroup, InputGroupAddon } from "@/components/ui/input-group"
import { Kbd, KbdGroup } from "@/components/ui/kbd"
import { MagnifyingGlassIcon } from "@phosphor-icons/react/dist/csr/MagnifyingGlass"
import { widths } from "@/theme/widths"

export default function KbdInputGroup() {
  return (
    <InputGroup xstyle={widths.xs}>
      <Input placeholder="Search..." aria-label="Search" />
      <InputGroupAddon>
        <MagnifyingGlassIcon aria-hidden="true" />
      </InputGroupAddon>
      <InputGroupAddon align="inline-end">
        <KbdGroup>
          <Kbd>⌘</Kbd>
          <Kbd>K</Kbd>
        </KbdGroup>
      </InputGroupAddon>
    </InputGroup>
  )
}

API reference

Pass render to replace a part's element or compose it with another component.

Both parts render a kbd through Base UI's useRender, and take xstyle, className and inline style alongside every kbd attribute.

Kbd

PropTypeDefault
renderReactElement | ((props, state) => ReactElement)<kbd />
xstyleStyleXStyles—
classNamestring—

A flat keycap, 1.25rem tall and at least as wide, in the monospace face. It is not focusable.

KbdGroup

An inline row of keys with a 0.25rem gap. Text between keys, such as a +, sits in the same row.

kbdStyles

The style maps behind the component. Spread them onto any element.

KeyWhat it is
baseThe cap: fill, hairline, size and type
groupThe inline row

Data attributes

AttributeOnEffect
data-slot="kbd"a keyTargets a key from a parent
data-slot="kbd-group"the groupTargets the group from a parent

CSS variables

VariableOnEffect
--arene-icon-sizea keyThe icon's size, read by interop.css. Defaults to 12px