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:- Rendering — turn a marker in the raw message text into styled HTML wherever the message is displayed (
format()). - Authoring — give users a way to produce that marker. Here, a button in the composer’s
toolbarTrailingViewwraps 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
CometChatMessageListandCometChatMessageComposer
Step 1: The Formatter
ExtendCometChatTextFormatter. 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’stoolbarTrailingView 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 withtextFormatters 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
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 — callformat() to produce the bubble HTML. Register the same formatter on each surface where the message can show up.
File: ChatScreen.tsx
How It Round-Trips
Next Steps
- Text Formatters — the built-in formatters and the full
CometChatTextFormatterAPI - Message Composer → toolbarTrailingView — the toolbar slot in detail
- Message Bubble — how bubbles render message content