Primitives

The primitive components included in Comments are a great way to build complex, completely custom components. These are headless and unstyled, and can be used to construct a fully styled comment body and rich-text comment composer.

  • Liveblocks functionality, but your own fully custom styles.
  • Build custom components for mentions, suggestions, links, and more.
  • Composable components that work similarly to Radix UI and Headless UI.
  • Can be used alongside the default components.

Concepts

Each primitive is made up of one or more components, for example Comment only needs Comment.Body. This component takes a comment’s body, and turns it into readable text, with links and mentions.

Use regular props

Each primitive accepts regular HTML props, and passes them down to it’s root component. For example a style prop on Comment.Body will be passed down to its root div component. This means you can use regular div properties with your component.

By default, each component uses an appropriate root element, for example Composer.Submit is a button, and Composer.Form is a form. You can use a custom component with asChild.

Use custom component parts

Many primitives allow you to customize their parts by passing a components property. In this example, we’re modifying links in the comment, so that they’re purple and bold.

Merge with your design system components

You can also add components directly from your design system. Let’s say you have a DesignSystemLink that looks like this.

If you were to place this inside <Comment.Link>, you’d render two <a> elements, which is not valid HTML. This occurs because both <Comment.Link> and <DesignSystemLink> render <a> elements.

However, if you add the asChild property to Comment.Link it won’t render any component, and will instead merge into the child element. This means you can use a link element from your design system, and only render a single <a> element.

This is called composability, and virtually all Comments primitives are composable with asChild; they forward their props and refs, merge their classes and styles, and chain their event handlers with the child element.

Composer

The Composer primitive allows you to build a custom rich-text composer, which can be used for creating, or editing, threads and comments. Here’s an example of a composer that creates a new thread when it’s submitted.

Custom component parts can be used to render custom mentions, links, and a suggestions selection popover.

useComposer

The useComposer hook can be placed within <Composer.Form> to check if the composer input is empty, or to submit the form, helpful for creating your own button, or styling the UI.

Comment

The Comment primitive is used to render a comment.body object as text, mentions, and links.

The component above would typically be combined with useThreads.

Custom component parts can be used to render mentions and links.

Timestamp

The Timestamp primitive is a quick helper that will convert a date object, or timestamp, into a friendly format. For example, it’ll render a format similar to “5 minutes ago” for a recent comment, and “22 Dec” for an older comment.