Skip to main content

Goal

By the end of this guide you will have a color formatter: a toolbar button in the composer that wraps the selected text in a color marker, and a formatter that renders that marker as colored text everywhere the message appears — in the composer, in the message list, conversation subtitle, pinned and saved messages. A text formatter has two jobs, and this guide covers both:
  1. Rendering — turn a marker in the raw message text into styled HTML wherever the message is displayed (format()).
  2. Authoring — give users a way to produce that marker. Here, a button in the composer’s toolbarTrailingView wraps the current selection.
For the full formatter reference — the built-in Markdown, Mentions, and URL formatters and the complete CometChatTextFormatter API — see Text Formatters. This guide is the minimal, task-focused version.

Prerequisites

  • Completed the Integration Guide
  • A chat screen using CometChatMessageList and CometChatMessageComposer

Step 1: The Formatter

Extend CometChatTextFormatter. The one method that matters for rendering is format(): it receives the raw message text and returns HTML. Our marker is {color=VALUE}...{/color}, and we turn it into a colored <span>. File: src/formatters/ColorFormatter.ts
format() must store originalText, set formattedText, and return the formatted string — the pipeline relies on those fields. Keep it fast: it runs on every text message render.

Step 2: The Toolbar Button

The composer’s toolbarTrailingView renders a node at the end of the rich-text toolbar. Put a button there that wraps the user’s current selection in the color marker. File: src/components/ColorButton.tsx
onMouseDown={(e) => e.preventDefault()} is the key detail — without it, clicking the button moves focus out of the editor and clears the selection before your handler runs.

Step 3: Wire It Into the Composer

Register the formatter with textFormatters and mount the button with toolbarTrailingView. The toolbar (and therefore the trailing view) only renders when the rich-text editor is enabled, so pass enableRichTextEditor. File: ChatScreen.tsx
Now: the user selects text, clicks 🎨, and the input becomes Hello {color=#e5484d}world{/color}. On send, that raw text is stored on the message.

Step 4: Render It Everywhere the Message Appears

The marker only becomes color when a surface runs the formatter. Read-only surfaces — like the message list, conversations, pinned/saved panels — call format() to produce the bubble HTML. Register the same formatter on each surface where the message can show up. File: ChatScreen.tsx
A formatter is only applied where you register it. If you add textFormatters to the composer but not the message list, the author sees the marker but readers see raw {color=...} text. Register it on every surface that displays the message.

How It Round-Trips

The marker is plain text on the message, so it survives storage and delivery untouched; each display surface turns it into color independently through the formatter you registered.

Next Steps