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
Events
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
In addition to the global theme tokens listed above, the Text Bubble exposes component-specific CSS custom properties prefixed with
--cometchat-text-bubble-. These are scoped to the component and can be overridden on the cometchat-text-bubble selector to fine-tune the bubble’s appearance independently of the global theme.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
ARIA Attributes
The component automatically applies appropriate ARIA 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.