@liveblocks/react-comments
provides you with React
components to build a commenting experience. Read our
getting started guide to learn more.
The default components come with default styles, import them either at the root of your app or directly in CSS depending on your setup.
They are built around a few customizable CSS variables:
If these CSS variables aren’t enough, every elements within the components have
clearly defined classes. Additionaly, some elements also have data attributes to
provide contextual information (e.g. data-variant="primary"
on .lb-button
,
data-self
on .lb-comment-mention
, data-loading
on .lb-avatar
and
.lb-user
, etc.).
Dark mode can be implemented by customizing some of the default CSS variables, and we provide styles that do so out-of-the-box. They exist in two versions:
media-query
uses the
prefers-color-scheme
media query.attributes
targets either the dark
class, the data-theme
attribute if
it’s set to "dark"
, or the data-dark
attribute.The default styles use modern CSS features like
container queries,
:has()
and
color-mix()
. While
container queries and :has()
are used as progressive enhancements and aren’t
required, color-mix()
is used extensively to generate the various color
scales. If you need to support browser versions that don’t support
color-mix()
, you can specify the color scales manually:
Overrides can be used to customize the components’ strings and localization-related properties like the locale and reading direction.
They can be set globally once for all components using CommentsConfig
:
Or they can be set per-component (which will take precedence over the global
ones) for contextual cases like a <Composer />
used specifically to reply for
example:
Displays a thread of comments, with a composer to reply to it.
Prop | Type | Description |
---|---|---|
thread | ThreadData<{ resolved?: boolean }> | The thread to display. |
showComposer | boolean | "collapsed" | undefined | How to show or hide the composer to reply to the thread. Defaults to "collapsed" . |
showActions | boolean | "hover" | undefined | How to show or hide the actions. Defaults to "hover" . |
showResolveAction | boolean | undefined | Whether to show the action to resolve the thread. Defaults to true . |
indentCommentBody | boolean | undefined | Whether to indent the comments’ bodies. Defaults to true . |
showDeletedComments | boolean | undefined | Whether to show deleted comments. Defaults to false . |
onResolveChange | (resolved: boolean) => void | The event handler called when changing the resolved status. |
onCommentEdit | (comment: CommentData) => void | The event handler called when a comment is edited. |
onCommentDelete | (comment: CommentData) => void | The event handler called when a comment is deleted. |
onAuthorClick | (userId: string, event: MouseEvent<HTMLElement>) => void | undefined | The event handler called when clicking on a comment’s author. |
onMentionClick | (userId: string, event: MouseEvent<HTMLElement>) => void | undefined | The event handler called when clicking on a mention. |
overrides | Partial<ThreadOverrides & CommentOverrides & ComposerOverrides> | undefined | Override the component’s strings. |
Displays a single comment.
Prop | Type | Description |
---|---|---|
comment | CommentData | The thread to display. |
showActions | boolean | "hover" | undefined | How to show or hide the actions. Defaults to "hover" . |
indentBody | boolean | undefined | Whether to indent the comment’s body. Defaults to true . |
showDeleted | boolean | undefined | Whether to show the comment if it was deleted. If set to false , it will render deleted comments as null .Defaults to false . |
onEdit | (comment: CommentData) => void | The event handler called when the comment is edited. |
onDelete | (comment: CommentData) => void | The event handler called when the comment is deleted. |
onAuthorClick | (userId: string, event: MouseEvent<HTMLElement>) => void | undefined | The event handler called when clicking on the author. |
onMentionClick | (userId: string, event: MouseEvent<HTMLElement>) => void | undefined | The event handler called when clicking on a mention. |
overrides | Partial<CommentOverrides & ComposerOverrides> | undefined | Override the component’s strings. |
Displays a composer to create comments.
By default, submitting the composer will create a new thread. If you provide a
threadId
prop, it will reply to that thread, and if you also provide a
commentId
prop, it will edit that comment. If you want to customize this, you
can use event.preventDefault()
in onComposerSubmit
to disable the default
behavior and call the mutation methods (e.g. createThread
) manually.
Prop | Type | Description |
---|---|---|
threadId | string | undefined | The ID of the thread to reply to or to edit a comment in. |
commentId | string | undefined | The ID of the comment to edit. |
onComposerSubmit | ((comment: ComposerSubmitComment, event: FormEvent<HTMLFormElement>) => Promise<void> | void) | undefined | The event handler called when the composer is submitted. |
defaultValue | CommentBody | undefined | The composer’s initial value. |
collapsed | boolean | undefined | Whether the composer is collapsed. Setting a value will make the composer controlled. |
onCollapsedChange | (collapsed: boolean) => void | The event handler called when the collapsed state of the composer changes. |
defaultCollapsed | boolean | undefined | Whether the composer is initially collapsed. Setting a value will make the composer uncontrolled. |
disabled | boolean | undefined | Whether the composer is disabled. |
autoFocus | boolean | undefined | Whether to focus the composer on mount. |
overrides | Partial<ComposerOverrides> | undefined | Override the component’s strings. |
Primitives are headless/unstyled components that can be used to create custom experiences with full control.
All primitives are composable: they forward their props and refs, merge their classes and styles, and chain their event handlers.
Inspired by Radix (and powered by its
Slot
utility), most
of the primitives also support an asChild
prop to replace the rendered element
by any provided child, and both set of props will be merged. Learn more about
this concept on
Radix’s composition guide.
Displays a comment body.
Prop | Type | Description |
---|---|---|
body | CommentBody | undefined | The comment body to display. If not defined, the component will render null . |
renderMention | ComponentType<CommentRenderMentionProps> | undefined | The component used to render mentions. Defaults to the mention’s userId prefixed by an @. |
renderLink | ComponentType<CommentRenderLinkProps> | undefined | The component used to render links. Defaults to the link’s children property. |
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
A render prop that accepts a component used to render mentions.
Prop | Type | Description |
---|---|---|
userId | string | The mention’s user ID. |
A render prop that accepts a component used to render links.
Prop | Type | Description |
---|---|---|
href | string | The link’s absolute URL. |
children | ReactNode | The link’s content. |
Displays mentions within Comment.Body
’s renderMention
prop.
Prop | Type | Description |
---|---|---|
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
Displays links within Comment.Body
’s renderLink
prop.
Prop | Type | Description |
---|---|---|
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
Surrounds the composer’s content and handles submissions.
Prop | Type | Description |
---|---|---|
onComposerSubmit | ((comment: ComposerSubmitComment, event: FormEvent<HTMLFormElement>) => Promise<void> | void) | undefined | The event handler called when the form is submitted. |
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
Displays the composer’s editor.
Prop | Type | Description |
---|---|---|
defaultValue | CommentBody | undefined | The editor’s initial value. |
placeholder | string | undefined | The text to display when the editor is empty. |
disabled | boolean | undefined | Whether the editor is disabled. |
autoFocus | boolean | undefined | Whether to focus the editor on mount. |
dir | "ltr" | "rtl" | undefined | The reading direction of the editor and related elements. |
renderMention | ComponentType<ComposerRenderMentionProps> | undefined | The component used to render mentions. Defaults to the mention’s userId prefixed by an @. |
renderMentionSuggestions | ComponentType<ComposerRenderMentionSuggestionsProps> | undefined | The component used to render mention suggestions. Defaults to a list of the suggestions’ userId . |
renderLink | ComponentType<ComposerRenderLinkProps> | undefined | The component used to render links. Defaults to the link’s children property. |
Attribute | Value |
---|---|
data-focused | Present when the component is focused. |
data-disabled | Present when the component is disabled. |
A render prop that accepts a component used to render mentions.
Prop | Type | Description |
---|---|---|
userId | string | The mention’s user ID. |
isSelected | boolean | Whether the mention is selected. |
A render prop that accepts a component used to render mention suggestions.
Prop | Type | Description |
---|---|---|
userIds | string[] | The list of suggested user IDs. |
selectedUserId | string | undefined | The currently selected user ID. |
A render prop that accepts a component used to render links.
Prop | Type | Description |
---|---|---|
href | string | The link’s absolute URL. |
children | ReactNode | The link’s content. |
Displays mentions within Composer.Editor
’s renderMention
prop.
Prop | Type | Description |
---|---|---|
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
Attribute | Value |
---|---|
data-selected | Present when the mention is selected. |
Surrounds a list of suggestions within Composer.Editor
.
Prop | Type | Description |
---|---|---|
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
Displays a list of suggestions within Composer.Editor
.
Prop | Type | Description |
---|---|---|
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
Displays a suggestion within Composer.SuggestionsList
.
Prop | Type | Description |
---|---|---|
value | string | The suggestion’s value. |
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
Attribute | Value |
---|---|
data-selected | Present when the item is selected. |
Displays links within Composer.Editor
’s renderLink
prop.
Prop | Type | Description |
---|---|---|
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
A button to submit the composer.
Prop | Type | Description |
---|---|---|
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
Displays a formatted date, and automatically re-renders to support relative formatting. Defaults to relative formatting for recent dates and a short absolute formatting for older ones.
Prop | Type | Description |
---|---|---|
date | Date | string | number | The date to display. |
children | ((date: Date) => ReactNode) | undefined | A function to format the displayed date. Defaults to a relative date formatting function. |
title | string | ((date: Date) => string) | undefined | The title attribute’s value or a function to format it.Defaults to an absolute date formatting function. |
interval | number | false | undefined | The interval in milliseconds at which the component will re-render. Can be set to false to disable re-rendering.Defaults to 30000 . |
asChild | boolean | undefined | Replace the rendered element by the one passed as a child. Defaults to false . |
Returns states and methods related to the composer. Can only be used within
Composer.Form
.