This Web Component follows the open-wc recommendation and is meant to be used with the Vonage Client SDK In-App Messaging.
A goal is to simplify the code needed to create a chat room quickly. Please see the Creating a chat app tutorial to see an implementation using Vanilla JavaScript.
This Web Component can be used to replace a part of the Chat app UI.
npm i @vonage/vc-messages
<script type="module">
import '@vonage/vc-messages/vc-messages.js';
</script>
<vc-messages></vc-messages>
-
loadPreviousMessages
: set to load any previous messages in the conversation. Default behavior is to load no previous messages. -
filter
: set to filter for only the events you want to load. Default is ['message','member:joined','member:left','member:invited']
<vc-messages loadPreviousMessages="10" filter='["message"]'></vc-messages>
The vc-messages
Web Component can handle all its responsibilities in a Conversation.
First, get a reference to the element:
const vcMessages = document.querySelector("vc-messages");
Then, pass the Conversation object to the Web Component:
vcMessages.client = client;
vcMessages.conversationId = conversationId;
Note: To see where
conversationId
andclient
came from, see step 4 and step 11 in the tutorial.
By default, there are no styles applied to the Web Component.
The vc-messages
component uses CSS part to apply custom styles.
Here are two diagram that labels the parts of the component as well as the default style:
To style the overall message, the part is "message". To style the application user's message, the part to target is "message mine".
Each message is made up of the text (part is "message-text") or image (part is "message-image"), and their name (part is "username"). To specifically style the application user's message text, target "message-text mine" or for message image, target "message-image mine". For their name, the target would be "username mine".
To style the image itself, the part is "image". To target the user's image, the part is "image mine".
To get the styling shown in the image:
vc-messages {
height: 300px;
}
vc-messages::part(message) {
padding: 15px;
margin: 5px;
}
vc-messages::part(message mine) {
text-align: right;
}
vc-messages::part(message-text) {
border-radius: 6px;
background-color: #f0f0f0;
display: inline-block;
padding: 2px 8px;
border-radius: 6px 6px 6px 0;
font-size: 1.4rem;
}
vc-messages::part(message-text mine) {
background-color: #e0e0ff;
border-radius: 6px 6px 0px 6px;
}
vc-messages::part(message-image) {
background-color: #f0f0f0;
display: inline-block;
padding: 8px;
border-radius: 6px 6px 6px 0;
font-size: 1.4rem;
width: 100%;
max-width: 250px;
}
vc-messages::part(message-image mine) {
background-color: #e0e0ff;
border-radius: 6px 6px 0px 6px;
}
vc-messages::part(username) {
font-size: 1.2rem;
}
vc-messages::part(username mine) {
text-align: right;
color: black;
}
Another example could be:
vc-messages {
background-color: red;
height: 300px;
}
vc-messages::part(message) {
background-color: green;
border-radius: 20px 20px 20px 0;
}
vc-messages::part(message mine) {
text-align: left;
background-color: royalblue;
border-radius: 20px 20px 0 20px;
border: 0;
}
vc-messages::part(message-text) {
background-color: yellow;
border-radius: 20px 20px 20px 0;
}
vc-messages::part(message-text mine) {
background-color: purple;
color: white;
font-style: italic;
border-radius: 20px 20px 0 20px;
}
vc-messages::part(message-image) {
background-color: yellow;
display: inline-block;
padding: 8px;
border-radius: 6px 6px 6px 0;
font-size: 1.4rem;
width: 100%;
max-width: 250px;
}
vc-messages::part(message-image mine) {
background-color: purple;
border-radius: 6px 6px 0px 6px;
}
vc-messages::part(username) {
color: white;
}
vc-messages::part(username mine) {
text-align: right;
font-style: italic;
color: black;
}
For most of the tools, the configuration is in the package.json
to minimize the amount of files in your project.
If you customize the configuration a lot, you can consider moving them to individual files.
npm start
To run a local development server that serves the basic demo located in demo/index.html