# Text thread

A phone text conversation where the other side is an assistant: runs of bubbles with tails, a typing bubble that grows into the reply, reactions, link cards, receipts and a script that plays the thread.

Example: Texting an assistant. A dentist appointment moved by text: receipts, typing that grows into the reply, a calendar card and a reaction. Loops. Preview: https://chatcn.dev/preview/text-thread-demo

## Installation

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

## Usage

```tsx
import { PlainScreen } from "@/components/ui/chatcn/plain-screen"
import { TextThread, useThreadScript } from "@/components/ui/chatcn/text-thread"

const story = [
  { type: "out", id: "ask", text: "Can you move my dentist to Thursday?" },
  { type: "receipt", text: "Read 9:41" },
  { type: "typing", ms: 1400 },
  { type: "in", id: "slots", text: "They have 2:15 or 4:40 on Thursday. Which works?" },
  { type: "wait", ms: 1200 },
  { type: "react", to: "slots", icon: "thumbs-up" },
] as const

function Demo() {
  const s = useThreadScript(story, { loop: true })
  return (
    <PlainScreen ref={s.ref} width={360}>
      <TextThread
        contact={{ name: "Juno", initials: "J" }}
        stamp="Today 9:41"
        messages={s.messages}
        typing={s.typing}
        clearing={s.clearing}
      />
    </PlainScreen>
  )
}
```

The thread fills its parent and reads the safe areas from `--chatcn-safe-*`, so inside `PlainScreen` the header clears the status bar and the composer clears the home bar. Messages from one sender within a run sit 2 px apart and runs 10 px apart. The corners where bubbles meet tighten to 6 px on the sender's side, and only the last bubble of a run has a tail. The tail is drawn under the bubble's corner in the same fill, so the join has no seam in any tint or theme. A wrapped message is as wide as its longest line. While the reader is at the bottom, the newest message stays in view, also when the thread itself gets shorter, such as when a keyboard rises under it.

New bubbles enter from their tail corner. Set `typing` and, after 300 ms, an incoming bubble holds three still dots with the kit's text shimmer (no bouncing). When the reply arrives it grows out of that bubble in place: the shape opens from the typing size to the message while the dots fade out, so nothing in the thread jumps. Reactions pop onto the top corner on a critically damped spring. The receipt sits under your latest message and moves to the next one you send.

Each message can carry `card`: a 240 px bubble with a 120 px media slot (any node, such as a calendar slice), a title and a domain; with `href` the card is a link. The list is a `log` named for the contact, each bubble says who sent it to screen readers, and the typing bubble reads as the contact typing. Right to left pages swap the sides and mirror the tails. Under reduced motion bubbles fade in and the dots hold still.

## Examples

### States

Runs and tails, typing, reactions, a link card, a verified business header, the blue and green tints, a live composer, and the thread on its own at 320 px.

Example: Text thread states. Runs and tails, typing, reactions, a link card, a verified header, blue and green, a live composer and standalone at 320 px. Preview: https://chatcn.dev/preview/text-thread-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `contact` | `{ name, initials, verified? }` | The header and the log's name |
| `messages` | `{ id, from, text?, card?, reaction?, receipt? }[]` | `from` is `in` or `out` |
| `typing` | `boolean` | Shows after 300 ms |
| `tint` | `neutral | blue | green` | Outgoing bubbles; `neutral` is your primary |
| `composer`, `onSend` | `static | live | none` | Live sends on Enter; Shift+Enter is a new line |
| `placeholder` | `string` | Default Text message… |
| `stamp` | `string` | The day stamp, such as Today 9:41 |
| `action`, `onBack` | `ReactNode`, `() => void` | Header icon; the back chevron becomes a button |
| `clearing` | `boolean` | Fades the messages out before a loop restarts |

`useThreadScript(events, { loop, paused })` takes `in`, `out`, `typing` (with `ms`), `react`, `receipt` and `wait` events and returns `messages`, `typing`, `clearing`, `restart` and the `ref` that pauses it off screen. Messages, reactions and receipts take no time of their own; put a `wait` between them. `threadAt(events, n)` returns the thread after the first `n` events, for a still frame.

Source: https://chatcn.dev/docs/components/text-thread