# Response

An AI agent's answer, drawn from its Markdown in the kit's native element styles: headings, paragraphs, emphasis, lists, quotes, rules, links, tables (each in a focusable group named by its columns, scrolling when wider than the reply, figures aligned to the end) and code (each block a CodeBlock named by its language and its first line, coloured where CodeBlock knows the language, plain otherwise; a repeated name in one reply gets " 2"). content takes parseMarkdown(text) from "turtlekit/ai" (its own entry, so an app without a chat never carries the formatter; plain data, so a server can read the reply), read again with the whole text as it arrives; only the blocks that changed draw again, and each block lands in its place as it arrives. streaming while it arrives: a line that could still become something else (a list marker, a fence, a table's header or row) waits for its end, emphasis, code and links left open draw closed where the text stops, an unfinished code block offers no copy yet, and the reply is aria-busy. Safe: HTML in the text shows as text, links lead only to web pages (opening in a new tab, said to screen readers), email, phone numbers and places on the page, and images become links named by their alt text. headingLevel (default 3) is the level of the reply's first heading; deeper ones step down from it. The reply reads in its own language's direction (dir="auto"). html takes HTML another parser made, sanitised first, used as given. codeLabel, tableLabel and newTabLabel are the words for other languages. Never widens a chat row: tables are contained and long tokens break. Naming each reply and announcing a finished one are the conversation's job.

Page: https://turtlekit.dev/components/response

## Import

```tsx
import { Response } from "turtlekit";
```

## Props

### Response

| Prop | Type | Default | What it does |
| --- | --- | --- | --- |
| `content` | `ResponseContent` |  | The reply, read by `parseMarkdown` ("turtlekit/ai"): `content={parseMarkdown(text)}`, read again with the whole text as more arrives. |
| `html` | `string` |  | The reply as HTML another Markdown parser made, sanitised first: Response renders it as given, unchecked. |
| `streaming` | `boolean` | `false` | True while the reply is still arriving, false once it ends or stops. |
| `headingLevel` | `2 \| 3 \| 4 \| 5 \| 6` | `3` | The level the reply's first heading takes on the page, whatever its "#"s; deeper ones step down from it, to h6. |
| `codeLabel` | `(code: { language: string; firstLine: string }) => string` | `defaultCodeLabel` | A code block's name, for its region and its copy button, from its language (the fence's word, or "") and its first line: default "Shell code, npm install acme"; a repeat in the same reply adds " 2". |
| `tableLabel` | `(columns: string[]) => string` | `defaultTableLabel` | A table's name, for the group it scrolls in when wider than the reply, from its column headings: default "Table: Plan, Seats, Price"; a repeat in the same reply adds " 2". |
| `newTabLabel` | `string` | `(opens in a new tab)` | What a link to another site adds for screen readers. |

## Shapes its props take

### ResponseContent

| Prop | Type | Default | What it does |
| --- | --- | --- | --- |
| `format` (required) | `"markdown"` |  | What it was read from. |

## Rules it keeps

- `a11y.accessible-name`
- `type.heading-order`
- `readability.link-text`

## Examples

Every example on the page, as one file:

```tsx
import { useEffect, useState } from "react";
import { Button, Response, Stack } from "turtlekit";
import { parseMarkdown } from "turtlekit/ai";

const REFUND = `## Refunding a paid invoice

You can refund **INV-0048** in full or in part from its page. The money returns to the card Brightwater Coffee paid with, in **5 to 10 working days**.

1. Open the invoice and choose **Refund**.
2. Enter the amount: up to £1,240.00, the sum paid.
3. Add a note for the client, then confirm.

| Refund | Client sees | Your books |
| :--- | :--- | ---: |
| Full | A credit note for the whole sum | −£1,240.00 |
| Part | A credit note for the amount | −£300.00 |

> A refund can't be undone. To charge again, send a new invoice.`;

/**
 * A reply as it streams in: the text arrives a few characters at a time, and the reply draws only what is settled. The
 * list's next marker, the table's header and each row wait for their line's end; bold closes where the text stops.
 * Nothing already shown moves.
 */
export function AnswerStreaming() {
  const [shown, setShown] = useState(0);
  const streaming = shown < REFUND.length;
  useEffect(() => {
    if (!streaming) return;
    // Four characters every 30ms, about 130 a second: the pace of a quick reply.
    const timer = window.setTimeout(() => setShown((n) => Math.min(REFUND.length, n + 4)), 30);
    return () => window.clearTimeout(timer);
  }, [shown, streaming]);
  return (
    <Stack gap="stack">
      <Response content={parseMarkdown(REFUND.slice(0, shown))} streaming={streaming} />
      <div>
        <Button
          variant="soft"
          unavailable={streaming ? "Wait for the reply to finish" : undefined}
          onClick={() => setShown(0)}
        >
          Play the reply again
        </Button>
      </div>
    </Stack>
  );
}

const WEBHOOK = `To get paid invoices into your own system, add a webhook. Acme sends it the moment an invoice is paid.

\`\`\`ts
import { createServer } from "node:http";
import { readBody, verify } from "./http";

createServer(async (request, response) => {
  const body = await readBody(request);
  if (!verify(body, request.headers["acme-signature"])) {
    response.writeHead(401).end();
    return;
  }
  const event = JSON.parse(body);
  if (event.type === "invoice.paid") await markPaid(event.invoice.id, event.invoice.amount_paid);
  response.writeHead(200).end();
}).listen(8787);
\`\`\`

Then register its address. The secret it prints signs every call:

\`\`\`sh
acme webhooks add https://books.example.com/hooks/acme --events invoice.paid
\`\`\`

Answer within **10 seconds** with a \`200\`, or Acme tries again: after 1 minute, 10 minutes, then hourly for a day.`;

/** A long code answer: each block a CodeBlock named by its language, its long lines scrolling inside it. */
export function AnswerWithCode() {
  return <Response content={parseMarkdown(WEBHOOK)} />;
}

const PLANS = `Here are the three plans side by side, billed monthly per seat:

| Plan | Seats | Per seat | Invoices a month | Support |
| :--- | ---: | ---: | ---: | :--- |
| Starter | 1 to 3 | £6 | 50 | Email |
| Team | 4 to 25 | £9 | 500 | Email and chat |
| Business | 26 and up | £14 | No limit | A named manager |

For **12 people** sending about 200 invoices a month, Team fits: **£108 a month**.`;

/** A table in a reply: a region of its own that scrolls on a narrow screen, its figures aligned to the end. */
export function AnswerWithTable() {
  return <Response content={parseMarkdown(PLANS)} />;
}

/** HTML another Markdown parser made (and sanitised): drawn in the same native styles. */
export function OtherParserHtml() {
  return (
    <Response html="<p>Your next invoice, <strong>INV-0049</strong>, goes to Harbour &amp; Finch on <em>1 December</em>.</p>" />
  );
}
```
