# React Native Kbd (keyboard key)

> A React Native Kbd component for iOS, Android and the web: keyboard keys and shortcuts like ⌘K drawn as keycaps or chips, read aloud as words by screen readers.

# Kbd

A keyboard key, or a shortcut of several, for menus, tooltips and help text. Screen readers say the symbols as words: `⌘ K` is read "Command K".

```tsx
import { Kbd } from 'patine-ui';
```

```tsx live
import { Box, HStack, Kbd, Separator, Text, VStack } from 'patine-ui';

const SHORTCUTS = [
  { label: 'Search', keys: ['⌘', 'K'] },
  { label: 'New message', keys: ['C'] },
  { label: 'Command palette', keys: ['⌘', '⇧', 'P'] },
  { label: 'Close panel', keys: ['Esc'] },
];

export default function Example() {
  return (
    <Box border radius="lg" padding="md" style={{ width: 320 }}>
      <Text weight="semibold" style={{ marginBottom: 8 }}>Keyboard shortcuts</Text>
      <VStack>
        {SHORTCUTS.map((s, i) => (
          <React.Fragment key={s.label}>
            {i > 0 ? <Separator /> : null}
            <HStack justify="space-between" style={{ paddingVertical: 10 }}>
              <Text>{s.label}</Text>
              <Kbd keys={s.keys} />
            </HStack>
          </React.Fragment>
        ))}
      </VStack>
    </Box>
  );
}
```

## Example: a shortcuts sheet

A text editor's help panel: the same shortcuts for Mac and for Windows and Linux, grouped by what they do. Switch the platform to see the keys change.

```tsx live
import { useState } from 'react';
import { Box, HStack, Kbd, SegmentedControl, Separator, Text, VStack } from 'patine-ui';

const GROUPS = [
  { title: 'Formatting', items: [{ label: 'Bold', keys: ['mod', 'B'] }, { label: 'Italic', keys: ['mod', 'I'] }, { label: 'Link', keys: ['mod', 'K'] }] },
  { title: 'Editing', items: [{ label: 'Undo', keys: ['mod', 'Z'] }, { label: 'Redo', keys: ['mod', 'shift', 'Z'] }, { label: 'Find and replace', keys: ['mod', 'alt', 'F'] }] },
];
const KEYS = { mac: { mod: '⌘', shift: '⇧', alt: '⌥' }, pc: { mod: 'Ctrl', shift: 'Shift', alt: 'Alt' } };

export default function Example() {
  const [os, setOs] = useState<'mac' | 'pc'>('mac');
  return (
    <Box border radius="lg" padding="md" gap="md" style={{ width: '100%', maxWidth: 380 }}>
      <SegmentedControl items={[{ value: 'mac', label: 'Mac' }, { value: 'pc', label: 'Windows and Linux' }]} value={os} onValueChange={setOs} />
      {GROUPS.map((group) => (
        <VStack key={group.title}>
          <Text size="sm" weight="semibold" color="textSecondary">{group.title}</Text>
          {group.items.map((item, i) => (
            <VStack key={item.label}>
              {i ? <Separator /> : null}
              <HStack justify="space-between" align="center" style={{ paddingVertical: 8 }}>
                <Text>{item.label}</Text>
                <Kbd keys={item.keys.map((k) => KEYS[os][k] ?? k)} separator={os === 'pc' ? '+' : undefined} />
              </HStack>
            </VStack>
          ))}
        </VStack>
      ))}
    </Box>
  );
}
```

## In a sentence

```tsx live
import { HStack, Kbd, Text } from 'patine-ui';

export default function Example() {
  return (
    <HStack gap="xs" wrap>
      <Text>Press</Text>
      <Kbd keys={['Ctrl', 'C']} separator="+" size="sm" />
      <Text>to copy, or</Text>
      <Kbd size="sm">Esc</Kbd>
      <Text>to cancel.</Text>
    </HStack>
  );
}
```

## Designs

brand and web draw an outlined keycap with a heavier bottom edge, as GitHub and shadcn do. iOS and Material draw a flat tonal chip.

## Accessibility

The keys are one element named by the shortcut, with symbols as words: ⌘ Command, ⇧ Shift, ⌥ Option, ⌃ Control, ↵ Enter, ⌫ Delete, ⎋ Escape, ⇥ Tab and the arrows.

## Props

Also takes `View` props.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `string` | — | One key. |
| `keys` | `string[]` | — | A shortcut, one key each. Wins over `children`. |
| `separator` | `string` | none | Between keys, e.g. `'+'`. |
| `size` | `'sm' \| 'md'` | `'md'` | 20 or 24 points tall. |
| `textStyle` | `StyleProp<TextStyle>` | — | The key's text. |
| `theme`, `design` | | | As every component. |
