# Prompt composer

> The input that starts it all: grows as you type, takes files, and turns Send into Stop while answering.

A composer that takes useChat’s status as is. Send blurs into a spinner while it waits for the first token, then into Stop while the answer streams. It grows with the text, sends on Enter (Shift+Enter for a new line, safe for IME input), and takes files from the paperclip, paste or drag and drop. Slots hold attachment chips, which fold open above the field as the first one lands, and a toolbar. Near maxLength a counter opens in and rolls as you type, and it can be disabled with a reason that folds open above it.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/prompt-composer.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `number-roll`
- Docs and live preview: https://hairlineui.com/ui/prompt-composer
- Registry item (source): https://hairlineui.com/r/prompt-composer.json

## Usage

```tsx
import { PromptComposer } from "@/components/prompt-composer";

<PromptComposer
  status={status}
  onSubmit={(text) => send(text)}
  onStop={stop}
  onFilesSelected={(files) => addFiles(files)}
  toolbar={<ModeSwitcher modes={modes} />}
  maxLength={4000}
/>
```

## With the AI SDK

```tsx
import { useChat } from "@ai-sdk/react";
import { useState } from "react";
import { PromptComposer } from "@/components/prompt-composer";
import { AttachmentChip } from "@/components/attachment-chip";

function Composer() {
  const { sendMessage, status, stop } = useChat();
  const [files, setFiles] = useState<File[]>([]);

  const send = (text: string) => {
    const list = new DataTransfer();
    files.forEach((file) => list.items.add(file));
    sendMessage({ text, files: list.files });
    setFiles([]);
  };

  return (
    <PromptComposer
      status={status}
      onSubmit={send}
      onStop={stop}
      allowEmpty={files.length > 0}
      onFilesSelected={(added) => setFiles((prev) => [...prev, ...added])}
      attachments={
        files.length > 0 &&
        files.map((file, i) => (
          <AttachmentChip
            key={file.name + i}
            name={file.name}
            size={file.size}
            type={file.type}
            onRemove={() => setFiles((prev) => prev.filter((_, j) => j !== i))}
          />
        ))
      }
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `onSubmit / onStop` | `(text) => void / () => void` | – | Send the prompt; stop the answer while streaming. |
| `status` | `"ready" \| "submitted" \| "streaming" \| "error"` | `"ready"` | useChat’s status, passed straight through. |
| `value / defaultValue / onValueChange` | `string` | – | Controlled or uncontrolled text. Uncontrolled clears itself after sending. |
| `placeholder` | `string` | `"Ask anything"` | Also used as the input’s accessible label. |
| `disabled / disabledReason` | `boolean / ReactNode` | – | Lock the input and say why. |
| `onFilesSelected / accept` | `(files: File[]) => void / string` | – | Enables the paperclip, paste and drag and drop. |
| `attachments / toolbar` | `ReactNode` | – | Chips above the text; controls at the bottom left. |
| `maxLength / allowEmpty` | `number / boolean` | – | A counter appears near the limit; allow sending files without text. |

## States

- **ready**: Type; Send is enabled once there’s text (or files, with allowEmpty).
- **submitted**: Sent and waiting for the first token: the arrow shrinks away through a 3px blur as a spinner grows in.
- **streaming**: The spinner blurs into Stop, which calls onStop.
- **error**: Back to ready so the user can resend.
- **disabled**: Input locked, with your reason folding open above it (380ms).
- **dragging**: A dashed border and "Drop files to attach" fading in while files are dragged over.
- **near the limit**: Past 80% of maxLength the counter opens in and rolls with each keystroke; it turns red once you’re over.

## What it handles

The input is where every AI conversation starts. This takes useChat’s status as it is, and handles the growing text, files, sending, stopping and limits.

- **It grows as you type**: The field grows up to 200px and then scrolls.
- **Enter sends**: Shift+Enter adds a line, and IME input in Chinese, Japanese and Korean never sends early.
- **Send, waiting, Stop**: Send blurs into a spinner while waiting for the first token, then into Stop while the answer streams.
- **Files three ways**: The paperclip, paste and drag and drop all attach files. Chips fold open above the field.
- **A counter near the limit**: Past 80% of maxLength a counter opens in and rolls as you type. It turns red over the limit.
- **Disabled with a reason**: Lock the input and the reason folds open above it.

## Replaces

a growing textarea, a Send and Stop button, paste and drop for files, a character counter, IME-safe Enter handling, a disabled reason

## Where it fits

- **AI chat apps**: The main input of a chat built on the AI SDK.
- **In-product assistants**: A help or copilot box inside a SaaS app.
- **AI tools that take files**: Let people attach documents or images to a prompt.
- **Free tiers and limits**: Lock the input and say why when a limit has been reached.

## Notes

- Enter sends only when not composing with an IME, so Chinese, Japanese and Korean input works.
- The send button’s label changes with its job: Send, Sending or Stop.
- The text grows to 200px, then scrolls.
- Paste with files attaches them; paste with text still pastes the text.
- Installs Number roll. With reduced motion, icons, folds and the counter change in place.

License: MIT (https://hairlineui.com/license#components)
