@skyra/discord-message-components v1.1.2
React discord-message-components to easily build and display fake Discord messages on your webpackge.
This is an adoptation of wc-discord-message from Danktuary, using Favna's fork as submodule to generate React components using @stencil/react-output-target. After generating the component data this library leverages Rollup to bundle it up for publishing to NPM. Please note that wc-discord-message is a peer dependency of this project! The rest if this README is based on wc-discord-message with adoptation to fit React coding style
Features
- Design modeled after Discord itself
- Comfy and compact mode support
- Dark and light themes support
- Set the message author's username, avatar (use defaults or provide your own), role color, and "bot" tag status
- Display fake user, role, and channel mentions
- Complete embed support
- Simple syntax!
Preview
<DiscordMessages>
<DiscordMessage>
Hey guys, I'm new here! Glad to be able to join you all!
</DiscordMessage>
<DiscordMessage author="Dawn" avatar="red">
Hi, I'm new here too!
</DiscordMessage>
<DiscordMessage
author="Sanctuary"
avatar="https://i.imgur.com/0TeacfY.png"
roleColor="#0099ff"
>
Hey, <DiscordMention>User</DiscordMention> and{" "}
<DiscordMention>Dawn</DiscordMention>. Welcome to our server! Be sure to
read through the <DiscordMention type="channel">rules</DiscordMention>. You
can ping{" "}
<DiscordMention type="role" color="#70f0b4">
Support
</DiscordMention>{" "}
if you need help.
</DiscordMessage>
<DiscordMessage
author="Twelve"
avatar="https://i.imgur.com/Mc0F0Hf.png"
roleColor="#70f0b4"
>
Hello everyone! How's it going?
</DiscordMessage>
<DiscordMessage author="User">
Thank you <DiscordMention highlight>Sanctuary</DiscordMention>!
</DiscordMessage>
<DiscordMessage author="Yotsuba" avatar="https://i.imgur.com/amw0MGJ.png">
I'm doing well, <DiscordMention>Twelve</DiscordMention>. What about
yourself?
</DiscordMessage>
<DiscordMessage
author="Twelve"
avatar="https://i.imgur.com/Mc0F0Hf.png"
roleColor="#70f0b4"
>
!8ball How am I doing today?
</DiscordMessage>
<DiscordMessage
author="Rinon"
avatar="https://i.imgur.com/axQ9wJl.png"
roleColor="violet"
bot
>
Yes.
</DiscordMessage>
</DiscordMessages>
Check out the live demo for further examples.
Installation
yarn add @skyra/discord-message-components && yarn add --dev wc-discord-message
# or npm install @skyra/discord-message-components && npm install --dev wc-discord-message
Usage
The syntax is kept fairly simple. Here's a basic example of a regular conversation:
<DiscordMessages>
<DiscordMessage>
Hey guys, I'm new here! Glad to be able to join you all!
</DiscordMessage>
<DiscordMessage author="Dawn" avatar="red">
Hi, I'm new here too!
</DiscordMessage>
<DiscordMessage
author="Sanctuary"
avatar="https://i.imgur.com/0TeacfY.png"
roleColor="#0099ff"
>
Hey, <DiscordMention>User</DiscordMention> and{" "}
<DiscordMention>Dawn</DiscordMention>. Welcome to our server!
</DiscordMessage>
</DiscordMessages>
Notes
Default font
By default, this package uses the Google Fonts CDN to pull in the Roboto font. This isn't the same font Discord uses, so if you want to provide your own, you can override the CSS.
/* index.css */
.discord-messages {
font-family: "Your Font", sans-serif;
}
TypeScript module augments
This module uses a custom object on the browser window
for configuration. In order to this you will need to include the following snippet in your source code when working in TypeScript:
declare global {
type DiscordMessageAvatars = {
blue: string;
gray: string;
green: string;
orange: string;
red: string;
} & Record<string, string>;
type DiscordMessageProfile = Partial<{
author: string;
avatar: string;
bot: boolean;
verified: boolean;
roleColor: string;
}>;
interface Window {
$discordMessage: Partial<{
avatars: DiscordMessageAvatars;
profiles: Record<string, DiscordMessageProfile>;
defaultTheme: string;
defaultMode: string;
defaultBackground: "discord" | "none";
}>;
}
}
Avatar shortcuts
The current avatar shortcut strings available are "blue" (default), "gray", "green", "orange", and "red". These shortcuts map to the following image links:
{
"blue": "https://cdn.discordapp.com/attachments/654503812593090602/665721745466195978/blue.png",
"gray": "https://cdn.discordapp.com/attachments/654503812593090602/665721746569166849/gray.png",
"green": "https://cdn.discordapp.com/attachments/654503812593090602/665721748431306753/green.png",
"orange": "https://cdn.discordapp.com/attachments/654503812593090602/665721750201434138/orange.png",
"red": "https://cdn.discordapp.com/attachments/654503812593090602/665721752277483540/red.png"
}
If you want to add to or override the shortcuts, you can set them via window.$discordMessage.avatars
.
window.$discordMessage = {
avatars: {
'default': 'blue'
skyra: 'https://github.com/NM-EEA-Y.png',
djs: require('./assets/discord-avatar-djs.png'),
},
}
Profile shortcuts
Sometimes you'll want to use the same message data across multiple messages. You can do so by providing an object of profiles in window.$discordMessage.profiles
.
window.$discordMessage = {
profiles: {
sanc: {
author: "Sanctuary",
avatar: "https://i.imgur.com/0TeacfY.png",
},
rinon: {
author: "Rinon",
avatar: "https://i.imgur.com/axQ9wJl.png",
bot: true,
roleColor: "#ee82ee",
},
},
};
And then in your React code:
<DiscordMessages>
<DiscordMessage profile="rinon">
Welcome to our server, <mention>Sanctuary</mention>!
</DiscordMessage>
<DiscordMessage profile="sanc">Hey, glad to be here!</DiscordMessage>
</DiscordMessages>
Theming
Each of the components accepts the standard HTML properties for passing styling, such as className
for passing CSS classes (JSS / CSS / SCSS etc.) or style
to pass inline style.
You can also pass your own custom HTML tags, for example set a data-qa
to be able to navigate to the component in your unit tests / end-to-end tests
Components notes
Below are notes for a few certain components. If you want to see what props each component has, check their readme.md file in the respective /src/components/
folder.
DiscordMessages component
This is a wrapper for any child <DiscordMessage>
component. It must be used in order for messages to display properly.
DiscordMention component
If the default slot is left empty, the mention will be rendered as 'User'
, 'Role'
, or 'channel
', depending on the type
prop given.
DiscordEmbed component
An embed that can be attached to the end of your messages. The default slot is used for the embed's description. The footer
slot is used for the footer text.
To ensure the embed gets displayed correctly inside your message, be sure to give it the proper slot
attribute.
<DiscordMessage>
Hi, I'm part of the normal message content.
<DiscordEmbed slot="embeds" color="#0099ff">
Hi, I'm part of the embed message content.
</DiscordEmbed>
</DiscordMessage>
EmbedFields component
A wrapper for any child <discord-embed-field>
components. Must be used in order for fields to display properly. To ensure the embed fields gets displayed correctly inside your embed, be sure to give it the proper slot
attribute.
<DiscordMessage>
<DiscordEmbed slot="embeds">
Hi, I'm part of the embed message content.
<DiscordEmbedFields slot="fields">
<!-- Embed fields go here -->
</DiscordEmbedFields>
</DiscordEmbed>
</DiscordMessage>
EmbedField component
At least 2 consecutive fields need to be marked as inline in order for them to actually display next to each other. The maximum amount of inline fields is 3, and drops to 2 if an embed thumbnail is used.
<DiscordMessage>
<DiscordEmbed slot="embeds">
Hi, I'm part of the embed message content.
<DiscordEmbedFields slot="fields">
<DiscordEmbedField fieldTitle="Inline field" inline>
Field content.
</DiscordEmbedField>
<DiscordEmbedField fieldTitle="Inline field" inline>
Field content.
</DiscordEmbedField>
</DiscordEmbedFields>
</DiscordEmbed>
</DiscordMessage>