# Formatted text

What people and agents write, drawn with a small, safe set of Markdown marks: strong, emphasis, struck, code, quotes, lists and links.

Live variants: https://design.arcadia.is/components/formatted-text

```tsx
import { FormattedText, parseFormatted, formattedPreview } from "@arcadia/ui";
```

```tsx
import { MessageGroup, Avatar, Bubble, FormattedText } from "@arcadia/ui";

<div {...sx("cmp-thread", FormattedTextStyles.thread)}>
  <MessageGroup side="end">
    <Bubble side="end"><FormattedText source="Can you find out why the **build** fails on the new runner?" /></Bubble>
  </MessageGroup>
  <MessageGroup author={<><Avatar kind="agent" avatar={FormattedTextHAZEL} name="Hazel" size={16} aria-hidden />Hazel</>}>
    <Bubble><FormattedText source={REPLY} ranges={pills(REPLY)} /></Bubble>
  </MessageGroup>
</div>
```

## Accessibility

The marks are the semantic elements (`strong`, `em`, `del`, `code`, `pre`, `blockquote`, `ul`, `ol` and `a`), so a screen reader names a list, its items and a quote. A heading in the source is strong text, never a heading of the page. A list's markers are text in the page, never generated content: an item's number is read with it, and a bullet, which only decorates, is not read.

A bare link's name is its whole address, the same as its title; what it shows is hidden from assistive technology so nothing is read twice. A labelled link's name is its words and its title the whole address, and the host after it is read as the text it is. Every link opens in a new tab with no opener and sends no referrer. A mention inside a link's words is its text, so a link holds nothing that acts. A code block that scrolls sideways takes focus, a group named Code, so its arrow keys scroll it and its ring stands inside its edge. Its copy control comes after the code in the Tab order, is named Copy code and says Copied once the clipboard has the text.

Each paragraph, list and quote takes its direction from its own text, and a link and a code block are always left to right, so an address keeps its order in a right-to-left message. A range is the product's node: a `MentionPill` brings its own name and, with `onClick`, its button.

## API reference

### FormattedText

- `source` (`string`, required): The text as written: plain text, read for a small set of Markdown marks. Nothing else decides its look.
- `ranges` (`readonly FormattedTextRange[]`): Spans of the source the product has already understood (`start` and `length` in UTF-16 units), each drawn as its `node`. A range is never read for marks, though a mark may run across it; inside code it is text. One that overlaps another, runs outside the source or crosses a line is dropped.
- `end` (`ReactNode`): A small mark that ends the last line (a read mark, a time): after the last word, `--space-1` on, in the text's own style at its regular weight, so an icon in it takes the line's pair. It goes to a new line only with that word, never alone, and a copy leaves it out. After a closing block of code it stands under the block, at its end.
- `codeCopy` (`boolean`, default `true`): A copy control in each code block's top end corner, under the pointer, on keyboard focus and always on touch. It copies the block's text as written. false where copying the code makes no sense.
- `compactCode` (`boolean`, default `false`)
- `rootStyle` (`RootStyle`): Typed StyleX override: where the text sits (layout only).

Rest props go to `<div>` props.
