Skip to main content
The 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

The component automatically displays rich link preview cards when message metadata contains link preview data:
Link Preview Features:
  • 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 linkClick event

Messages with Translation

Display original and translated text side-by-side:
Translation Display:
  • 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:
Mentions Features:
  • Automatically detects and formats @mentions
  • Different styling for self-mentions vs other users
  • Special styling for @all channel mentions
  • Clickable mentions that emit mentionClick event
  • Accessible with ARIA labels

Messages with Rich Text Formatting

The component renders rich text HTML with bold, italic, lists, code blocks, and more:
Supported Rich Text Elements:
  • 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>)
Security: All HTML is sanitized to prevent XSS attacks while preserving safe formatting tags.

Long Messages with Read More/Less

The component automatically truncates long messages and provides read more/less controls:
Truncation Behavior:
  • 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:
Single Emoji Detection:
  • 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:
Formatter Order: Formatters are applied sequentially. Each formatter’s output becomes the input for the next formatter.

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 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:
  1. Message text: Read naturally with proper pauses
  2. Mentions: “Mention John Doe” when encountering @mentions
  3. Links: “Link, opens in new tab” for URLs
  4. Link previews: “Article, Link preview for example.com”
  5. Translation: “Translated message” label before translated text
  6. Read more: “Button, Read more” when truncated

Accessibility Best Practices

The component automatically handles all accessibility requirements. No additional ARIA attributes or keyboard handling is needed.
When handling linkClick events, ensure your navigation maintains accessibility by managing focus appropriately.
All interactive elements (read more/less buttons, mentions, links) are keyboard accessible and have visible focus indicators.

Best Practices

Always provide the complete CometChat.TextMessage object to ensure all features (link previews, translations, mentions) work correctly.
The component sanitizes HTML to prevent XSS attacks. Don’t bypass this sanitization or inject unsanitized HTML into the message text.
Use the alignment property to distinguish between incoming and outgoing messages for proper visual styling.
Handle linkClick and mentionClick events to provide interactive behavior like opening URLs or navigating to user profiles.
When providing custom text formatters, ensure they return safe HTML. The component will sanitize the output, but formatters should not introduce XSS vulnerabilities.
The component automatically detects single emoji messages and displays them larger. No special handling is required.
  • 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:
  1. All HTML content is processed through DOMPurify
  2. Only safe rich text formatting tags are allowed
  3. Script tags, event handlers, and dangerous attributes are stripped
  4. JavaScript URLs (javascript:) and data URIs (data:) are removed
  5. Iframes, objects, and embeds are forbidden
Allowed HTML Tags:
  • Basic formatting: <p>, <br>, <strong>, <em>, <u>, <s>
  • Lists: <ul>, <ol>, <li>
  • Code: <code>, <pre>
  • Headings: <h1> through <h6>
  • Links: <a> (with sanitized href)
Forbidden Elements:
  • <script> tags
  • Event handlers (onclick, onerror, onload, etc.)
  • <iframe>, <object>, <embed> tags
  • <form>, <input> elements
  • <style> tags
  • Data attributes
Never bypass the component’s HTML sanitization. All user-generated content is automatically sanitized to prevent XSS attacks.

Performance Considerations

Optimization Strategies

Change Detection:
  • Uses OnPush strategy to minimize unnecessary re-renders
  • Only updates when inputs change or events are emitted
Content Truncation:
  • Measures content height only once after view initialization
  • Avoids repeated DOM measurements during scrolling
Text Formatting:
  • Formatters are applied sequentially in a single pass
  • Sanitization occurs once after all formatters complete
Link Preview Rendering:
  • Images are lazy-loaded by the browser
  • Failed image loads are handled gracefully without blocking

Best Practices for Performance

Provide text formatters via input to avoid recreating default formatters on every message.
The component uses OnPush change detection. Ensure message objects are immutable for optimal performance.
For troubleshooting tips, see the Troubleshooting Guide.