CometChatTextBubble component is a sophisticated message display component that renders text messages with rich formatting capabilities. It serves as the primary content renderer for text-based messages in the CometChat Angular UIKit, handling everything from simple text display to complex features like link previews, translations, mentions, and rich text formatting.
Overview
The Text Bubble component intelligently handles various message types and metadata while maintaining visual consistency with the design system and ensuring full accessibility:- Message Object Processing: Extracts text, metadata, and sender information from CometChat.TextMessage objects
- Rich Text Formatting: Renders rich text HTML with bold, italic, lists, code blocks, and headings
- Link Previews: Displays rich preview cards for URLs with images, titles, descriptions, and favicons
- Message Translation: Shows original and translated text side-by-side
- Mentions Formatting: Highlights @user and @channel mentions with interactive behavior
- URL Detection: Converts URLs to clickable links
- Content Truncation: Implements read more/less for long messages
- Single Emoji Detection: Displays emoji-only messages at larger size
- Dual Styling: Supports sender (outgoing) and receiver (incoming) visual variants
- Security: Sanitizes HTML to prevent XSS attacks
- Accessibility: Full keyboard navigation and screen reader support
Live Preview — Default text bubble preview.
Open in Storybook ↗
Basic Usage
Simple Text Message
Incoming vs Outgoing Messages
With Event Handlers
Properties
| Property | Type | Default | Description |
|---|---|---|---|
message | CometChat.TextMessage | required | The CometChat.TextMessage object to render. Contains text content, metadata, and sender information |
alignment | MessageBubbleAlignment | MessageBubbleAlignment.LEFT | The alignment of the message bubble. LEFT for incoming/receiver messages, RIGHT for outgoing/sender messages |
textFormatters | CometChatTextFormatter[] | [] | Array of text formatters to apply to the message text. If empty, default formatters (mentions, URLs) will be used |
translatedTextOverride | string | undefined | Override for the translated text display. When provided, this text is shown as the translation instead of the message’s built-in translated text |
Events
| Event | Payload Type | Description |
|---|---|---|
linkClick | string | Emitted when a link preview card or URL link is clicked. Contains the URL string |
mentionClick | CometChat.User | Emitted when a formatted mention is clicked. Contains the CometChat.User object of the mentioned user |
Advanced Usage
Messages with Link Previews
The component automatically displays rich link preview cards when message metadata contains link preview data:- Displays preview image (if available)
- Shows title and description
- Extracts and displays domain name
- Shows favicon (if available)
- Supports multiple link previews in a single message
- Clickable cards that emit
linkClickevent
Messages with Translation
Display original and translated text side-by-side:- Shows original text first
- Visual separator line between original and translated text
- Localized label “Translated message”
- Both texts have formatters applied (mentions, URLs, rich text)
Messages with Mentions
The component automatically formats @mentions with interactive behavior:- Automatically detects and formats @mentions
- Different styling for self-mentions vs other users
- Special styling for @all channel mentions
- Clickable mentions that emit
mentionClickevent - Accessible with ARIA labels
Messages with Rich Text Formatting
The component renders rich text HTML with bold, italic, lists, code blocks, and more:- Bold (
<strong>) - Italic (
<em>) - Underline (
<u>) - Strikethrough (
<s>) - Ordered Lists (
<ol>,<li>) - Unordered Lists (
<ul>,<li>) - Code Blocks (
<pre>,<code>) - Headings (
<h1>-<h6>) - Links (
<a>)
Long Messages with Read More/Less
The component automatically truncates long messages and provides read more/less controls:- Content exceeding 80px height (≈4 lines) is automatically truncated
- “Read more” button appears for truncated content
- Clicking “Read more” expands to show full content
- “Show less” button appears when expanded
- Clicking “Show less” collapses back to truncated state
- Buttons use localized text
- Fully keyboard accessible
Single Emoji Messages
The component detects single emoji messages and displays them at a larger size:- Detects messages containing exactly one emoji and no other text
- Handles complex emoji sequences (skin tones, ZWJ sequences, flags)
- Displays at larger font size for better visibility
- Whitespace is ignored in detection
Custom Text Formatters
Provide custom text formatters to transform message text:Combined Features Example
A message with multiple features working together:Customization
Styling with CSS Variables
The Text Bubble component uses CSS variables exclusively for easy customization:Available CSS Variables
| Variable | Purpose | Default |
|---|---|---|
--cometchat-spacing-1 to --cometchat-spacing-8 | Padding, margin, gap | 4px to 32px |
--cometchat-font-body-regular | Main text font | 400 14px Roboto |
--cometchat-font-body-medium | Medium weight text | 500 14px Roboto |
--cometchat-font-caption1-regular | Small text (labels) | 400 12px Roboto |
--cometchat-font-title-regular | Single emoji font | 400 32px Roboto |
--cometchat-background-color-02 | Incoming bubble background | #F5F5F5 |
--cometchat-text-color-primary | Primary text color | #141414 |
--cometchat-text-color-secondary | Secondary text color | #666666 |
--cometchat-primary-button-background | Outgoing bubble background | #6852D6 |
--cometchat-primary-button-text | Outgoing text color | #FFFFFF |
--cometchat-radius-2 | Border radius | 8px |
--cometchat-border-color-light | Border color | #E0E0E0 |
Custom Color Schemes
Custom Mention Styling
Custom Link Preview Styling
Custom Read More/Less Button Styling
Accessibility
The Text Bubble component is fully accessible and follows WCAG 2.1 Level AA guidelines.WCAG 2.1 Compliance
The component meets the following WCAG 2.1 Level AA success criteria:- ✅ 1.1.1 Non-text Content (Level A): All images have text alternatives
- ✅ 1.3.1 Info and Relationships (Level A): Proper semantic structure with ARIA roles
- ✅ 2.1.1 Keyboard (Level A): All functionality available via keyboard
- ✅ 2.4.7 Focus Visible (Level AA): Clear focus indicators provided
- ✅ 4.1.2 Name, Role, Value (Level A): All elements have accessible names and roles
Keyboard Support
| Key | Action | Context |
|---|---|---|
Tab | Navigate to read more/less button | When button is present |
Shift + Tab | Navigate backwards | When button is focused |
Enter | Toggle read more/less | When button is focused |
Space | Toggle read more/less | When button is focused |
ARIA Attributes
The component automatically applies appropriate ARIA attributes:| Attribute | Element | Value | Purpose |
|---|---|---|---|
role | Link preview card | "article" | Identifies the preview as an article |
aria-label | Link preview card | "Link preview for {domain}" | Provides accessible name |
aria-label | Read more button | "Read more" | Describes button action |
aria-label | Show less button | "Show less" | Describes button action |
aria-label | Mention element | "Mention {username}" | Identifies mentioned user |
aria-label | URL link | "Link opens in new tab" | Warns about new tab |
target | URL link | "_blank" | Opens in new tab |
rel | URL link | "noopener noreferrer" | Security attributes |
Screen Reader Behavior
Screen readers announce the text bubble with:- Message text: Read naturally with proper pauses
- Mentions: “Mention John Doe” when encountering @mentions
- Links: “Link, opens in new tab” for URLs
- Link previews: “Article, Link preview for example.com”
- Translation: “Translated message” label before translated text
- Read more: “Button, Read more” when truncated
Accessibility Best Practices
All interactive elements (read more/less buttons, mentions, links) are keyboard accessible and have visible focus indicators.
Best Practices
Use the
alignment property to distinguish between incoming and outgoing messages for proper visual styling.The component automatically detects single emoji messages and displays them larger. No special handling is required.
Related Components
- CometChatMessageBubble: Uses Text Bubble as the content view for text messages
- CometChatMessageList: Displays lists of messages including text bubbles
- CometChatConversations: Shows conversation previews with text message snippets
- CometChatMessagePreview: Displays message previews in conversation lists
Technical Details
- Standalone Component: Can be imported and used independently
- Change Detection: Uses OnPush change detection strategy for optimal performance
- Dependencies:
- Angular CommonModule
- DOMPurify for HTML sanitization
- CometChat SDK for message types
- TranslatePipe for localization
- Bundle Size: Minimal footprint (~8KB including sanitization)
- BEM CSS: Follows Block Element Modifier naming convention
- Accessibility: WCAG 2.1 Level AA compliant
Security
XSS Prevention
The component implements comprehensive XSS prevention through HTML sanitization: Sanitization Process:- All HTML content is processed through DOMPurify
- Only safe rich text formatting tags are allowed
- Script tags, event handlers, and dangerous attributes are stripped
- JavaScript URLs (
javascript:) and data URIs (data:) are removed - Iframes, objects, and embeds are forbidden
- Basic formatting:
<p>,<br>,<strong>,<em>,<u>,<s> - Lists:
<ul>,<ol>,<li> - Code:
<code>,<pre> - Headings:
<h1>through<h6> - Links:
<a>(with sanitized href)
<script>tags- Event handlers (
onclick,onerror,onload, etc.) <iframe>,<object>,<embed>tags<form>,<input>elements<style>tags- Data attributes
Performance Considerations
Optimization Strategies
Change Detection:- Uses OnPush strategy to minimize unnecessary re-renders
- Only updates when inputs change or events are emitted
- Measures content height only once after view initialization
- Avoids repeated DOM measurements during scrolling
- Formatters are applied sequentially in a single pass
- Sanitization occurs once after all formatters complete
- Images are lazy-loaded by the browser
- Failed image loads are handled gracefully without blocking
Best Practices for Performance
The component uses OnPush change detection. Ensure message objects are immutable for optimal performance.
For troubleshooting tips, see the Troubleshooting Guide.