# Edit and resend

> Fix a sent message where it sits. The bubble grows into an editor, then folds back and branches the reply.

People rephrase constantly, and scrolling down to retype a question breaks their flow. Hover a sent message for Copy and Edit. Editing grows the bubble into a field right where it sits, widening from its right edge to fit, with the text ready to change. Enter sends and folds it back; Escape cancels. After a resend the pager slides open beside Copy, and "2 / 2" steps between branches: the counter rolls, the message slides in from the side you stepped toward and the bubble eases to fit it, so the earlier question and its answer are never lost. Copy blurs into a check that draws itself.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/message-edit.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/message-edit
- Registry item (source): https://hairlineui.com/r/message-edit.json

## Usage

```tsx
import { MessageEdit } from "@/components/message-edit";

<MessageEdit text={message.text} onSubmit={(text) => resend(message.id, text)} disabled={busy} />
```

## With the AI SDK

```tsx
"use client";
import { useChat } from "@ai-sdk/react";
import { MessageEdit } from "@/components/message-edit";

export function UserMessages() {
  const { messages, sendMessage, status } = useChat();
  const busy = status === "submitted" || status === "streaming";

  return messages
    .filter((m) => m.role === "user")
    .map((m) => {
      const text = m.parts.map((p) => (p.type === "text" ? p.text : "")).join("");
      return (
        <MessageEdit
          key={m.id}
          text={text}
          disabled={busy}
          // Replaces this message, drops everything after it and asks again.
          onSubmit={(next) => sendMessage({ text: next, messageId: m.id })}
        />
      );
    });
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `text` | `string` | – | The message as sent; the editor starts from it. |
| `onSubmit` | `(text: string) => void` | – | Called with the edited text. Replace the message and regenerate from it. |
| `disabled / disabledReason` | `boolean / string` | `false / "Wait for the answer to finish"` | Pause editing, e.g. while an answer streams. |
| `versions` | `{ index; count; onIndexChange }` | – | Branches of this message, shown as "2 / 3" when there is more than one. |
| `children` | `ReactNode` | – | How the sent message renders (mentions, attachments); defaults to the text. |

## States

- **sent**: The message bubble. Copy and Edit appear on hover or focus (always on touch screens). Copy shrinks away through a 3px blur as a check draws itself in 380ms.
- **editing**: The bubble grows into a field in place, caret at the end. Send stays disabled until the text changes.
- **versions**: The pager slides open (420ms) once there is a second branch. Each step rolls the counter, sends the old message out 16px toward where you came from through a 6px blur, brings the new one in from the other side and eases the bubble to its size.
- **disabled**: While an answer streams, Edit is dimmed and says why on hover.

## What it handles

People rephrase constantly, and scrolling down to retype a question breaks their flow. This lets them fix a sent message where it sits, without losing the earlier answer.

- **Edit where it sits**: Hover a message for Copy and Edit. The bubble grows into a field in place with the caret at the end.
- **Resend and branch**: Enter sends the edit through onSubmit and folds the bubble back. Escape cancels.
- **Versions are kept**: A pager shows “2 / 3” and steps between branches, so the earlier question and its answer are never lost.
- **Copy with a check**: The icon blurs into a drawn check, and “Copied” is announced beside the button.
- **Locked while answering**: While an answer streams, Edit is dimmed and says why on hover.
- **Keyboard and IME safe**: Enter sends, Shift+Enter adds a line, and IME composition never sends early. Focus returns to the Edit button.

## Replaces

an edit mode toggle, an inline editor, a version pager, copy button feedback, an edit-while-streaming guard

## Where it fits

- **AI chat apps**: Edit a question and resend it, keeping the earlier answer.
- **Team chat and support**: Fix a sent message where it sits.
- **Prompt playgrounds**: Iterate on a prompt and step back through earlier versions.
- **Agent workspaces**: Reword an instruction to the agent without starting over.

## Notes

- Enter sends and Shift+Enter adds a line, like the composer; Escape cancels. IME composition never sends early.
- Focus moves into the field on edit and back to the Edit button after sending or cancelling.
- The version counter is announced politely; the pager arrows are real buttons, disabled at either end.
- Copy announces "Copied" from a status region beside the buttons, so the button keeps its name.
- The morphs use the Web Animations API and CSS transitions, no animation library. With reduced motion, versions and edits change in place.

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