@molecule/app-feature-thread-tree-react
Feature · thread-tree · App (browser) · v4.1.2 · Apache-2.0
Nested comment tree with per-node collapse, auto-collapse below a configurable depth, and parent-line indicator. Recursive renderer with depth-based indentation and reply / vote slots.
npm install @molecule/app-feature-thread-tree-reactHow it works
@molecule/app-feature-thread-tree-react is a ready-made thread-tree feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.
import { type JSX, useState } from 'react'
import { type Comment, ThreadTree } from '@molecule/app-feature-thread-tree-react'
function vote(comments: Comment[], id: string, next: boolean): Comment[] {
return comments.map((comment) => {
if (comment.id === id) {
const delta = Number(next) - Number(Boolean(comment.upvoted))
return { ...comment, upvoted: next, score: (comment.score ?? 0) + delta }
}
return comment.children ? { ...comment, children: vote(comment.children, id, next) } : comment
})
}
export function CommentsSection(props: {
initialComments: Comment[]
onReply: (id: string) => void
}): JSX.Element {
const [comments, setComments] = useState(props.initialComments)
return (
<ThreadTree
comments={comments}
onReply={props.onReply}
onUpvote={(id, next) => setComments((previous) => vote(previous, id, next))}
/>
)
}Works with: @molecule/app-i18n, @molecule/app-react, @molecule/app-ui, @molecule/app-ui-react
Reference
Nested comment tree with per-node collapse, auto-collapse below a configurable depth, and a parent-line indicator.
Exports the <ThreadTree> recursive renderer plus the Comment type
and defaultCollapsedDepth prop. Used by link-aggregator, blog comments,
podcast, video-streaming, ai-customer-service-bot, and rag-knowledge-base
apps.
Plain-string bodies render as complete paragraphs, preserve line breaks, and wrap long words. Strings stay escaped text. Rich React bodies keep a block container without an extra paragraph around their own block elements.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { type JSX, useState } from 'react'
import { type Comment, ThreadTree } from '@molecule/app-feature-thread-tree-react'
function vote(comments: Comment[], id: string, next: boolean): Comment[] {
return comments.map((comment) => {
if (comment.id === id) {
const delta = Number(next) - Number(Boolean(comment.upvoted))
return { ...comment, upvoted: next, score: (comment.score ?? 0) + delta }
}
return comment.children ? { ...comment, children: vote(comment.children, id, next) } : comment
})
}
export function CommentsSection(props: {
initialComments: Comment[]
onReply: (id: string) => void
}): JSX.Element {
const [comments, setComments] = useState(props.initialComments)
return (
<ThreadTree
comments={comments}
onReply={props.onReply}
onUpvote={(id, next) => setComments((previous) => vote(previous, id, next))}
/>
)
}
Type
feature
Installation
npm install @molecule/app-feature-thread-tree-react @molecule/app-i18n @molecule/app-react @molecule/app-ui @molecule/app-ui-react react
npm install -D @types/react
API
Interfaces
Comment
A single comment node in the tree. Apps own the data shape — the
component reads id, children, and renders the rest via slots.
interface Comment {
/** Stable, unique identifier used as React key + collapse-state key. */
id: string
/** Author display name / handle. */
author: ReactNode
/**
* Plain strings render as complete, escaped paragraphs with line breaks and
* long-word wrapping. Rich React content keeps a block container, so callers
* can supply their own paragraphs, lists or other structured content.
*/
body: ReactNode
/**
* Timestamp string rendered VERBATIM in a `<time>` element — pass a
* pre-formatted display string (e.g. "2 hours ago"); an ISO string
* will be shown raw to users. Also used as the `dateTime` attribute.
* Omit to hide the timestamp.
*/
createdAt?: string
/** Score / upvote count. Optional — omit to hide the count display. */
score?: number
/** Whether the current viewer has upvoted this comment. */
upvoted?: boolean
/** Direct child comments (the recursive payload). */
children?: Comment[]
}
ThreadTreeProps
Props for <ThreadTree>.
interface ThreadTreeProps {
/** Top-level comments. Each may carry nested `children`. */
comments: Comment[]
/** Reply-button handler — receives the comment id. */
onReply?: (commentId: string) => void
/** Upvote-button handler — receives the comment id and the next state. */
onUpvote?: (commentId: string, next: boolean) => void
/** Optional handler called when a node is collapsed or expanded. */
onCollapse?: (commentId: string, collapsed: boolean) => void
/**
* Comments at this depth (0-indexed) start collapsed by default.
* Defaults to `4`. Pass `Infinity` to start fully expanded.
*/
defaultCollapsedDepth?: number
/** Extra classes appended to the outer wrapper. */
className?: string
/** `data-mol-id` selector for AI-agent interaction. */
dataMolId?: string
}
Functions
ThreadTree(props)
Recursive nested-comment tree with per-node collapse / expand, depth-based indentation, and reply / upvote slots. Apps own the data; the component handles rendering, collapse state, and event dispatch.
Intended for link-aggregator threads (HN-style), blog comments, podcast / video comment sections, and AI-conversation transcripts with branching replies.
All UI text resolves through t() so apps localise via the companion
@molecule/app-locales-feature-thread-tree package. All styling
resolves through getClassMap() — no Tailwind utilities live here.
function ThreadTree({
comments,
onReply,
onUpvote,
onCollapse,
defaultCollapsedDepth = DEFAULT_COLLAPSED_DEPTH,
className,
dataMolId,
}: ThreadTreeProps): React.ReactElement
props— Component props.props.comments— Top-level comments (each may carrychildren).props.onReply— Reply-button handler.props.onUpvote— Upvote-button handler.props.onCollapse— Optional handler called on collapse / expand.props.defaultCollapsedDepth— Depth at which nodes auto-collapse (default 4).props.className— Extra classes appended to the outer wrapper.props.dataMolId—data-mol-idselector for AI-agent interaction.
Returns: The rendered tree element.
Injection Notes
Requirements
Peer dependencies:
@molecule/app-i18n^1.0.1@molecule/app-react^4.0.0 || ^5.0.0@molecule/app-ui^1.0.1@molecule/app-ui-react^4.4.0react^18.0.0 || ^19.0.0
Runtime Dependencies
@molecule/app-i18n@molecule/app-react@molecule/app-ui@molecule/app-ui-reactreact
Pass each comment's replies in its children array. The optional
onUpvote and onReply callbacks enable the corresponding buttons.
The parent owns vote state and opens its reply form for the reported id.
All UI text resolves through t() — install
@molecule/app-locales-feature-thread-tree for non-English
locales. All styling resolves through getClassMap() — no Tailwind
utilities live in this package.
Translations
Translation strings are provided by @molecule/app-locales-feature-thread-tree.