Mention in React Chat UI

11 Aug 202619 minutes to read

The Syncfusion ChatUI allows users to mention others in messages using the @ character, with dropdown for selecting users. The following sections explain how to configure mentions

Configure mention users

You can use the mentionUsers property to define an array of users for the mention suggestion popup.

import { ChatUIComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    const currentUserModel = {
        id: "user1",
        user: "Albert"
    };

    const michaleUserModel = {
        id: "user2",
        user: "Michale Suyama"
    };

    const customUserModel = {
        id : "custom-user",
        user : "Reena"
    };

    const chatMessages = [
        {
            author: currentUserModel,
            text: "Want to get coffee tomorrow?"
        },
        {
            author: michaleUserModel,
            text: "Sure! What time?"
        },
        {
            author: currentUserModel,
            text: "{0} How about 10 AM?",
            mentionUsers: [michaleUserModel]
        }
    ];

    return (
        // Specifies the tag for rendering the Chat UI component
        <ChatUIComponent 
            headerText={"TeamSync Professionals"}
            user={currentUserModel} 
            messages={chatMessages} 
            mentionUsers={[currentUserModel, customUserModel]} 
        />
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
import { ChatUIComponent, UserModel, MessageModel } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    const currentUserModel: UserModel = {
        id: "user1",
        user: "Albert"
    };

    const michaleUserModel: UserModel = {
        id: "user2",
        user: "Michale Suyama"
    };

    const customUserModel: UserModel = {
        id : "custom-user",
        user : "Reena"
    };

    const chatMessages = [
        {
            author: currentUserModel,
            text: "Want to get coffee tomorrow?"
        },
        {
            author: michaleUserModel,
            text: "Sure! What time?"
        },
        {
            author: currentUserModel,
            text: "{0} How about 10 AM?",
            mentionUsers: [michaleUserModel]
        }
    ];

    return (
        // Specifies the tag for rendering the Chat UI component
        <ChatUIComponent 
            headerText={"TeamSync Professionals"}
            user={currentUserModel} 
            messages={chatMessages} 
            mentionUsers={[currentUserModel, customUserModel]} 
        />
    );
}

ReactDOM.render(<App />, document.getElementById('container'));

Customize the mention trigger character

You can use the mentionTriggerChar property to customize the character which triggers the mention popup. The default value is @.

import { ChatUIComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    const currentUserModel = {
        id: "user1",
        user: "Albert"
    };

    const michaleUserModel = {
        id: "user2",
        user: "Michale Suyama"
    };

    const chatMessages = [
        {
            author: currentUserModel,
            text: "Want to get coffee tomorrow?"
        },
        {
            author: michaleUserModel,
            text: "Sure! What time?"
        },
        {
            author: currentUserModel,
            text: "{0} How about 10 AM?",
            mentionUsers: [michaleUserModel]
        }
    ];

    return (
        // Specifies the tag for rendering the Chat UI component
        <ChatUIComponent 
            headerText={"TeamSync Professionals"}
            user={currentUserModel} 
            messages={chatMessages} 
            mentionUsers={[currentUserModel, { user: 'Reena', id: 'custom-user' }]} 
            mentionTriggerChar="@"
        />
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
import { ChatUIComponent, UserModel, MessageModel } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    const currentUserModel: UserModel = {
        id: "user1",
        user: "Albert"
    };

    const michaleUserModel: UserModel = {
        id: "user2",
        user: "Michale Suyama"
    };

    const chatMessages = [
        {
            author: currentUserModel,
            text: "Want to get coffee tomorrow?"
        },
        {
            author: michaleUserModel,
            text: "Sure! What time?"
        },
        {
            author: currentUserModel,
            text: "{0} How about 10 AM?",
            mentionUsers: [michaleUserModel]
        }
    ];

    return (
        // Specifies the tag for rendering the Chat UI component
        <ChatUIComponent 
            headerText={"TeamSync Professionals"}
            user={currentUserModel} 
            messages={chatMessages} 
            mentionUsers={[currentUserModel, { user: 'Reena', id: 'custom-user' }]} 
            mentionTriggerChar="@"
        />
    );
}

ReactDOM.render(<App />, document.getElementById('container'));

Predefine mentions with messages

You can use the text property in the MessageModel to include predefined mentions in chat messages. The mentions field stores the selected users for each message.

The mentionUsers property in a message contains a list of users, mapped by their order where the placeholder {0} is replaced by the first user, {1} by the second, and so on, automatically generating mentions in the user interface. If a placeholder’s index is negative or exceeds the bounds of the mentionUsers array (e.g., using {5} with only two users), the mapping for that placeholder fails, and the placeholder text itself (e.g., {5}) is displayed in the final message.

import { ChatUIComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {
    const currentUserModel = {
        id: "user1",
        user: "Albert"
    };

    const michaleUserModel= {
        id: "user2",
        user: "Michale Suyama"
    };

    const chatMessages= [
        {
            author: currentUserModel,
            text: "Hi {0}, are we on track for the deadline?",
            mentionUsers: [michaleUserModel]
        },
        {
            author: michaleUserModel,
            text: "Yes {0}, the design phase is complete.",
            mentionUsers: [currentUserModel]
        },
        {
            author: currentUserModel,
            text: "I’ll review it and send feedback by today."
        }
    ];

    return (
        // Specifies the tag for rendering the Chat UI component
        <ChatUIComponent   headerText= {"TeamSync Professionals"} user={currentUserModel} messages={chatMessages} />
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
import { ChatUIComponent, UserModel, MessageModel } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    const currentUserModel: UserModel = {
        id: "user1",
        user: "Albert"
    };

    const michaleUserModel: UserModel = {
        id: "user2",
        user: "Michale Suyama"
    };

    const chatMessages: MessageModel[] = [
        {
            author: currentUserModel,
            text: "Hi {0}, are we on track for the deadline?",
            mentionUsers: [michaleUserModel]
        },
        {
            author: michaleUserModel,
            text: "Yes {0}, the design phase is complete.",
            mentionUsers: [currentUserModel]
        },
        {
            author: currentUserModel,
            text: "I’ll review it and send feedback by today."
        }
    ];

    return (
        // Specifies the tag for rendering the Chat UI component
        <ChatUIComponent headerText= {"TeamSync Professionals"} user={currentUserModel} messages={chatMessages} />
    );
}

ReactDOM.render(<App />, document.getElementById('container'));

Configure mentionSelect

You can use the mentionSelect event which triggers when a user selects an item from the mention dropdown, providing access to the selected user’s details for custom handling.

import { ChatUIComponent } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    const currentUserModel ={
        id: "user1",
        user: "Albert"
    };

    const michaleUserModel= {
        id: "user2",
        user: "Michale Suyama"
    };
   const handleMentionSelect = (args) => {
       //your required action 
 }

    return (
        // Specifies the tag for rendering the Chat UI component
        <ChatUIComponent 
            mentionUsers={[currentUserModel, michaleUserModel]} 
            mentionSelect={handleMentionSelect} 
        />
    );
}

ReactDOM.render(<App />, document.getElementById('container'));
import { ChatUIComponent, UserModel, MentionSelectEventArgs } from '@syncfusion/ej2-react-interactive-chat';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    const currentUserModel: UserModel = {
        id: "user1",
        user: "Albert"
    };

    const michaleUserModel: UserModel = {
        id: "user2",
        user: "Michale Suyama"
    };

    const handleMentionSelect = (args: MentionSelectEventArgs) => {
        // Your required action here
    };

    return (
        // Specifies the tag for rendering the Chat UI component
        <ChatUIComponent 
            mentionUsers={[currentUserModel, michaleUserModel]} 
            mentionSelect={handleMentionSelect} 
        />
    );
}

ReactDOM.render(<App />, document.getElementById('container'));