# Template composer

Prompt templates with fillable slots inline in the sentence. Tab between them, watch filled ones settle into the text, and never send half a prompt.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Template composer. A coffee roaster's support desk fills a double charge reply slot by slot. Filled slots settle into the sentence, then it sends. Preview: https://chatcn.dev/preview/template-composer-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/template-composer
```

## Usage

```tsx
import { TemplateComposer } from "@/components/ui/chatcn/pro/template-composer"

<TemplateComposer
  templates={[
    {
      id: "refund",
      name: "Double charge reply",
      template: "Write a {{tone:warm}} reply to {{customer}} about order {{order_number}}.",
    },
  ]}
  onSubmit={({ text, template, values }) => send(text)}
/>
```

Templates use the same `{{name}}` and `{{name:default}}` syntax as the free prompt template, so one library of prompts serves both. Picking a template (the Templates button, or `/` in the empty composer) lays the sentence out with each slot as an inline chip named for what goes there. Slots are text, not inputs: long values wrap with the sentence, and a filled slot drops its chip and keeps only a dotted rule, so the finished prompt reads like something you wrote. Send with required slots empty does not send: it jumps to the first blank, marks the rest and names them under the composer. Edit as text turns the template into a plain draft, with blanks kept as `[Label]` so nothing silently disappears. With no template it is the free `PromptInput`, with the same Enter, paste and IME behaviour.

## Examples

### States

Example: States. Free text, a fresh template, half filled, Send blocked by blanks, ready, streaming, long values and disabled. Preview: https://chatcn.dev/preview/template-composer-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `templates` | `ComposerTemplate[]` | `{ id, name, description?, template, slots? }` |
| `template`, `defaultTemplate`, `onTemplateChange` | `string | null` | The active template, or null for free text |
| `values`, `defaultValues`, `onValuesChange` | `Record<string, string>` | What is typed in each slot |
| `onSubmit` | `({ text, template?, values? }) => void` | Only once every required slot is filled |
| `slots[name]` | `{ label?, optional?, multiline? }` | Per slot details. A slot with a default is optional |
| `slashOpens` | `boolean` | `/` in the empty composer opens the templates. Default true |
| `showMissing` | `boolean` | Show the blocked send hint now |
| `value`, `defaultValue`, `onValueChange` | `string` | The draft, controlled or not |
| `status`, `onStop` | `"idle" | "submitting" | "streaming"`, `() => void` | Send becomes Stop in the same footprint |
| `disabled` | `boolean` |  |
| `tools`, `actions`, `attachments` | `ReactNode` | Toolbar start, before Send, above the text |
| `labels` | `Partial<TemplateComposerLabels>` | Every string, for translation |

## Keyboard

| Key | Action |
| --- | --- |
| / | Open the templates from the empty composer |
| Tab, Shift Tab | Next and previous slot. Past the last slot, Tab moves on as usual |
| Right, Left at a slot's edge | Cross into the neighbouring slot |
| Backspace in an empty slot | Back to the previous slot |
| Enter | Send, or jump to the first blank if any are left |
| Escape | Stop a streaming reply |

## Accessibility

- Each slot is a `textbox` named by its label, with `aria-required` and `aria-invalid` after a blocked send.
- The sentence is a group labelled by the template's name, so the slots are heard in context.
- The blocked send hint is announced politely and carries an icon, not color alone.
- Repeated slots mirror the first and stay out of the tab order, so each blank is visited once.

## Motion

Focus moves are instant because they come from the keyboard. A filled slot settles over 200 ms on the kit's ease out: the chip's fill fades and the words take the sentence's ink. Nothing moves or resizes.

Source: https://chatcn.dev/docs/components/template-composer