# Kbd

## Usage

`Kbd` presents a keyboard shortcut as a small group of painted key badges, such as `⌘K` or `⇧↵`. It belongs next to the action it triggers — on the right of a menu item, inside a tooltip, or at the end of a line of help text — so you can mention the shortcut without writing out "hold Command and press K".

Pass the shortcut as one string and separate the keys with `+`:

```vue
<script setup lang="ts">
import { Kbd } from '@astryx-vue/core/Kbd'
</script>

<template>
  <Kbd keys="mod+k" />
</template>
```

Every segment becomes its own badge, so `mod+k` paints two keys. Whitespace around a segment is ignored and the case does not matter, which makes `mod + K` and `mod+k` the same shortcut. Anything the component does not recognize is uppercased as written, so `f1` shows as `F1`.

The modifiers follow the platform: on Apple platforms they paint Apple's symbols, and elsewhere they paint the text printed on the keycap. A server render does not know the platform yet, so before hydration every modifier shows its text. `mod` is the Command key on Apple platforms and Control elsewhere, so reach for `mod` when you describe a shortcut that works across platforms, and use `ctrl` only when the shortcut really is bound to the Control key. The other named keys have a fixed glyph:

| Written as | Painted as |
| --- | --- |
| `mod` | `⌘` on Apple platforms, `Ctrl` elsewhere |
| `ctrl` | `⌃` on Apple platforms, `Ctrl` elsewhere |
| `alt` | `⌥` on Apple platforms, `Alt` elsewhere |
| `shift` | `⇧` on Apple platforms, `Shift` elsewhere |
| `meta` | `⌘` on Apple platforms, `Win` elsewhere |
| `enter` | `↵` |
| `backspace` | `⌫` |
| `tab` | `⇥` |
| `escape` | `Esc` |
| `up` / `down` / `left` / `right` | `↑` `↓` `←` `→` |
| `plus` | `+` |

`esc` is an alias for `escape`, and `return` is an alias for `enter`; an alias paints and is announced exactly like the key it names. Because `+` separates the keys, write `plus` for the plus key itself, as in `shift+plus`.

To a screen reader the whole group is one image named "Command + K"; the glyphs inside the badges are hidden because `⌘` and `⇧` are announced as nothing useful. The keys are spoken as words instead. Note that `Kbd` only presents a shortcut: it does not listen for keystrokes, and the shortcut still has to be implemented by the product.

Each key badge keeps the theme's height at the default text size and grows with a larger text size, so a bigger glyph stays inside its badge.

### Anatomy

- **Shortcut**: Group that presents the complete keyboard shortcut and its accessible name.
- **Key badge**: Painted key badge rendered once for each key in the shortcut.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Place the shortcut next to the action it triggers: on the right of a menu item, in a tooltip, or straight after the wording it belongs to. |
| Do | Describe a cross-platform shortcut with `mod` rather than `ctrl` or `cmd`, so macOS users read `⌘` and everyone else reads `Ctrl`. |
| Do | Only mention shortcuts that exist and can actually be pressed in the current context. A wrong hint is worse than no hint. |
| Do | Spell shortcuts the same way as the rest of the product, so documentation and settings never disagree. |
| Don't | Treat `Kbd` as the only way to discover an action. Shortcuts supplement visible controls, they do not replace them. |
| Don't | Scatter shortcut hints across a screen. They are supporting information; in bulk they drown out the content. |
| Don't | Hand-write `⌘` or `⇧` characters instead of using the component. Hand-written glyphs do not follow the platform, and screen readers cannot announce what they mean. |

## Examples

### Inline instructions

Keyboard shortcuts rendered inline within instructional text.

```vue
<script setup lang="ts">
import { Kbd } from '@astryx-vue/core/Kbd'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <VStack :gap="3">
    <Text type="body">
      Press <Kbd keys="mod+k" /> to open the command palette.
    </Text>
    <Text type="body">
      Use <Kbd keys="mod+shift+p" /> to access all commands.
    </Text>
    <Text type="body">
      Press <Kbd keys="escape" /> to close the dialog.
    </Text>
    <Text type="body">
      Navigate with <Kbd keys="up" /> and <Kbd keys="down" /> arrow keys, then press <Kbd keys="enter" /> to select.
    </Text>
  </VStack>
</template>
```

### Menu shortcuts

Menu-style list pairing action labels with their keyboard shortcuts.

```vue
<script setup lang="ts">
import { Card } from '@astryx-vue/core/Card'
import { Kbd } from '@astryx-vue/core/Kbd'
import { List, ListItem } from '@astryx-vue/core/List'

const menuItems = [
  { label: 'Cut', keys: 'mod+x' },
  { label: 'Copy', keys: 'mod+c' },
  { label: 'Paste', keys: 'mod+v' },
  { label: 'Undo', keys: 'mod+z' },
  { label: 'Redo', keys: 'mod+shift+z' },
]
</script>

<template>
  <Card :padding="0">
    <List density="compact">
      <ListItem v-for="item in menuItems" :key="item.label" :label="item.label">
        <template #endContent>
          <Kbd :keys="item.keys" />
        </template>
      </ListItem>
    </List>
  </Card>
</template>
```

### Modifier combinations

Modifier combinations and special keys rendered as shortcut badges.

```vue
<script setup lang="ts">
import { Kbd } from '@astryx-vue/core/Kbd'
import { HStack, VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <VStack :gap="3">
    <HStack :gap="4">
      <Kbd keys="mod+k" />
      <Kbd keys="shift+enter" />
      <Kbd keys="ctrl+c" />
      <Kbd keys="alt+tab" />
    </HStack>
    <HStack :gap="4">
      <Kbd keys="mod+shift+z" />
      <Kbd keys="ctrl+alt+delete" />
      <Kbd keys="mod+shift+p" />
    </HStack>
    <HStack :gap="4">
      <Text type="body">
        Special keys and aliases:
      </Text>
      <Kbd keys="escape" />
      <Kbd keys="esc" />
      <Kbd keys="enter" />
      <Kbd keys="return" />
      <Kbd keys="backspace" />
      <Kbd keys="tab" />
      <Kbd keys="space" />
    </HStack>
  </VStack>
</template>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `keys` | `string` | - | Required. The keyboard shortcut to present, with the keys separated by `+`, such as `"mod+k"` or `"shift+plus"`. Each segment paints one key badge; whitespace around a segment is ignored, and an unrecognized segment is uppercased. Modifiers paint Apple's symbol on Apple platforms and text elsewhere and before hydration (see the table below). In development, a `keys` that is not a string throws an error naming the prop; Kbd does not read slot content. |

## Key names

| Written as | Painted as | Spoken as |
| --- | --- | --- |
| `mod` | `⌘` / `Ctrl` | Command on Apple platforms, Control elsewhere |
| `ctrl` | `⌃` / `Ctrl` | Control |
| `alt` | `⌥` / `Alt` | Alt |
| `shift` | `⇧` / `Shift` | Shift |
| `meta` | `⌘` / `Win` | META |
| `enter` / `return` | `↵` | Enter |
| `backspace` | `⌫` | Backspace |
| `tab` | `⇥` | Tab |
| `escape` / `esc` | `Esc` | Escape |
| `up` | `↑` | Up arrow |
| `down` | `↓` | Down arrow |
| `left` | `←` | Left arrow |
| `right` | `→` | Right arrow |
| `plus` | `+` | Plus |

A segment that is not in this table is painted uppercased as written and spoken as that same text, so `f1` paints as `F1`.

## Accessibility

The root element carries `role="img"` and an `aria-label` assembled from the spoken key names, so `keys="mod+k"` is announced as "Control + K". The badges themselves are `aria-hidden="true"`, because the glyphs they paint mean nothing to assistive technology. `role` and `aria-label` are computed by the component, and a consumer value for either is ignored; every other `aria-*` attribute passes through.

## Native attributes

The root element is a `<span>`.

The following go to the root element:

- HTML attributes.
- `aria-*` attributes, except `role` and `aria-label` (see the previous section).
- `data-*` attributes.
- `class`.
- `style`.
