Streaming text
Markdown that grows while a reply streams. Raw HTML stays text, and links keep only safe schemes.
Example
<focsd-streaming-text id="text-demo" streaming></focsd-streaming-text>
<script type="module">document.getElementById('text-demo').text = 'Markdown is **built as it arrives**:\n\n- lists and `inline code`\n- [links](https://focsd.dev) that are safe to follow\n- <b>raw HTML</b> shown as text\n\n```js\nawait chat.send("Hello");\n```';</script>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/ai';</script>
<focsd-streaming-text streaming></focsd-streaming-text>import { useEffect, useRef } from 'react';import '@focsd-dev/ai';
export function Example() { const ref = useRef(null); return <focsd-streaming-text ref={ref} streaming></focsd-streaming-text>;}<script setup>import '@focsd-dev/ai';</script>
<template> <focsd-streaming-text streaming></focsd-streaming-text></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/ai';
// template<focsd-streaming-text streaming></focsd-streaming-text><script> import '@focsd-dev/ai';</script>
<focsd-streaming-text streaming></focsd-streaming-text>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
text | text | string | — |
plain | plain | boolean | false |
streaming | streaming | boolean | false |
CSS parts
Style the inside of the component with ::part(), for example focsd-streaming-text::part(text).
| Part | Description |
|---|---|
text | The text. |
caret | The caret shown while streaming (hidden from assistive technology). |
code | Each code block (`pre`). |
code-copy | The copy button of each code block. |
Accessibility
- The text is built with DOM calls only (never
innerHTML), so it runs under a strict Content Security Policy with Trusted Types. Raw HTML, images and tables are shown as text. - Links keep
https:,http:,mailto:and relative URLs only (javascript:,data:and the rest become text) and getrel="noopener noreferrer nofollow". - Only the last block is rebuilt on each update, so a selection or focus inside finished blocks survives while text streams.
- The caret is hidden from assistive technology and stops pulsing under reduced motion. Each code block shows its language and has its own copy button. A code block that scrolls sideways is a named region in the tab order, so it scrolls by keyboard; one that fits adds no tab stop.
- Headings inside a reply are rendered from level 3 down, never as
h1orh2, so they sit under the page’s own headings. - On the server the text renders as plain text with its line breaks, so there is content before the script loads.
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.