# Streaming Text

An agent's answer as it streams in, with sources and follow-up questions.

An agent's answer as it streams in, with inline sources, then actions and suggested follow-ups. Host-driven: the host feeds the tokens and flips done. A caret follows the words, cite chips pop in where they land, and once done the actions fade in, the follow-ups rise in a short stagger and the sources list can be revealed. Space for the actions and follow-ups is held from the start, so finishing never moves anything.

Kind: block · Category: Agent · Status: beta · Targets: React, Vue, Svelte

## Add

```bash
npx fndn add streaming-text
```

It writes the folder `streaming-text/` into the project's ui folder (`index.ts`, `streaming-text.tsx`, `StreamingText.vue`, `StreamingText.svelte`, `streaming-text.recipe.ts`), and what it is built from: [Avatar](/components/avatar.md), [Button](/components/button.md), [Collapsible](/components/collapsible.md), [Tooltip](/components/tooltip.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { StreamingText } from "@/components/ui/streaming-text";
```

```vue
import { StreamingText } from "@/components/ui/streaming-text";
```

```svelte
import { StreamingText } from "$lib/components/ui/streaming-text";
```

## Parts

In the DOM the root carries `data-slot="streaming-text"` and each part `data-slot="streaming-text-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `streaming-text` |  | data-state=streaming\|done; aria-busy while streaming |
| `status` | `span` | `streaming-text-status` |  | role=status, sr-only; says the status label once done (the words themselves are not announced) |
| `text` | `p` | `streaming-text-text` |  |  |
| `caret` | `span` | `streaming-text-caret` |  | follows the words; keeps its box (hidden) when done |
| `cite` | `a` | `streaming-text-cite` | repeats | inline source chip: avatar + domain, on the text baseline; pops in |
| `cite-avatar` | `span` | `streaming-text-cite-avatar` |  | The source's Avatar (12px square, decorative): its image over its initials |
| `actions` | `div` | `streaming-text-actions` |  | hidden (but holding its space) while streaming; fades in when done |
| `action` | `button` | `streaming-text-action` | repeats | copy · retry · thumbs up · thumbs down: a Button (xs, square, ghost, rounded, muted) with aria-label, a tooltip of the same text, and data-action |
| `sources` | `div` | `streaming-text-sources` | optional | A collapsible (variant inline, no caret) whose root lays out as part of the actions row (display: contents): its trigger (avatar stack + label) follows the actions, its region wraps onto its own line below them. aria-expanded, aria-controls and data-state from the engine; a closed list is hidden. It opens only once done |
| `avatars` | `span` | `streaming-text-avatars` |  | first three source images, aria-hidden |
| `avatar` | `span` | `streaming-text-avatar` | repeats | An Avatar, 14px round, ringed in the page colour; the first three sources |
| `sources-label` | `span` | `streaming-text-sources-label` |  |  |
| `sources-list` | `div` | `streaming-text-sources-list` |  |  |
| `source` | `a` | `streaming-text-source` | repeats |  |
| `source-avatar` | `span` | `streaming-text-source-avatar` |  | An Avatar, 16px square, decorative |
| `source-name` | `span` | `streaming-text-source-name` |  |  |
| `source-domain` | `span` | `streaming-text-source-domain` |  |  |
| `followups` | `div` | `streaming-text-followups` |  |  |
| `followups-title` | `p` | `streaming-text-followups-title` |  |  |
| `followups-list` | `div` | `streaming-text-followups-list` |  |  |
| `followup` | `button` | `streaming-text-followup` | repeats | --fndn-row-index (set at render) staggers the entrance when done |
| `followup-text` | `span` | `streaming-text-followup-text` |  |  |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `content` | `{ text: string; cite: number (source index) \| true (first source) }[]` |  | The tokens streamed so far (host-driven). |
| `sources` | `{ name: string; domain: string; href: string; image: string }[]` |  |  |
| `followUps` | `string[]` |  |  |
| `done` | `boolean` | `false` | data-state=streaming\|done |
| `sourcesOpen` | `boolean` | `false` | Bindable (React: `defaultSourcesOpen` and `onSourcesOpenChange`; Vue: `v-model:sourcesOpen`; Svelte: `bind:sourcesOpen`). The sources list is open (it opens only once done). |
| `fill` | `boolean` | `false` | Fill the parent width (and drop the reserved height) instead of the 380px measure. |
| `sourcesLabel` | `string` | `"Sources"` |  |
| `followUpsLabel` | `string` | `"Follow-ups"` |  |
| `statusLabel` | `string` | `"Answer complete"` | Announced once done. |
| `appear` | `boolean` | `false` | Whether what is present at mount enters: the caret fades in, cites already streamed pop in, the follow-ups of an answer mounted done rise. Off by default: set it on the answer your code adds live, never on one rendered from history or with the page (a mount is not an arrival). Cites streamed after mount and the follow-ups that come with done enter either way. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `followUp` | (text, index) |
| `action` | (name: copy\|retry\|up\|down) |
| `sourcesOpenChange` | (open) |

## States

`streaming`, `done`, `sources-open`, `cite-hover`, `cite-press`, `action-press`, `followup-hover`, `followup-press`, `focus-visible`

## Keyboard and accessibility

Keyboard:

- Actions, the sources toggle and follow-ups are buttons (Enter / Space); while streaming they are hidden and out of the tab order
- Sources toggle: Enter / Space reveal the list at once; focus stays on the toggle (aria-expanded, aria-controls); the closed list is hidden
- Each action shows a tooltip with its name on hover and keyboard focus
- Cite chips and sources are links

Notes:

- aria-busy while streaming; the words are not announced one by one, the status label is announced once done.
- Every icon button has an aria-label and a tooltip with the same text; the avatars are aria-hidden next to their labels.

## Motion

- caret: fndn-fade-in over duration.base, on mount only with appear; when done it fades out over duration.fast (easing.out-strong) and is hidden, keeping its box
- caret stall: while streaming, when no word has arrived for duration.enter × 2 the caret blinks (fndn-caret-blink, 1s steps(1), a loop; the stalled variant, set by the adapter through streaming-text.logic.ts); the next word or done stops it
- cite chip: fndn-pop-in over duration.popup when it streams in (a cite present at mount pops only with appear) (fill backwards, so :active can press afterwards); presses with --fndn-hover-2 and motion.press-scale
- actions: opacity + visibility over duration.slow when done; the buttons press with the Button's scale
- follow-ups: title fndn-fade-in and rows fndn-fade-up over duration.enter when done arrives (an answer mounted done shows them at once unless appear), rows staggered by duration.stagger capped at motion.stagger-max; rows hover --fndn-hover and press --fndn-hover-2
- sources: the collapsible's reveal recipe (rows below move with it; instant from the keyboard)
- words: no animation (they arrive too often)

## Tokens

`color.ink`, `color.ink-2`, `color.ink-3`, `color.inset`, `color.hover`, `color.hover-2`, `color.surface`, `color.canvas`, `color.line`, `shadow.hairline`, `radius.pill`, `radius.chip`, `radius.card`, `text.xs`, `text.sm`, `font.mono`, `duration.fast`, `duration.base`, `duration.press`, `duration.popup`, `duration.reveal`, `duration.slow`, `duration.enter`, `duration.stagger`, `motion.stagger-max`, `motion.press-scale`, `easing.out-strong`

## Dependencies

- [Avatar](/components/avatar.md)
- [Button](/components/button.md)
- [Collapsible](/components/collapsible.md)
- [Tooltip](/components/tooltip.md)

## Examples

### Answer why an account's bookings dropped

React · `streaming.tsx`

```tsx
import { useEffect, useState, type ComponentProps } from "react";
import { StreamingText } from "@/components/ui/streaming-text";

type Props = ComponentProps<typeof StreamingText>;

const words = (sentence: string) => sentence.split(" ").map((text) => ({ text }));

/* the whole answer; a token with `cite` is the chip of that source */
const answer: Props["content"] = [
  ...words("Kestrel Logistics booked 41% fewer appointments in October than in September."),
  { text: "", cite: 0 },
  ...words("Their admin turned off SMS reminders on October 3, and no-shows rose from 6% to 19% the same week."),
  { text: "", cite: 1 },
  ...words("Two open tickets ask how to turn the reminders back on."),
  { text: "", cite: 2 },
];

const initial: Props = {
  content: [],
  sources: [
    {
      name: "Booking analytics",
      domain: "analytics.harbor.dev",
      href: "https://analytics.harbor.dev/accounts/kestrel-logistics",
      image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%232f6fec'/%3E%3Cpath d='M17 45V25h8v20h-8Zm11 0V16h8v29h-8Zm11 0V30h8v15h-8Z' fill='%23fff'/%3E%3C/svg%3E",
    },
    {
      name: "Workspace audit log",
      domain: "admin.harbor.dev",
      href: "https://admin.harbor.dev/workspaces/kestrel-logistics/audit-log",
      image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%23e56d24'/%3E%3Cpath d='M20 22h24M20 32h24M20 42h16' fill='none' stroke='%23fff' stroke-width='5' stroke-linecap='round'/%3E%3C/svg%3E",
    },
    {
      name: "Tickets #8812, #8830",
      domain: "support.harbor.dev",
      href: "https://support.harbor.dev/tickets?account=kestrel-logistics",
      image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%231f7a5f'/%3E%3Cpath d='M19 18h26a5 5 0 0 1 5 5v14a5 5 0 0 1-5 5H31l-9 7v-7h-3a5 5 0 0 1-5-5V23a5 5 0 0 1 5-5Z' fill='%23fff'/%3E%3C/svg%3E",
    },
  ],
  followUps: ["Turn SMS reminders back on for Kestrel", "Which other accounts turned reminders off"],
  done: false,
  sourcesLabel: "3 sources",
};

/* [pause in ms, tokens received so far]: the answer arrives a few words at a time */
const arrivals: [number, number][] = [
  [600, 1], [72, 3], [64, 4], [86, 6], [70, 8], [78, 9], [61, 11], [240, 12],
  [180, 13], [90, 15], [73, 17], [62, 18], [84, 21], [71, 23], [66, 24], [88, 26], [69, 28], [75, 31], [240, 32],
  [180, 33], [82, 35], [64, 37], [77, 39], [70, 41], [86, 43], [240, 44],
];

/* each step waits `after` ms, then merges its props over the current ones */
const timeline: { after: number; props: Partial<Props> }[] = [
  ...arrivals.map(([after, count]) => ({ after, props: { content: answer.slice(0, count) } })),
  { after: 480, props: { done: true } },
];

export default function Streaming() {
  const [props, setProps] = useState(initial);
  useEffect(() => {
    let i = 0, timer = 0;
    const next = () => {
      const step = timeline[i++];
      if (step) timer = window.setTimeout(() => { setProps((p) => ({ ...p, ...step.props })); next(); }, step.after);
    };
    next();
    return () => clearTimeout(timer);
  }, []);
  return <StreamingText {...props} />;
}
```

Vue · `streaming.vue`

```vue
<script setup lang="ts">
import { onBeforeUnmount, onMounted, shallowRef } from "vue";
import { StreamingText, type StreamingSource, type StreamingToken } from "@/components/ui/streaming-text";

type Props = {
  content: StreamingToken[];
  sources: StreamingSource[];
  followUps: string[];
  done: boolean;
  sourcesLabel: string;
};

const words = (sentence: string) => sentence.split(" ").map((text) => ({ text }));

/* the whole answer; a token with `cite` is the chip of that source */
const answer: StreamingToken[] = [
  ...words("Kestrel Logistics booked 41% fewer appointments in October than in September."),
  { text: "", cite: 0 },
  ...words("Their admin turned off SMS reminders on October 3, and no-shows rose from 6% to 19% the same week."),
  { text: "", cite: 1 },
  ...words("Two open tickets ask how to turn the reminders back on."),
  { text: "", cite: 2 },
];

const initial: Props = {
  content: [],
  sources: [
    {
      name: "Booking analytics",
      domain: "analytics.harbor.dev",
      href: "https://analytics.harbor.dev/accounts/kestrel-logistics",
      image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%232f6fec'/%3E%3Cpath d='M17 45V25h8v20h-8Zm11 0V16h8v29h-8Zm11 0V30h8v15h-8Z' fill='%23fff'/%3E%3C/svg%3E",
    },
    {
      name: "Workspace audit log",
      domain: "admin.harbor.dev",
      href: "https://admin.harbor.dev/workspaces/kestrel-logistics/audit-log",
      image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%23e56d24'/%3E%3Cpath d='M20 22h24M20 32h24M20 42h16' fill='none' stroke='%23fff' stroke-width='5' stroke-linecap='round'/%3E%3C/svg%3E",
    },
    {
      name: "Tickets #8812, #8830",
      domain: "support.harbor.dev",
      href: "https://support.harbor.dev/tickets?account=kestrel-logistics",
      image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%231f7a5f'/%3E%3Cpath d='M19 18h26a5 5 0 0 1 5 5v14a5 5 0 0 1-5 5H31l-9 7v-7h-3a5 5 0 0 1-5-5V23a5 5 0 0 1 5-5Z' fill='%23fff'/%3E%3C/svg%3E",
    },
  ],
  followUps: ["Turn SMS reminders back on for Kestrel", "Which other accounts turned reminders off"],
  done: false,
  sourcesLabel: "3 sources",
};

/* [pause in ms, tokens received so far]: the answer arrives a few words at a time */
const arrivals: [number, number][] = [
  [600, 1], [72, 3], [64, 4], [86, 6], [70, 8], [78, 9], [61, 11], [240, 12],
  [180, 13], [90, 15], [73, 17], [62, 18], [84, 21], [71, 23], [66, 24], [88, 26], [69, 28], [75, 31], [240, 32],
  [180, 33], [82, 35], [64, 37], [77, 39], [70, 41], [86, 43], [240, 44],
];

/* each step waits `after` ms, then merges its props over the current ones */
const timeline: { after: number; props: Partial<Props> }[] = [
  ...arrivals.map(([after, count]) => ({ after, props: { content: answer.slice(0, count) } })),
  { after: 480, props: { done: true } },
];

const props = shallowRef<Props>({ ...initial });
let timer = 0;
onMounted(() => {
  let i = 0;
  const next = () => {
    const step = timeline[i++];
    if (step) timer = window.setTimeout(() => { props.value = { ...props.value, ...step.props }; next(); }, step.after);
  };
  next();
});
onBeforeUnmount(() => clearTimeout(timer));
</script>

<template>
  <StreamingText v-bind="props" />
</template>
```

Svelte · `streaming.svelte`

```svelte
<script lang="ts">
  import { onMount, type ComponentProps } from "svelte";
  import { StreamingText } from "$lib/components/ui/streaming-text";

  type Props = ComponentProps<typeof StreamingText>;

  const words = (sentence: string) => sentence.split(" ").map((text) => ({ text }));

  /* the whole answer; a token with `cite` is the chip of that source */
  const answer: Props["content"] = [
    ...words("Kestrel Logistics booked 41% fewer appointments in October than in September."),
    { text: "", cite: 0 },
    ...words("Their admin turned off SMS reminders on October 3, and no-shows rose from 6% to 19% the same week."),
    { text: "", cite: 1 },
    ...words("Two open tickets ask how to turn the reminders back on."),
    { text: "", cite: 2 },
  ];

  const initial: Props = {
    content: [],
    sources: [
      {
        name: "Booking analytics",
        domain: "analytics.harbor.dev",
        href: "https://analytics.harbor.dev/accounts/kestrel-logistics",
        image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%232f6fec'/%3E%3Cpath d='M17 45V25h8v20h-8Zm11 0V16h8v29h-8Zm11 0V30h8v15h-8Z' fill='%23fff'/%3E%3C/svg%3E",
      },
      {
        name: "Workspace audit log",
        domain: "admin.harbor.dev",
        href: "https://admin.harbor.dev/workspaces/kestrel-logistics/audit-log",
        image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%23e56d24'/%3E%3Cpath d='M20 22h24M20 32h24M20 42h16' fill='none' stroke='%23fff' stroke-width='5' stroke-linecap='round'/%3E%3C/svg%3E",
      },
      {
        name: "Tickets #8812, #8830",
        domain: "support.harbor.dev",
        href: "https://support.harbor.dev/tickets?account=kestrel-logistics",
        image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%231f7a5f'/%3E%3Cpath d='M19 18h26a5 5 0 0 1 5 5v14a5 5 0 0 1-5 5H31l-9 7v-7h-3a5 5 0 0 1-5-5V23a5 5 0 0 1 5-5Z' fill='%23fff'/%3E%3C/svg%3E",
      },
    ],
    followUps: ["Turn SMS reminders back on for Kestrel", "Which other accounts turned reminders off"],
    done: false,
    sourcesLabel: "3 sources",
  };

  /* [pause in ms, tokens received so far]: the answer arrives a few words at a time */
  const arrivals: [number, number][] = [
    [600, 1], [72, 3], [64, 4], [86, 6], [70, 8], [78, 9], [61, 11], [240, 12],
    [180, 13], [90, 15], [73, 17], [62, 18], [84, 21], [71, 23], [66, 24], [88, 26], [69, 28], [75, 31], [240, 32],
    [180, 33], [82, 35], [64, 37], [77, 39], [70, 41], [86, 43], [240, 44],
  ];

  /* each step waits `after` ms, then merges its props over the current ones */
  const timeline: { after: number; props: Partial<Props> }[] = [
    ...arrivals.map(([after, count]) => ({ after, props: { content: answer.slice(0, count) } })),
    { after: 480, props: { done: true } },
  ];

  let props: Props = $state.raw({ ...initial });
  onMount(() => {
    let i = 0, timer = 0;
    const next = () => {
      const step = timeline[i++];
      if (step) timer = window.setTimeout(() => { props = { ...props, ...step.props }; next(); }, step.after);
    };
    next();
    return () => clearTimeout(timer);
  });
</script>

<StreamingText {...props} />
```

### Finished answer with its sources open

React · `done.tsx`

```tsx
import type { ComponentProps } from "react";
import { StreamingText } from "@/components/ui/streaming-text";

type Props = ComponentProps<typeof StreamingText>;

const words = (sentence: string) => sentence.split(" ").map((text) => ({ text }));

/* a token with `cite` is the chip of that source */
const content: Props["content"] = [
  ...words("Support logged 27 tickets about double bookings this week, up from 9 the week before."),
  { text: "", cite: 0 },
  ...words("All but two come from workspaces on calendar sync 4.12, released on Monday."),
  { text: "", cite: 1 },
  ...words("Maya Chen's team has a fix in review."),
  { text: "", cite: 2 },
];

const sources: Props["sources"] = [
  {
    name: "Support tickets",
    domain: "support.harbor.dev",
    href: "https://support.harbor.dev/tickets?tag=double-booking",
    image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%231f7a5f'/%3E%3Cpath d='M19 18h26a5 5 0 0 1 5 5v14a5 5 0 0 1-5 5H31l-9 7v-7h-3a5 5 0 0 1-5-5V23a5 5 0 0 1 5-5Z' fill='%23fff'/%3E%3C/svg%3E",
  },
  {
    name: "Release notes 4.12",
    domain: "harbor.dev",
    href: "https://harbor.dev/changelog/4-12",
    image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%23e56d24'/%3E%3Cpath d='M20 22h24M20 32h24M20 42h16' fill='none' stroke='%23fff' stroke-width='5' stroke-linecap='round'/%3E%3C/svg%3E",
  },
  {
    name: "harbor/api #1931",
    domain: "github.com",
    href: "https://github.com/harbor/api/pull/1931",
    image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%2324292f'/%3E%3Cpath d='M24 25v14M42 31c0 8-6 10-13 11' fill='none' stroke='%23fff' stroke-width='4' stroke-linecap='round'/%3E%3Ccircle cx='24' cy='20' r='5' fill='%23fff'/%3E%3Ccircle cx='24' cy='44' r='5' fill='%23fff'/%3E%3Ccircle cx='42' cy='26' r='5' fill='%23fff'/%3E%3C/svg%3E",
  },
];

const followUps: Props["followUps"] = ["Draft a reply to the affected customers", "When does the fix ship"];

export default function Done() {
  return (
    <StreamingText
      content={content}
      sources={sources}
      followUps={followUps}
      done
      defaultSourcesOpen
      sourcesLabel="3 sources"
    />
  );
}
```

Vue · `done.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { StreamingText } from "@/components/ui/streaming-text";

const words = (sentence: string) => sentence.split(" ").map((text) => ({ text }));

/* a token with `cite` is the chip of that source */
const content = [
  ...words("Support logged 27 tickets about double bookings this week, up from 9 the week before."),
  { text: "", cite: 0 },
  ...words("All but two come from workspaces on calendar sync 4.12, released on Monday."),
  { text: "", cite: 1 },
  ...words("Maya Chen's team has a fix in review."),
  { text: "", cite: 2 },
];

const sources = [
  {
    name: "Support tickets",
    domain: "support.harbor.dev",
    href: "https://support.harbor.dev/tickets?tag=double-booking",
    image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%231f7a5f'/%3E%3Cpath d='M19 18h26a5 5 0 0 1 5 5v14a5 5 0 0 1-5 5H31l-9 7v-7h-3a5 5 0 0 1-5-5V23a5 5 0 0 1 5-5Z' fill='%23fff'/%3E%3C/svg%3E",
  },
  {
    name: "Release notes 4.12",
    domain: "harbor.dev",
    href: "https://harbor.dev/changelog/4-12",
    image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%23e56d24'/%3E%3Cpath d='M20 22h24M20 32h24M20 42h16' fill='none' stroke='%23fff' stroke-width='5' stroke-linecap='round'/%3E%3C/svg%3E",
  },
  {
    name: "harbor/api #1931",
    domain: "github.com",
    href: "https://github.com/harbor/api/pull/1931",
    image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%2324292f'/%3E%3Cpath d='M24 25v14M42 31c0 8-6 10-13 11' fill='none' stroke='%23fff' stroke-width='4' stroke-linecap='round'/%3E%3Ccircle cx='24' cy='20' r='5' fill='%23fff'/%3E%3Ccircle cx='24' cy='44' r='5' fill='%23fff'/%3E%3Ccircle cx='42' cy='26' r='5' fill='%23fff'/%3E%3C/svg%3E",
  },
];

const followUps = ["Draft a reply to the affected customers", "When does the fix ship"];

const sourcesOpen = ref(true);
</script>

<template>
  <StreamingText
    :content="content"
    :sources="sources"
    :followUps="followUps"
    done
    v-model:sourcesOpen="sourcesOpen"
    sourcesLabel="3 sources"
  />
</template>
```

Svelte · `done.svelte`

```svelte
<script lang="ts">
  import { StreamingText } from "$lib/components/ui/streaming-text";

  const words = (sentence: string) => sentence.split(" ").map((text) => ({ text }));

  /* a token with `cite` is the chip of that source */
  const content = [
    ...words("Support logged 27 tickets about double bookings this week, up from 9 the week before."),
    { text: "", cite: 0 },
    ...words("All but two come from workspaces on calendar sync 4.12, released on Monday."),
    { text: "", cite: 1 },
    ...words("Maya Chen's team has a fix in review."),
    { text: "", cite: 2 },
  ];

  const sources = [
    {
      name: "Support tickets",
      domain: "support.harbor.dev",
      href: "https://support.harbor.dev/tickets?tag=double-booking",
      image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%231f7a5f'/%3E%3Cpath d='M19 18h26a5 5 0 0 1 5 5v14a5 5 0 0 1-5 5H31l-9 7v-7h-3a5 5 0 0 1-5-5V23a5 5 0 0 1 5-5Z' fill='%23fff'/%3E%3C/svg%3E",
    },
    {
      name: "Release notes 4.12",
      domain: "harbor.dev",
      href: "https://harbor.dev/changelog/4-12",
      image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%23e56d24'/%3E%3Cpath d='M20 22h24M20 32h24M20 42h16' fill='none' stroke='%23fff' stroke-width='5' stroke-linecap='round'/%3E%3C/svg%3E",
    },
    {
      name: "harbor/api #1931",
      domain: "github.com",
      href: "https://github.com/harbor/api/pull/1931",
      image: "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='16' fill='%2324292f'/%3E%3Cpath d='M24 25v14M42 31c0 8-6 10-13 11' fill='none' stroke='%23fff' stroke-width='4' stroke-linecap='round'/%3E%3Ccircle cx='24' cy='20' r='5' fill='%23fff'/%3E%3Ccircle cx='24' cy='44' r='5' fill='%23fff'/%3E%3Ccircle cx='42' cy='26' r='5' fill='%23fff'/%3E%3C/svg%3E",
    },
  ];

  const followUps = ["Draft a reply to the affected customers", "When does the fix ship"];

  let sourcesOpen = $state(true);
</script>

<StreamingText
  content={content}
  sources={sources}
  followUps={followUps}
  done
  bind:sourcesOpen={sourcesOpen}
  sourcesLabel="3 sources"
/>
```

## Recipe

`streaming-text.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* streaming-text — a streamed answer. The host feeds the words and sets done.
 * While streaming: a caret follows the words, cite chips pop in where they land, and the actions
 * and follow-ups already hold their space (hidden), so finishing never moves anything.
 * When done: the actions fade in, the follow-ups rise in a short capped stagger, and the sources
 * list can be revealed. Words themselves never animate: they arrive too often. A mount is not an
 * arrival (`docs motion`): what is there at mount (the caret, the cites already streamed,
 * the follow-ups of an answer mounted done) is simply there, unless the host sets appear on an
 * answer its code adds live; cites that stream in later and the follow-ups that come with done
 * always enter. The cite's pop-in fills backwards, not both: once in, the keyframe lets go of
 * transform so :active can press.
 *
 * Composed parts, rendered by the adapter: the actions are Buttons (xs, square, ghost, rounded,
 * muted) with tooltips; the sources toggle and list are a Collapsible (inline, no caret) whose
 * root lays out as part of the actions row (display: contents) and is restyled from here (its
 * trigger at this row's size: 24px, 12px text, chip radius); every source image is an Avatar,
 * restyled by the cite-avatar, avatar and source-avatar slots passed as its class.
 *
 * Author text takes its direction from its own first letter (plaintext).
 * The composed parts are marked data-streaming-text="action | sources |
 * cite-avatar | avatar | source-avatar"; the caret is a slot (streaming-text-caret) that other
 * blocks may render with this recipe's caret classes. */

export const streamingText = tv({
  slots: {
    /* room for a typical answer, so the block does not push the page down as words arrive */
    base: "w-full max-w-[380px] min-h-[15.5rem]",
    status: "sr-only",
    text: "m-0 text-sm leading-[1.625] text-ink wrap-anywhere [unicode-bidi:plaintext]",
    /* the caret keeps its box when done, so the last line never rewraps */
    caret: [
      "inline-block w-0.5 h-3 ms-0.5 rounded-pill bg-ink align-[-1px]",
      /* backwards, not both: once in, the caret rests on its own opacity, which done fades out */
      "animate-[fndn-fade-in_var(--fndn-duration-base)_var(--fndn-easing-out-strong)_backwards]",
    ],
    /* inline source chip: an inline-block whose one line box is the chip's height, so its text sits
     * on the paragraph's baseline (an inline-flex chip would take the avatar's bottom edge instead) */
    cite: [
      "inline-block h-[18px] me-1 px-[3px] rounded-[5px] bg-inset shadow-hairline",
      "text-ink-2 font-mono text-[10.5px] leading-[18px] no-underline whitespace-nowrap align-baseline",
      "hover:bg-hover hover:text-ink active:bg-hover-2 active:text-ink",
      "motion-colors-fast motion-transform-press active:scale-press",
      "animate-[fndn-pop-in_var(--fndn-duration-popup)_var(--fndn-easing-out-strong)_backwards]",
    ],
    /* the source's avatar (12px square) is centred in the chip's 18px line: top of the line plus 3px
     * (an avatar's own baseline is its initials' baseline, which moves with font rounding) */
    citeAvatar: "mt-[3px] me-1 align-top",
    /* hidden but present while streaming, fade in when done. No row gap: the sources region's
     * height alone moves what is below, down to 0 when it closes */
    actions: [
      "flex min-w-0 flex-wrap items-center gap-x-0.5 gap-y-0 mt-2",
      "motion-opacity-slow motion-also-[visibility_var(--fndn-duration-slow)_ease]",
    ],
    action: "",
    /* the Collapsible's root: its trigger sits after the buttons, its region wraps onto a line of
     * its own below them */
    sources: [
      "contents",
      "[&>[data-slot=collapsible-trigger]]:h-6 [&>[data-slot=collapsible-trigger]]:max-w-full [&>[data-slot=collapsible-trigger]]:gap-1.5",
      "[&>[data-slot=collapsible-trigger]]:ms-1.5 [&>[data-slot=collapsible-trigger]]:me-0 [&>[data-slot=collapsible-trigger]]:px-1 [&>[data-slot=collapsible-trigger]]:py-0",
      "[&>[data-slot=collapsible-trigger]]:rounded-chip [&>[data-slot=collapsible-trigger]]:text-xs [&>[data-slot=collapsible-trigger]]:font-normal",
      "[&>[data-slot=collapsible-trigger]>[data-slot=collapsible-label]]:inline-flex [&>[data-slot=collapsible-trigger]>[data-slot=collapsible-label]]:items-center [&>[data-slot=collapsible-trigger]>[data-slot=collapsible-label]]:gap-1.5",
      "[&>[data-slot=collapsible-content]]:flex-[0_0_100%] [&>[data-slot=collapsible-content]]:min-w-0",
    ],
    avatars: "inline-flex shrink-0",
    /* a ring in the page colour keeps the overlapping avatars apart */
    avatar: "not-first:-ms-1 shadow-[0_0_0_1.5px_var(--fndn-canvas)]",
    sourcesLabel: "min-w-0 overflow-hidden text-ellipsis whitespace-nowrap text-ink-2 tabular-nums",
    sourcesList: "flex flex-col mt-1.5 p-1 rounded-card bg-inset shadow-hairline",
    source: [
      "flex min-w-0 items-center gap-2 px-1.5 py-1 rounded-chip text-ink-2 text-xs leading-4 no-underline",
      "hover:bg-hover hover:text-ink active:bg-hover-2 active:text-ink motion-colors-fast",
    ],
    sourceAvatar: "",
    sourceName: "min-w-0 overflow-hidden text-ellipsis whitespace-nowrap [unicode-bidi:plaintext]",
    sourceDomain: "shrink-0 max-w-[50%] ms-auto overflow-hidden text-ellipsis whitespace-nowrap font-mono text-[10.5px] text-ink-2 [unicode-bidi:plaintext]",
    /* one entrance when done: the title with the first row, rows in a capped stagger
     * (--fndn-row-index, set at render) */
    followups: "mt-2.5",
    followupsTitle: "m-0 text-xs font-medium text-ink-2",
    followupsList: "flex flex-col mt-0.5",
    followup: [
      /* the hairline is its own straight 1px line under the row (6px in, on the text), not the bottom
       * border of a rounded box, which would curl at both ends; the radius belongs to the hover fill */
      "relative flex min-w-0 items-center gap-2 -mx-1.5 p-1.5 rounded-chip",
      "after:content-[''] after:absolute after:inset-x-1.5 after:bottom-0 after:h-px after:bg-line after:pointer-events-none last:after:hidden",
      "text-ink text-dense leading-4 text-start hover:bg-hover active:bg-hover-2 motion-colors-fast",
      "[&>[data-slot=icon]]:size-[11px] [&>[data-slot=icon]]:shrink-0 [&>[data-slot=icon]]:text-[11px] [&>[data-slot=icon]]:text-ink-3",
    ],
    followupText: "min-w-0 wrap-anywhere [unicode-bidi:plaintext]",
  },
  variants: {
    /* streaming: the caret blinks in after the words; the actions and follow-ups hold their space,
     * unseen. done: the caret keeps its box, unseen; the actions fade in, the follow-ups title fades
     * in and its rows rise in a capped stagger (--fndn-row-index, set at render) */
    done: {
      false: {
        actions: "invisible opacity-0",
        followups: "invisible opacity-0",
      },
      true: {
        /* the caret leaves on the beat the actions arrive: a short fade (exit, duration.fast), then hidden */
        caret: "opacity-0 invisible animate-none motion-opacity-fast-out motion-also-[visibility_var(--fndn-duration-fast)]",
        followupsTitle: "animate-[fndn-fade-in_var(--fndn-duration-enter)_var(--fndn-easing-out-strong)_both]",
        followup: "animate-[fndn-fade-up_var(--fndn-duration-enter)_var(--fndn-easing-out-strong)_calc(min(var(--fndn-row-index,0),var(--fndn-motion-stagger-max))*var(--fndn-duration-stagger))_both]",
      },
    },
    /* appear=false (the default): the parts present at mount are simply there (set per part by
     * the adapter); after done, so it wins over done's entrances, and before stalled, whose blink
     * must still run */
    appear: { true: {}, false: { caret: "animate-none", cite: "animate-none", followupsTitle: "animate-none", followup: "animate-none" } },
    /* stalled (set by the adapter through streaming-text.logic.ts's stallTimer while streaming): no word
     * for duration.enter × 2, the caret blinks (a loop, so a literal 1s) so the stream does not look
     * frozen; the next word drops it. The fade-in keeps its place in the list, so it does not replay. */
    stalled: { true: { caret: "animate-[fndn-fade-in_var(--fndn-duration-base)_var(--fndn-easing-out-strong)_backwards,fndn-caret-blink_1s_steps(1)_infinite]" } },
    /* fill: as wide as its container, no reserved height */
    fill: { true: { base: "max-w-none min-h-0" } },
  },
  defaultVariants: { done: false },
});

export type StreamingTextRecipeProps = VariantProps<typeof streamingText>;
```
