# Phone keyboard

The on screen phone keyboard with a strip above the keys: word suggestions, rewrite tones with a preview that underlines what changed, and dictation. Letter keys pop into a callout as a script types.

Example: Phone keyboard with a rewrite. A reply typed with key callouts and auto shift, then rewritten friendlier with a preview and replaced. Loops; tap a tone to take over. Preview: https://chatcn.dev/preview/phone-keyboard-demo

## Installation

```bash
npx shadcn@latest add https://chatcn.dev/r/phone-keyboard.json
```

## Usage

```tsx
import { PhoneKeyboard, useKeyboardScript } from "@/components/ui/chatcn/phone-keyboard"

const story = [
  { type: "type", text: "can u move the meeting to thurs", wpm: 120 },
  { type: "rewrite", tone: "Friendlier", result: "Could we move it to Thursday? Thanks!" },
  { type: "wait", ms: 1400 },
  { type: "replace" },
] as const

function Demo() {
  const s = useKeyboardScript(story, { loop: true })
  return (
    <div ref={s.ref}>
      <MyField value={s.value} />
      <PhoneKeyboard
        value={s.value}
        pressed={s.pressed}
        mode={s.mode}
        tone={s.tone}
        preview={s.preview}
        suggestions={s.suggestions ?? undefined}
      />
    </div>
  )
}
```

The keyboard fills its parent's width and lays its keys out with `phoneRows()` from `chatcn-keyboard`, designed at 393 px. It is drawn from your theme tokens, so it follows light, dark and any preset. Under it sits 34 px for the home indicator; `safe={false}` drops it.

A script types with a believable rhythm: 70 words a minute by default, each key 25% faster or slower from a seeded generator, so every run looks the same. Text lands when a key comes up, as on a phone, and shift turns on by itself at the start and after a full stop, so the first letter comes out capitalised. Off screen and in a hidden tab the story waits.

- `mode="suggest"`: three words split by hairlines, the best guess in the middle.
- `mode="rewrite"`: a row of tones that scrolls sideways. Set `preview` and the keyboard grows to hold a card above the strip with the rewrite, the words that changed underlined, and Dismiss and Replace. The field above moves up with it, so it stays in view. Replace calls `onValueChange` with the preview, then `onReplace`; Dismiss and Escape call `onDismiss`. After a replace the strip offers Undo for `undoFor` ms (4 s), which restores the original text.
- `mode="voice"`: the waveform from `level` and Listening; the space bar reads Tap to stop.
- The strip is one 44 px box and the modes crossfade inside it, so nothing below it moves.

With `interactive` the keys take taps and become one tab stop: arrows move between keys, Enter or Space taps one, and keys on a physical keyboard light their twin. Reduced motion keeps the darker pressed key and drops the callout. The keys never mirror in right to left pages; the strip and the card do.

## Examples

### States

Suggestions, a rewrite with its preview, dictation, shift on, interactive keys, dark, and under a `TextThread` conversation in `PlainScreen`.

Example: Phone keyboard states. Suggestions, a rewrite with its preview, dictation, shift on, interactive keys, dark and in a text thread. Preview: https://chatcn.dev/preview/phone-keyboard-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `value`, `defaultValue`, `onValueChange` | `string` | The text typed into; drives auto shift |
| `mode` | `suggest | rewrite | voice` | Default `suggest` |
| `suggestions`, `onSuggestion` | `[string, string, string]` | The middle word is the best guess |
| `tones`, `tone`, `onTone` | `string[]`, `string` | Default Shorter, Friendlier, Fix grammar, More formal |
| `preview`, `onReplace`, `onDismiss` | `string | null` | The rewrite card, open while set |
| `onUndo`, `undoFor` | `(original) => void`, `number` | Undo in the strip after a replace, default 4000 ms |
| `level` | `number | () => number` | Dictation input level, 0 to 1 |
| `pressed` | `string | null` | A scripted tap: a key code, `tone:<name>`, `suggestion:<n>` or `replace` |
| `interactive` | `boolean` | Keys take taps, arrows and physical key presses |
| `safe` | `boolean` | Default `true`: 34 px under the keys |

`useKeyboardScript(events, { loop, paused, loopGap })` takes `type` (with `wpm`), `suggest`, `pick`, `rewrite`, `replace`, `voice` and `wait` events and returns `value`, `pressed`, `mode`, `preview`, `tone`, `suggestions`, `restart` and the `ref` that pauses it off screen. A closing `wait` holds the last frame before a loop starts again.

Source: https://chatcn.dev/docs/components/phone-keyboard