Chat thread
A conversation as a feed of messages that follows new text only while the reader is at the bottom.
Example
<focsd-chat-thread id="thread-demo" label="Conversation"></focsd-chat-thread>
<script type="module">const thread = document.getElementById('thread-demo');thread.style.setProperty('--focsd-chat-thread-height', '18rem');thread.messages = [ { id: 'u1', role: 'user', status: 'done', createdAt: Date.now() - 60000, parts: [{ type: 'text', text: 'How is a reply announced?' }] }, { id: 'a1', role: 'assistant', status: 'done', createdAt: Date.now(), parts: [{ type: 'text', text: 'Once, when it is **complete**. Never token by token.' }] },];</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-chat-thread label="Conversation"></focsd-chat-thread>import { useEffect, useRef } from 'react';import '@focsd-dev/ai';
export function Example() { const ref = useRef(null); return <focsd-chat-thread ref={ref} label="Conversation"></focsd-chat-thread>;}<script setup>import '@focsd-dev/ai';</script>
<template> <focsd-chat-thread label="Conversation"></focsd-chat-thread></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/ai';
// template<focsd-chat-thread label="Conversation"></focsd-chat-thread><script> import '@focsd-dev/ai';</script>
<focsd-chat-thread label="Conversation"></focsd-chat-thread>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
label | label | string | — |
CSS parts
Style the inside of the component with ::part(), for example focsd-chat-thread::part(feed).
| Part | Description |
|---|---|
feed | The feed (the scroll container). |
jump | The "Jump to latest" button. |
Accessibility
- The thread is
role="feed"named bylabel(default "Conversation"; until the first message it is a named group, because a feed must hold articles); each message is anarticlewitharia-posinsetandaria-setsize, and the one that is streaming hasaria-busy="true". - There is no live region on the feed. When a reply finishes, the thread announces "Assistant replied:" and its text once, politely, cut at the last word before 300 characters. A stopped reply says "Response stopped"; a failed one says the error, assertively. History set all at once is silent.
- It follows new content only while the reader is at the bottom. Scrolled up, a "Jump to latest" button appears; it scrolls down and moves focus to the latest message.
- The feed is one tab stop (the last focused message, at first the latest). Page Down and Page Up move between messages; Ctrl+Home and Ctrl+End leave the feed, as the APG feed pattern describes.
- Give it a chat controller with
thread.chat = createChat({ transport })and it shows and follows the controller; retry callschat.retry(id)unless a listener cancelsfocsd-retry.
Keyboard
| Key | Action |
|---|---|
| Page Down | Focus the next message |
| Page Up | Focus the previous message |
| Ctrl+Home | Focus the element before the feed |
| Ctrl+End | Focus the element after the feed |
| Tab | Leave the feed |
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.