antlitz
Menu

Kbd

A key the reader is meant to press — a shortcut in a tooltip, a hint beside a search field. Renders a native kbd.

import { Kbd } from "antlitz";

API

PropTypeDefault
asChildbooleanfalse
radius"none" | "sm" | "md" | "lg" | "full"none
size"sm" | "md" | "lg"md
tone"base" | "muted" | "subtle"base
variant"outline" | "soft" | "plain"outline

Usage

Esc
export function Basic() {
  return <Kbd>Esc</Kbd>;
}

Variants

outlinesoftplain
export function Variants() {
  return VARIANTS.map((variant) => (
    <Kbd key={variant} variant={variant}>
      {variant}
    </Kbd>
  ));
}

outline is the default because a key reads as a key when it looks like one: a bordered cap on a fill a shade off the page. Every variant keeps a 1px border — transparent where it is not drawn — so switching variants never changes the size of a key sitting in a line of text.

Sizes

smmdlg
export function Sizes() {
  return SIZES.map((size) => (
    <Kbd key={size} size={size}>
      {size}
    </Kbd>
  ));
}

Like Code, size sets padding and the text is 0.875em, so a key stays in proportion with the sentence around it.

Radius

nonesmmdlgfull
export function Radius() {
  return RADII.map((radius) => (
    <Kbd key={radius} radius={radius}>
      {radius}
    </Kbd>
  ));
}

Tones

basemutedsubtle
export function Tones() {
  return TONES.map((tone) => (
    <Kbd key={tone} tone={tone}>
      {tone}
    </Kbd>
  ));
}

Combinations

Press Ctrl + K to search.

export function Combination() {
  return (
    <Text>
      Press{" "}
      <kbd>
        <Kbd>Ctrl</Kbd> + <Kbd>K</Kbd>
      </kbd>{" "}
      to search.
    </Text>
  );
}

HTML marks up a key combination as a kbd of kbds: one element for the whole input, one for each key in it. Wrap the keys in a plain kbd rather than another Kbd — nesting the component would draw a cap around the caps, and shrink the inner text to 0.875em of 0.875em.

Symbols

Press Command K to search.

export function Symbols() {
  return (
    <Text>
      Press{" "}
      <Kbd>
        <span aria-hidden="true">⌘</span>
        <span className="sr-only">Command</span>
      </Kbd>{" "}
      <Kbd>K</Kbd> to search.
    </Text>
  );
}

A screen reader announces ⌘ by its Unicode name, "place of interest sign". Hide the symbol and put the key's name beside it in screen-reader-only text. kbd has no role of its own, so an aria-label on it is not reliably read.

Prose

Kbd is for interface text. Inside content rendered with the Typeset stylesheet, a plain kbd element is already styled and no component is needed.