# Kbd

Composable keycaps for keyboard keys and shortcuts.

Display a keyboard key with `Kbd`, or combine several keys with `Kbd.Group`. Keycaps are informational: the application owns shortcut registration and handling.

```tsx
import { Kbd, Stack } from "@zentara-co/zenos-ui";

function KbdExample() {
  return (
    <Stack direction="row" gap="5" align="center" wrap="wrap">
      <Kbd>Ctrl</Kbd>
      <Kbd>Shift</Kbd>
      <Kbd>Enter</Kbd>
      <Kbd>Esc</Kbd>
      <Kbd.Group>
        <Kbd>Ctrl</Kbd>
        <span>+</span>
        <Kbd>K</Kbd>
      </Kbd.Group>
    </Stack>
  );
}
```

## Import

```tsx
import { Kbd } from "@zentara-co/zenos-ui";
```

## Group

Use `Kbd.Group` to keep the keys of a shortcut together. Each `Kbd` renders a native `kbd` element; the group renders a span. Separators are optional and belong to the content you compose. Import only `Kbd` to use both keycaps and groups. `Kbd.Root` is an alias for `Kbd`.

```tsx
<Kbd.Group>
  <Kbd>Ctrl</Kbd>
  <span>+</span>
  <Kbd>K</Kbd>
</Kbd.Group>
```

```tsx
import { Kbd, Text } from "@zentara-co/zenos-ui";

function KbdGroupExample() {
  return (
    <Text size="s">
      Press{" "}
      <Kbd.Group>
        <Kbd>Ctrl</Kbd>
        <span>+</span>
        <Kbd>K</Kbd>
      </Kbd.Group>{" "}
      to open the command menu.
    </Text>
  );
}
```

Groups inherit text direction from their container. Set native `dir="rtl"` when a group needs right-to-left presentation independently of the page.

## Inside a button

Place a keycap next to an action label without adding another focusable control. The button keeps its native keyboard behavior; the keycap does not install a shortcut.

```tsx
import { Button, Kbd } from "@zentara-co/zenos-ui";

function KbdButtonExample() {
  return (
    <Button className="gap-3">
      Accept <Kbd>Enter</Kbd>
    </Button>
  );
}
```

## Tooltip

Combine an action description and keycaps inside `Tooltip.Content`. Keep the trigger's accessible name meaningful even when the tooltip is closed.

```tsx
import { Button, Kbd, Stack, Tooltip } from "@zentara-co/zenos-ui";

function KbdTooltipExample() {
  return (
    <Tooltip.Provider>
      <Tooltip.Root>
        <Tooltip.Trigger render={<Button />}>Save</Tooltip.Trigger>
        <Tooltip.Content>
          <Stack direction="row" align="center" gap="2">
            Save changes{" "}
            <Kbd.Group>
              <Kbd>Ctrl</Kbd>
              <Kbd>S</Kbd>
            </Kbd.Group>
          </Stack>
        </Tooltip.Content>
      </Tooltip.Root>
    </Tooltip.Provider>
  );
}
```

## Input group

Use `InputGroup.Addon` to show shortcut hints beside a field. Label the input independently; the keycaps do not replace its accessible name.

```tsx
import { InputGroup, Kbd } from "@zentara-co/zenos-ui";

function KbdInputGroupExample() {
  return (
    <InputGroup className="w-full max-w-sm">
      <InputGroup.Control aria-label="Search commands" placeholder="Search commands…" />
      <InputGroup.Addon>
        <Kbd.Group>
          <Kbd>Ctrl</Kbd>
          <Kbd>K</Kbd>
        </Kbd.Group>
      </InputGroup.Addon>
    </InputGroup>
  );
}
```

## Custom styling

Use the usual `className` prop to override keycap appearance or group spacing. No `variant` or `color` props are required. Classes apply after the defaults, and native props, styles, and refs are forwarded to the rendered element.

```tsx
import { Kbd } from "@zentara-co/zenos-ui";

function KbdCustomExample() {
  return (
    <Kbd.Group className="gap-2">
      <Kbd className="h-6 rounded-md bg-background-accent-purple-subtlest-default px-2 text-text-accent-purple-bolder">
        Ctrl
      </Kbd>
      <Kbd className="h-6 rounded-md bg-background-accent-purple-subtlest-default px-2 text-text-accent-purple-bolder">
        K
      </Kbd>
    </Kbd.Group>
  );
}
```

## Platform-aware shortcuts

The optional `keys` shorthand remains available for automatic key labels. Join tokens with `+`; use `plus` for a literal plus key. `mod` displays Command on Apple platforms and Control elsewhere, with a matching spoken label. When `keys` is supplied, it takes precedence over children.

```tsx
import { Kbd, Stack, Text } from "@zentara-co/zenos-ui";

function KbdShortcutListExample() {
  return (
    <Stack gap="3" className="w-full max-w-sm">
      <Stack direction="row" align="center" justify="between" gap="4">
        <Text size="s">Open command menu</Text>
        <Kbd keys="mod+k" />
      </Stack>
      <Stack direction="row" align="center" justify="between" gap="4">
        <Text size="s">Create new item</Text>
        <Kbd keys="mod+enter" />
      </Stack>
      <Stack direction="row" align="center" justify="between" gap="4">
        <Text size="s">Close overlay</Text>
        <Kbd keys="escape" />
      </Stack>
    </Stack>
  );
}
```

Recognized names include `mod`, `ctrl`, `alt`, `shift`, `enter`, `escape`, `tab`, `backspace`, the four arrow directions, and `plus`. Other tokens render as uppercase text. Literal children, such as `<Kbd>Ctrl</Kbd>`, are displayed exactly as supplied and are not platform-dependent.

```tsx
import { Kbd, Stack } from "@zentara-co/zenos-ui";

function KbdSpecialKeysExample() {
  return (
    <Stack direction="row" gap="4" align="center" wrap="wrap">
      <Kbd keys="up+down" />
      <Kbd keys="left+right" />
      <Kbd keys="shift+tab" />
      <Kbd keys="backspace" />
      <Kbd keys="shift+plus" />
    </Stack>
  );
}
```

## Anatomy

```text
Kbd
Kbd.Root
Kbd.Group
```

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| children | ReactNode | — | Literal key text or an icon rendered in a native kbd keycap. Group multiple keys with Kbd.Group. |
| className | string | — | Custom keycap styles or group spacing. No variant or color props are required. |
| keys | string | — | Optional platform-aware shortcut shorthand. Tokens are separated by +; use plus for a literal plus key. When supplied, it takes precedence over children. |
| native props | React.HTMLAttributes<HTMLElement> | — | Forwards native attributes, events, aria-*, data-*, className, style, and ref. |

Unless narrowed above, each DOM-rendering part also accepts its underlying native or Base UI props, including className, style, aria-*, and data-*.

## Accessibility

Children-based keycaps use native keyboard-input semantics and leave their text available to assistive technology. Spell out ambiguous key symbols or pair a decorative symbol with visually hidden text. `Kbd.Group` only groups content and does not add interaction or focus.

The `keys` shorthand exposes the whole shortcut as one accessible image label and hides the individual visual keycaps from assistive technology. Neither form listens for keyboard commands. Keep the related action clear in nearby text and only advertise shortcuts that the application implements.

Source: https://zenos-ui.pages.dev/components/kbd/