# Response versions

> Step between regenerated answers with "2 / 3", and regenerate without losing the old ones.

Regenerating shouldn’t throw away an answer you half liked. Wrap the answer and this adds a small pager under it. Each step sends the answer you leave out one way through a blur while the next comes in from the other side, the height eases between them and "2 / 3" rolls. Regenerate blurs into a spinner while the new version is written, and the count rolls up when it lands.

## Install

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

## Usage

```tsx
import { ResponseVersions } from "@/components/response-versions";

<ResponseVersions count={versions.length} index={index} onIndexChange={setIndex} onRegenerate={regenerate} regenerating={busy}>
  <Markdown>{versions[index]}</Markdown>
</ResponseVersions>
```

## With the AI SDK

```tsx
import { useChat } from "@ai-sdk/react";
import { useState } from "react";
import type { UIMessage } from "ai";
import { ResponseVersions } from "@/components/response-versions";

// regenerate() replaces the last answer, so keep the earlier ones yourself.
// Render with key={the user message's id} so versions start fresh each turn.
function LastAnswer() {
  const { messages, status, regenerate } = useChat();
  const [older, setOlder] = useState<UIMessage[]>([]);
  const [index, setIndex] = useState<number | null>(null);

  const last = messages.at(-1);
  const versions = last?.role === "assistant" ? [...older, last] : older;
  if (!versions.length) return null;
  const shown = Math.min(index ?? versions.length - 1, versions.length - 1);

  return (
    <ResponseVersions
      count={versions.length}
      index={shown}
      onIndexChange={setIndex}
      regenerating={status === "submitted" || status === "streaming"}
      onRegenerate={() => {
        if (last?.role === "assistant") setOlder((o) => [...o, last]);
        setIndex(null);
        regenerate();
      }}
    >
      {versions[shown].parts.map((part, i) => (part.type === "text" ? <p key={i}>{part.text}</p> : null))}
    </ResponseVersions>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `count / index` | `number` | – | How many versions, and which one is shown (zero-based). |
| `onIndexChange` | `(index: number) => void` | – | Called by the arrows. |
| `onRegenerate / regenerating` | `() => void / boolean` | – | Shows Regenerate, and a spinner while a version is written. |
| `children` | `ReactNode` | – | The content of the version shown. |

## States

- **single**: One version: the arrows are disabled and only Regenerate is live.
- **browsing**: The old answer leaves 24px toward where you came from (260ms) as the new one arrives from the side you stepped toward (420ms); the height eases between them and the counter rolls.
- **regenerating**: The arrow blurs into a spinner and Regenerate is disabled until the new version arrives; then "3 / 3" rolls up.

## What it handles

Regenerating should not throw away an answer someone half liked. This keeps every version and lets people step back and forth between them.

- **Step between versions**: Arrows move between answers, and “2 / 3” rolls with each step.
- **The old answer leaves the way you went**: It slides out one way through a blur while the next arrives from the other side, and the height eases between them.
- **Regenerate without losing anything**: Regenerate blurs into a spinner while the new version is written, and the count rolls up when it lands.
- **It wraps any content**: Text, code blocks, images or anything else you render as the answer.
- **Safe at the ends**: The arrows are disabled at either end. With one version, only Regenerate is live.
- **Announced politely**: Screen readers hear “Version 2 / 3” as you step.

## Replaces

version state, a pager, a Regenerate button, a content swap animation, a loading state

## Where it fits

- **AI chat apps**: Regenerate an answer and flip between the versions.
- **Writing assistants**: Try a draft again without losing the one you started with.
- **Marketing copy tools**: Compare several versions of a headline or a description.
- **Support reply drafting**: Regenerate a suggested reply and keep the earlier ones.

## Notes

- The counter is announced politely as "Version 2 / 3".
- Arrows are real disabled buttons at either end, so they’re skipped by Tab.
- Installs Number roll. With reduced motion the content simply swaps and the counter changes in place.

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