Comment Thread
A discussion with nested replies, reaction toggles, editable own comments, and resolve and reopen controls.
pnpm dlx shadcn@latest add @haze/comment-thread---
import { ChevronDown, MessageSquare, Pencil, ThumbsUp, Trash2 } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
import Avatar from "./avatar.astro";
import Button from "./button.astro";
import Textarea from "./textarea.astro";
interface Comment { id: string; author: string; text: string; time: string; own?: boolean; replies?: Comment[] }
interface Props { id: string; label?: string; author?: string; comments?: Comment[]; class?: string }
const { id, label = "Design review", author = "You", class: className, comments = [
{ id: "mila", author: "Mila Chen", text: "Could we keep the export action next to the date range?", time: "Today, 10:24", replies: [{ id: "owen", author: "Owen Park", text: "That also makes the keyboard path shorter.", time: "Today, 10:31" }] },
{ id: "you", author: "You", text: "I will update that grouping before the next review.", time: "Today, 10:42", own: true },
] } = Astro.props;
---
<section id={id} data-haze-comment-thread data-author={author} aria-labelledby={`${id}-title`} class={cn("group/thread grid min-w-0 gap-3 rounded-xl border border-border bg-card p-4.5", className)}>
<div class="flex min-w-0 flex-wrap items-center justify-between gap-2">
<p id={`${id}-title`} class="text-[0.8125rem]/5 font-medium">{label}</p>
<Button data-thread-resolve hidden variant="ghost" size="sm" aria-pressed="false" class="[&[hidden]]:hidden">Resolve thread</Button>
</div>
<div data-thread-body class="grid min-w-0 gap-4 [&[hidden]]:hidden">
<div data-comment-list class="grid min-w-0 gap-4">
{comments.map(comment => <article data-comment data-comment-id={comment.id} data-author={comment.author} aria-label={`Comment by ${comment.author}`} class="grid min-w-0 gap-2">
<div class="flex min-w-0 items-start gap-3">
<Avatar name={comment.author} size="sm" />
<div class="min-w-0 flex-1"><p class="text-[0.8125rem]/5 font-medium">{comment.author}<span class="ms-2 text-muted-foreground">{comment.time}</span></p><p data-comment-text class="mt-1 text-[0.8125rem]/5 whitespace-pre-wrap wrap-anywhere">{comment.text}</p></div>
</div>
<div class="flex flex-wrap gap-1">
<Button data-comment-reaction size="sm" variant="ghost" aria-pressed="false" aria-label={`Like comment by ${comment.author}, 0 likes`} class="h-8 text-xs aria-pressed:bg-muted aria-pressed:text-foreground"><ThumbsUp aria-hidden="true" /><span data-reaction-count>0</span></Button>
<Button data-comment-reply size="sm" variant="ghost" class="h-8 text-xs"><MessageSquare aria-hidden="true" />Reply</Button>
{comment.own && <><Button data-comment-edit size="sm" variant="ghost" aria-label="Edit your comment" class="h-8"><Pencil aria-hidden="true" /></Button><Button data-comment-delete size="sm" variant="ghost" aria-label="Delete your comment" class="h-8"><Trash2 aria-hidden="true" /></Button></>}
</div>
<details data-comment-replies class="group/replies min-w-0 border-s border-border ps-3">
<summary class="inline-flex min-h-6 w-fit cursor-pointer list-none items-center gap-1 rounded-sm text-xs/5 text-muted-foreground transition-colors duration-150 ease-snappy hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring [&::-webkit-details-marker]:hidden"><span data-reply-count>{comment.replies?.length ?? 0}</span> <span data-reply-noun>{comment.replies?.length === 1 ? "reply" : "replies"}</span><ChevronDown aria-hidden="true" class="size-3.5 shrink-0 transition-[rotate] duration-200 ease-snappy group-open/replies:rotate-180" /></summary>
<div data-reply-list class="grid min-w-0 gap-3 pt-3">
{comment.replies?.map(reply => <article aria-label={`Reply by ${reply.author}`} class="grid min-w-0 gap-1"><p class="text-[0.8125rem]/5 font-medium">{reply.author}<span class="ms-2 text-muted-foreground">{reply.time}</span></p><p class="text-[0.8125rem]/5 whitespace-pre-wrap wrap-anywhere">{reply.text}</p></article>)}
</div>
</details>
</article>)}
</div>
<form data-comment-composer class="grid min-w-0 gap-2">
<label for={`${id}-message`} data-composer-label class="text-[0.8125rem]/5 font-medium">Add a comment</label>
<Textarea id={`${id}-message`} name="comment" placeholder="Share feedback with the team" required rows={3} class="resize-none" />
<div class="flex flex-wrap gap-2"><Button type="submit" size="sm" variant="secondary">Post comment</Button><Button data-comment-cancel hidden type="button" size="sm" variant="ghost" class="[&[hidden]]:hidden">Cancel</Button></div>
</form>
</div>
<span data-comment-status role="status" class="text-xs/5 text-muted-foreground empty:hidden"></span>
<template data-new-comment>
<article data-comment class="grid min-w-0 gap-2 motion-safe:animate-rise">
<p class="text-[0.8125rem]/5 font-medium"><span data-new-author></span><span class="ms-2 text-muted-foreground">Just now</span></p>
<p data-comment-text class="text-[0.8125rem]/5 whitespace-pre-wrap wrap-anywhere"></p>
<div class="flex flex-wrap gap-1"><Button data-comment-reaction size="sm" variant="ghost" aria-pressed="false" class="h-8 text-xs aria-pressed:bg-muted aria-pressed:text-foreground"><ThumbsUp aria-hidden="true" /><span data-reaction-count>0</span></Button><Button data-comment-reply size="sm" variant="ghost" class="h-8 text-xs">Reply</Button><Button data-comment-edit size="sm" variant="ghost" aria-label="Edit your comment" class="h-8"><Pencil aria-hidden="true" /></Button><Button data-comment-delete size="sm" variant="ghost" aria-label="Delete your comment" class="h-8"><Trash2 aria-hidden="true" /></Button></div>
<details data-comment-replies class="group/replies min-w-0 border-s border-border ps-3"><summary class="inline-flex min-h-6 w-fit cursor-pointer list-none items-center gap-1 rounded-sm text-xs/5 text-muted-foreground transition-colors duration-150 ease-snappy hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring [&::-webkit-details-marker]:hidden"><span data-reply-count>0</span> <span data-reply-noun>replies</span><ChevronDown aria-hidden="true" class="size-3.5 shrink-0 transition-[rotate] duration-200 ease-snappy group-open/replies:rotate-180" /></summary><div data-reply-list class="grid min-w-0 gap-3 pt-3"></div></details>
</article>
</template>
</section>
<script>
function enhance(root: HTMLElement) {
if (root.hasAttribute("data-enhanced")) return;
const composer = root.querySelector<HTMLFormElement>("[data-comment-composer]")!;
const input = composer.querySelector("textarea")!;
const label = composer.querySelector<HTMLElement>("[data-composer-label]")!;
const cancel = composer.querySelector<HTMLButtonElement>("[data-comment-cancel]")!;
const list = root.querySelector<HTMLElement>("[data-comment-list]")!;
const template = root.querySelector<HTMLTemplateElement>("[data-new-comment]")!;
const status = root.querySelector<HTMLElement>("[data-comment-status]")!;
const resolve = root.querySelector<HTMLButtonElement>("[data-thread-resolve]")!;
let replying: HTMLElement | null = null, editing: HTMLElement | null = null;
let sequence = 0;
const reset = () => { replying = editing = null; input.value = ""; label.textContent = "Add a comment"; cancel.hidden = true; };
const notify = (kind: string, detail: Record<string, unknown> = {}) => root.dispatchEvent(new CustomEvent("haze:comment-change", { bubbles: true, detail: { kind, threadId: root.id, ...detail } }));
root.addEventListener("click", event => {
const button = (event.target as Element).closest<HTMLButtonElement>("button");
const comment = button?.closest<HTMLElement>("[data-comment]");
if (!button || !comment) return;
if (button.hasAttribute("data-comment-reaction")) {
const selected = button.getAttribute("aria-pressed") !== "true";
button.setAttribute("aria-pressed", String(selected));
button.querySelector<HTMLElement>("[data-reaction-count]")!.textContent = selected ? "1" : "0";
button.setAttribute("aria-label", `Like comment by ${comment.dataset.author ?? "You"}, ${selected ? "1 like" : "0 likes"}`);
notify("reaction", { id: comment.dataset.commentId, reaction: "like", selected });
} else if (button.hasAttribute("data-comment-reply")) {
editing = null; replying = comment; label.textContent = `Reply to ${comment.dataset.author ?? "comment"}`; input.value = ""; cancel.hidden = false; input.focus();
} else if (button.hasAttribute("data-comment-edit")) {
replying = null; editing = comment; label.textContent = "Edit your comment"; input.value = comment.querySelector<HTMLElement>(":scope > [data-comment-text], :scope > div [data-comment-text]")?.textContent ?? ""; cancel.hidden = false; input.focus();
} else if (button.hasAttribute("data-comment-delete")) {
comment.querySelector<HTMLElement>("[data-comment-text]")!.textContent = "Deleted comment";
button.parentElement?.remove();
if (editing === comment || replying === comment) reset();
input.focus(); status.textContent = "Comment deleted."; notify("delete", { id: comment.dataset.commentId });
}
});
composer.addEventListener("submit", event => {
event.preventDefault();
const text = input.value.trim();
if (!text) { input.setCustomValidity("Write a comment before posting."); input.reportValidity(); return; }
const edit = editing, reply = replying;
let commentId = edit?.dataset.commentId;
if (edit) edit.querySelector<HTMLElement>("[data-comment-text]")!.textContent = text;
else {
const article = template.content.firstElementChild!.cloneNode(true) as HTMLElement;
commentId = crypto.randomUUID?.() ?? `${root.id}-${Date.now()}-${++sequence}`;
article.dataset.commentId = commentId;
article.dataset.author = root.dataset.author ?? "You";
article.setAttribute("aria-label", `Comment by ${article.dataset.author}`);
article.querySelector<HTMLElement>("[data-new-author]")!.textContent = article.dataset.author;
article.querySelector<HTMLElement>("[data-comment-text]")!.textContent = text;
article.querySelector("[data-comment-reaction]")?.setAttribute("aria-label", `Like comment by ${article.dataset.author}, 0 likes`);
const target = reply?.querySelector<HTMLElement>(":scope > [data-comment-replies] > [data-reply-list]") ?? list;
target.append(article);
if (reply) {
reply.querySelector<HTMLDetailsElement>(":scope > details")!.open = true;
reply.querySelector<HTMLElement>(":scope > details [data-reply-count]")!.textContent = String(target.children.length);
reply.querySelector<HTMLElement>(":scope > details [data-reply-noun]")!.textContent = target.children.length === 1 ? "reply" : "replies";
}
}
notify(edit ? "edit" : reply ? "reply" : "post", { id: commentId, parentId: reply?.dataset.commentId, text });
reset();
input.focus();
status.textContent = edit ? "Comment updated." : reply ? "Reply posted." : "Comment posted.";
});
input.addEventListener("input", () => input.setCustomValidity(""));
cancel.addEventListener("click", () => { reset(); input.focus(); });
resolve.hidden = false;
resolve.addEventListener("click", () => {
const resolved = resolve.getAttribute("aria-pressed") !== "true";
resolve.setAttribute("aria-pressed", String(resolved));
resolve.textContent = resolved ? "Reopen thread" : "Resolve thread";
root.querySelector<HTMLElement>("[data-thread-body]")!.hidden = resolved;
status.textContent = resolved ? "Thread resolved." : "Thread reopened.";
notify(resolved ? "resolve" : "reopen");
});
root.setAttribute("data-enhanced", "");
}
const init = () => document.querySelectorAll<HTMLElement>("[data-haze-comment-thread]").forEach(enhance);
init();
if (!document.documentElement.hasAttribute("data-haze-comment-thread-listener")) {
document.documentElement.setAttribute("data-haze-comment-thread-listener", "");
document.addEventListener("astro:page-load", init);
}
</script>When to use
- Review feedback needs replies anchored to one document or draft.
- People should edit their own notes and resolve a finished discussion.
When not to use
- The conversation is a flat realtime feed. Use chat-thread.
Installation
Add the Haze registry to components.json once, then install Comment Thread with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/comment-threadnpx shadcn@latest add @haze/comment-threadyarn dlx shadcn@latest add @haze/comment-threadbunx --bun shadcn@latest add @haze/comment-threadFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Heading and composer input prefix. |
| label | string | – | Thread title. |
| author | string | – | Display author for new local comments. Defaults to You. |
| comments | Comment[] | – | Initial id, author, text, time, own flag and optional reply array. |
| class | string | – | Additional classes on the component wrapper. |
Keyboard interactions
- EnterSpace
- Open replies, react, edit, delete, resolve or reopen.
- Tab
- Reach the labelled composer and post or cancel a reply.
Accessibility
- Every comment and reply is an article named by its author. Replies use native disclosures; reaction controls expose aria-pressed.
- New text is inserted as plain text. Comment changes emit haze:comment-change for host persistence, and resolve retains access through the reopen control.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Comment Thread" with `npx shadcn@latest add @haze/comment-thread`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import CommentThread from "@/components/haze/ui/comment-thread.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/comment-thread.md
Customize with real content
Adapt the Haze comment-thread ui for our product. Replace the demo values of these props with our real content: id, label, author, comments, class. Keep the layout, the heading level rules and these accessibility details: Every comment and reply is an article named by its author. Replies use native disclosures; reaction controls expose aria-pressed. New text is inserted as plain text. Comment changes emit haze:comment-change for host persistence, and resolve retains access through the reopen control. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using comment-thread, check it fits: use it when review feedback needs replies anchored to one document or draft.; people should edit their own notes and resolve a finished discussion.. Avoid it when the conversation is a flat realtime feed. use chat-thread.. Alternatives: @haze/chat-thread, @haze/inline-edit, @haze/timeline.
Dependencies
- npm: @lucide/astro
- @haze/avatar
- @haze/button
- @haze/textarea
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.