Comment Thread
One conversation on a piece of content: the first comment and its replies, a reply box, edit and delete on your own comments, and Resolve.
c2-comment-thread is a comment and its replies, like a comment in Google Docs, a review thread on GitHub or a pin in Figma. It shows the text the thread is about, each comment with its author and time, a reply box, edit and delete on your own comments, emoji reactions, and Resolve.
Import
import '@c2n/components/comment-thread'Usage
Commenting on: border-radius: 6px
Comments
comments lists the thread oldest first, as a JSON attribute or a property: the first is the root comment, the rest its replies. Each has an id, an author ({ id?, name, avatar? }), a body, a time (an ISO date-time or a timestamp), and optionally edited and reactions. Bodies are plain text, never read as HTML: line breaks are kept and @mentions are highlighted. quote shows the text the thread is about above the first comment.
Who can do what
current-user ({ id?, name, avatar? }) says who is writing. It gets the reply box and Resolve, and can edit and delete its own comments, matched by id, or by name when there are no ids. With no current user, or with readonly, the thread is read-only.
Events
Every change is a cancelable event, fired before it is applied:
comment-add({ body }), from the Reply button or Ctrl/⌘+Enter.comment-edit({ id, body }). Escape cancels an edit.comment-delete({ id }), after a confirmation.resolve-change({ resolved }).reaction-change({ id, emoji, reacted, count }), shown withreactions.
Cancel an event to keep the thread controlled, for example while the server answers, and set comments yourself. An added comment that is not cancelled gets a temporary id (local-1). Deleting the first comment only fires the event: removing the thread is up to the app.
thread.addEventListener('comment-add', async (event) => {
event.preventDefault()
const saved = await api.reply(threadId, event.detail.body)
thread.comments = [...thread.comments, saved]
})Long and resolved threads
A thread with more than collapse-after replies (4 by default) shows its first reply and its last two, with “Show N earlier replies” between them. A resolved thread folds down to its first comment, with the number of replies and a Reopen button.
With a selection toolbar
c2-selection-toolbar puts a Comment button over the text a reader selects. Its selection-action event carries the text, which becomes the new thread’s quote.