Skip to content

Chat

The Chat component provides a flexible and interactive messaging interface for applications. It supports both incoming and outgoing messages with avatars, sender names, timestamps, and a message input area with a send button.

  • Textarea component (imported from ”../textarea/index.js”)
  • Button component (imported from ”../button/index.js”)

See the Example by Serhii Pimenov on CodePen.

<div data-role="chat"></div>
<div data-role="chat"
data-name="John Smith"
data-avatar="<span>👨</span>"
data-welcome="Welcome to our chat! How can I help you today?"
data-welcome-avatar="<span>🤖</span>"
data-title="Customer Support"
data-width="100%"
data-height="400px">
</div>
const chat = Metro.makePlugin("#myChatElement", "chat", {
name: "John Smith",
avatar: "<span>👨</span>",
welcome: "Welcome to our chat! How can I help you today?",
welcomeAvatar: "<span>🤖</span>",
title: "Customer Support",
width: "100%",
height: "400px"
});
ParameterTypeDefaultDescription
chatDeferrednumber0Deferred initialization time in milliseconds
inputTimeFormatstringnullFormat for parsing input time strings
timeFormatstring”D MMM hh:mm A”Format for displaying message timestamps
namestring”John Doe”Default name for outgoing messages
avatarstring👦HTML for the avatar of outgoing messages
welcomestringnullWelcome message text (displayed when chat initializes)
welcomeAvatarstring👽HTML for the avatar of the welcome message
titlestringnullTitle for the chat component
widthstring”100%“Width of the chat component
heightstring”auto”Height of the chat component
initialHeightnumber100Initial Height of the chat component
messagesobjectnullInitial messages to display
attachAcceptstring”*“Attach file types
readonlybooleanfalseWhether the chat is in read-only mode
sendModestring”enter”Trigger for message send: enter, control+enter, button
useEmojibooleantrueUse emoji in message
useLinkbooleantrueUse link in message
useCodebooleantrueUse code blocks in message
buttonsstring”send attach”Buttons to use
clsChatstring""Additional CSS class for the chat container
clsNamestring""Additional CSS class for the sender name
clsTimestring""Additional CSS class for the message time
clsInputstring""Additional CSS class for the input field
clsSendButtonstring""Additional CSS class for the send button
clsMessageLeftstring”default”CSS class for left-aligned messages
clsMessageRightstring”default”CSS class for right-aligned messages

Each message object should contain the following properties:

  • text - Message text
  • time - Message timestamp
  • position - “left” or “right”
  • name - Sender name
  • avatar - Sender avatar HTML
  • id (optional) - Unique message ID
  • attach (optional) - Attachment, must be a file object

To access the Chat API methods, use:

const chat = Metro.getPlugin("#myChatElement", "chat");
  • add(msg) - Adds a message to the chat. The message object should contain:
chat.add({
id: "msg-1", // Optional unique ID for the message
name: "User Name",
avatar: "<span>👨</span>", // Can be HTML, image URL, or data URL
text: "Hello, world!",
position: "left", // "left" or "right"
time: new Date() // Date object or string
});
const attach = new File(["content"], "file.txt", { type: "text/plain" });
chat.add({
name: "User Name",
avatar: "<span>👨</span>",
position: "right",
time: new Date(),
attach: attach // File object for attachment
});
  • addMessages(messages) - Adds multiple messages to the chat.
chat.addMessages([
{
name: "User 1",
avatar: "<span>👨</span>",
text: "Hello!",
position: "left",
time: new Date()
},
{
name: "User 2",
avatar: "<span>👩</span>",
text: "Hi there!",
position: "right",
time: new Date()
}
]);
  • delMessage(id) - Deletes a message by ID.

  • updMessage(msg) - Updates a message. The message object should contain:

  • id - Message ID to update
  • text - New message text
  • time - New message timestamp
  • clear() - Clears all messages from the chat.

  • toggleReadonly(readonly) - Toggles the read-only state of the chat.

EventDescription
onMessageTriggered when a message is added to the chat
onSendTriggered when a message is sent
onSendButtonClickTriggered when the send button is clicked
onChatCreateTriggered when the chat component is created
VariableDefault (Light)Dark ModeDescription
--chat-border-colorvar(—border-color)var(—border-color)Border color for the chat component
--chat-background#ffffff#343637Background color for the chat component
--chat-color#191919#dbdfe7Text color for the chat component
--chat-message-background#f5f5f5#4a4d51Background color for chat messages
--chat-message-color#191919#dbdfe7Text color for chat messages
/* Custom styling for chat */
.custom-chat {
--chat-border-color: #2196F3;
--chat-background: #E3F2FD;
--chat-color: #0D47A1;
--chat-message-background: #BBDEFB;
--chat-message-color: #0D47A1;
}
  • .chat - Main container class
  • .title - Chat title
  • .messages - Messages container
  • .message-input - Input area container
  • .chat-input - Text input field
  • .message - Message container
  • .message.left - Left-aligned message (incoming)
  • .message.right - Right-aligned message (outgoing)
  • .message-item - Message item container
  • .message-avatar - Message avatar
  • .message-text - Message text container
  • .message-text-inner - Message text content
  • .message-sender - Message sender name
  • .message-time - Message timestamp
  • .--next - Modifier for consecutive messages from the same side
  1. Use appropriate avatars to distinguish between different users
  2. Keep message texts concise for better readability
  3. Use the welcome message to provide initial instructions or greeting
  4. Consider using custom styling to match your application’s theme
  5. Use the readonly mode for displaying chat history without allowing new messages