How the comment thread, the comment form and the reaction bar fit together, and the events your code handles to save what readers write.
Display here, storage with you
Three components cover reader response. None of them saves anything. Each one shows state you give it and fires an event when the reader does something. Your code sends that to a server and tells the component the result.
| Component | Shows | You write |
<comment-thread> |
A list of comments with author, time, nesting, and Reply, Edit and Delete actions |
One <article data-comment> per comment |
<comment-box> |
A Markdown editor with a preview and Submit and Cancel buttons |
Nothing, or a wrapping <form> |
<reaction-bar> |
Emoji chips with counts, and a button that opens a palette of reactions to add |
One <button> per reaction and a <template> for the palette |
The demo uses a mock server: a short delay, then the component is told the action succeeded. Reply to a comment, add a reaction and watch the event log underneath.
A thread
Render each comment as an <article data-comment> with an id. Its attributes carry the facts and a child marked data-comment-body carries the text. The component adds the header, a relative time and the action buttons around what you wrote.
<comment-thread aria-label="Comments on this recipe"> <article data-comment id="c1" data-author="Noor Haddad" data-time="2026-11-10T08:00:00Z"> <section data-comment-body> <p>Worked well with spelt flour. I needed a little less water.</p> </section> </article> <article data-comment id="c2" data-author="Sam Reyes" data-time="2026-11-10T09:30:00Z" data-parent="c1" data-mine> <section data-comment-body> <p>Good to know. How much less?</p> </section> </article> <template data-reply-form> <comment-box submit-label="Reply" data-show-cancel></comment-box> </template></comment-thread>
| Attribute on the comment | Effect |
data-author, data-author-href, data-author-avatar | The name, an optional profile link and an optional avatar |
data-time | An ISO 8601 time, shown as a relative time in the page's language |
data-parent | The id of the comment this one replies to. Replies are indented under it. |
data-mine | The current reader wrote it. Edit and Delete appear. |
data-edited | A time of last edit. An edited mark appears. |
data-disabled on the thread removes every action, for a closed discussion.
Handling the actions
The thread fires three events. For a reply, pass the promise of your save to waitUntil(). The form shows as busy until it settles, closes when it resolves and keeps the reader's text when it rejects.
const thread = document.querySelector('comment-thread'); thread.addEventListener('comment-thread:reply', (event) => { const { parentId, value } = event.detail; const saved = fetch('/api/comments', { method: 'POST', body: JSON.stringify({ parentId, body: value }), }) .then((response) => response.json()) .then((comment) => { thread.addComment({ id: comment.id, author: comment.author, time: comment.time, bodyText: comment.body, parentId, mine: true, }); }); event.detail.waitUntil(saved);}); thread.addEventListener('comment-thread:delete-request', (event) => { thread.removeComment(event.detail.commentId);});
comment-thread:edit-request carries the comment's id and current text. Show an editor of your choice, then call updateComment(id, { bodyText, edited }).
addComment() and updateComment() take the text as bodyText, which is inserted as plain text, or as bodyHtml, which goes through the library's sanitizer first. Use bodyText unless your server has already turned the reader's Markdown into HTML.
The comment form
<comment-box> is the form for a new top-level comment, and the thread clones it from the <template data-reply-form> for replies. It is a form control: give it a name inside a <form> and its text is submitted with the form, with required, data-min-length and data-max-length as its validation.
Used alone, listen for comment-box:submit. It follows the same rule as a reply: do nothing and the box clears, call waitUntil(promise) to hold it busy until your save settles, or call preventDefault() to keep the text.
Reactions
Write a <button> for each reaction that already has a count, and a <template data-palette> listing every reaction a reader may add. data-reaction is the name your server knows it by. data-label gives the emoji a spoken name.
<reaction-bar aria-label="Reactions to this recipe"> <button type="button" data-reaction="thumbsup" data-label="Thumbs up" data-count="12" data-mine>👍</button> <button type="button" data-reaction="heart" data-label="Heart" data-count="4">❤️</button> <template data-palette> <button type="button" data-reaction="thumbsup" data-label="Thumbs up">👍</button> <button type="button" data-reaction="heart" data-label="Heart">❤️</button> <button type="button" data-reaction="hooray" data-label="Hooray">🎉</button> </template></reaction-bar>
A click fires reaction-bar:toggle with the reaction, whether it is being added or removed, and the count and mine state from before the click. The chip then waits, ignoring further clicks, until you call setCount(). Call it with the new values when the save succeeds and with the old values when it fails.
document.addEventListener('reaction-bar:toggle', async (event) => { const bar = event.target.closest('reaction-bar'); const { reaction, action, count, mine } = event.detail; try { await fetch(`/api/reactions/${reaction}`, { method: action === 'add' ? 'PUT' : 'DELETE' }); bar.setCount(reaction, action === 'add' ? count + 1 : count - 1, { mine: action === 'add' }); } catch { bar.setCount(reaction, count, { mine }); }});
A <reaction-bar> can sit inside a comment, as in the first demo, or under an article. data-disabled makes it read-only.
What is not here
- Storage, accounts, moderation and spam control. Those are your server's job.
- Comments pinned to a place in the text.
<comment-wc> is an experimental stub for that. It needs the <review-surface> element from the separate vb-project-planning package, and without one its button is disabled.
- Private notes for one reader. Those are Highlights and Annotations, and need no server.