Skip to main content
FieldValue
Package@cometchat/chat-uikit-react-native
Key classCometChatUrlsFormatter (extends CometChatTextFormatter)
Required setupCometChatUIKit.init(UIKitSettings) then CometChatUIKit.login("UID")
PurposeAuto-detects URLs, emails, and phone numbers in text messages and converts them to clickable links
Sample appGitHub
RelatedCustom Text Formatter · Mentions Formatter · All Guides
CometChatUrlsFormatter extends CometChatTextFormatter to detect URLs, email addresses, and phone numbers in text messages and render them as clickable links using React Native’s Linking API.

Features

FeatureDescription
URL detectionAutomatically detects HTTP/HTTPS URLs
Email detectionDetects email addresses and opens mail client
Phone detectionDetects phone numbers and opens dialer
Custom stylingCustomize link text color and font
Native linkingUses React Native Linking API for native behavior

Usage

The CometChatUrlsFormatter is included by default in the UI Kit. You can also create a custom instance with custom styling:
import React, { useState, useEffect, useMemo } from "react";
import { View, SafeAreaView } from "react-native";
import { CometChat } from "@cometchat/chat-sdk-react-native";
import {
  CometChatMessageHeader,
  CometChatMessageList,
  CometChatMessageComposer,
  CometChatUrlsFormatter,
} from "@cometchat/chat-uikit-react-native";

function ChatScreen(): React.JSX.Element {
  const [chatUser, setChatUser] = useState<CometChat.User | undefined>();

  useEffect(() => {
    CometChat.getUser("uid").then((user) => {
      setChatUser(user);
    });
  }, []);

  const urlFormatter = useMemo(() => {
    const formatter = new CometChatUrlsFormatter();
    formatter.setStyle({
      linkTextColor: "#1a0dab",
      linkTextFont: {
        fontSize: 16,
        fontWeight: "500",
      },
    });
    return formatter;
  }, []);

  return (
    <SafeAreaView style={{ flex: 1 }}>
      {chatUser && (
        <View style={{ flex: 1 }}>
          <CometChatMessageHeader user={chatUser} />
          <CometChatMessageList
            user={chatUser}
            textFormatters={[urlFormatter]}
          />
          <CometChatMessageComposer user={chatUser} />
        </View>
      )}
    </SafeAreaView>
  );
}

export default ChatScreen;

Customization

Use the setStyle method to customize link appearance:
import { CometChatUrlsFormatter } from "@cometchat/chat-uikit-react-native";

const urlFormatter = new CometChatUrlsFormatter();

urlFormatter.setStyle({
  linkTextColor: "#007AFF",  // iOS blue
  linkTextFont: {
    fontSize: 16,
    fontWeight: "600",
  },
});

Style Properties

PropertyTypeDescription
linkTextColorColorValueColor of the link text
linkTextFontTextStyleFont styling for link text (fontSize, fontWeight, etc.)

How It Works

The CometChatUrlsFormatter uses regex patterns to detect:
  1. URLs: HTTP and HTTPS links
  2. Emails: Email addresses (opens mailto: link)
  3. Phone numbers: Phone numbers (opens tel: link)
When a link is tapped, it uses React Native’s Linking API to open the appropriate handler:
// Internal implementation
const handlePress = async () => {
  let finalUrl = url;
  if (!url.match(/^(https?|mailto|tel):/i)) {
    finalUrl = `http://${url}`;
  }
  
  const canOpen = await Linking.canOpenURL(finalUrl);
  if (canOpen) {
    Linking.openURL(finalUrl);
  }
};

Combining with Other Formatters

You can use multiple formatters together. The order matters — formatters are applied in sequence:
import {
  CometChatUrlsFormatter,
  CometChatMentionsFormatter,
} from "@cometchat/chat-uikit-react-native";

const urlFormatter = new CometChatUrlsFormatter();
const mentionsFormatter = new CometChatMentionsFormatter();

// Apply mentions first, then URLs
const textFormatters = [mentionsFormatter, urlFormatter];

<CometChatMessageList
  user={chatUser}
  textFormatters={textFormatters}
/>

Next Steps