-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customize message bubble background color? #79
Comments
@Hyffer Thank you for the question. The simplest way is to add an inline style or class to the <Message/> component. Adding a class is preferred over style. You can also override scss variables (there are almost 400). Below is the excerpt from a (not yet published) tutorial about the chatscope styling ... Example. Create a class .my-chat-container: .my-chat-container {
background-color: plum;
height: 300px;
} Add it to the like this: <ChatContainer className="my-chat-container"></ChatContainer> The HTML code generated by the component will be as follws: <div class="cs-chat-container my-chat-container"></div> The result of the above HTML code will be a rendering of a container with a background in a beautiful plum color. This way, you can change the background color or margin of the main component element but not only. By taking advantage of CSS inheritance you can also override styles of HTML elements that are deeper in the component structure. Suppose you want to add a counter to the content of every message that has a .counted-message class. /* Counter definition */
body {
counter-reset: message;
}
/* Displaying the counter */
.counted-message .cs-message__html-content::before {
counter-increment: message;
content: counter(message);
font-weight: bold;
margin-right: 1rem;
} const messages = [
{
message: "Hi!",
},
{
message: "What’s up buddy",
},
{
message: "We haven't talked for a long time",
}
];
<>
{messages.map( (model,i) => <Message className="counted-message" key={i} model={model} />)}
</> The result will be something like this: Of course, there is no magic here, it’s just plain CSS inheritance. I’m sure you can handle it. To find the class names of components' internal HTML elements, just use the inspector available in your browser’s dev tools. However, I would like to use css-in-js, how to do it?You are probably already using some sort of css-in-js library in your project. If so, you can do exactly the same as above. If you are using the solution that allows you to create css classes, such as CSS Modules, JSS, Emotion, Linaria, etc, simply create your class and pass it in the className property to the component. You can also use styled components, however, there is a small trap here. Some components check the types of their child components. Inserting a non-allowed component will cause React to complain (red warning on the console). You can read why this is the case in the chatscope documentation here: Changing component type to allow place it in container slot. I hope this helps you a little :) |
I tried as described above, to reload css style of <ChatContainer/>. But encountered a problem. The height of chat container changed to 300px, but the background color did not turn into plum. what I have done: /*color.css*/
.my-chat-container {
background-color: plum;
height: 300px;
} import "@chatscope/chat-ui-kit-styles/dist/default/styles.min.css";
import "./color.css"
<ChatContainer style={chatContainerStyle} className="my-chat-container">
// chatContainerStyle controls visibilility
...
<ChatContainer> packages are updated Am I getting something wrong? |
@Hyffer please check the elements inside the .my-chat-container, probably some of these elements have the background color set. |
I tried using custom styling on the wrapper, but it only changes the background color of the particular message container, not the chat bubble itself. How can one change the color of only the bubble? |
I found by doing .cs-message--outgoing .cs-message__content {
background-color: #0f0 !important;
} You can force the outgoing message color to be what you want, probably not the best solution, I do see people mentioning changing the variable contents in a SCSS file can also solve color issues chatscope/chat-ui-kit-styles#5 |
@WeebNetsu indeed, thank you for the solution |
@mayankjain25 I know using styling library is convenient but the problem is that everyone uses a different css library like emotion, styled components, css modules and so on... |
i am facing some issues in styles ,even i have installed the styles it shows error like i given below: ERROR in ./src/components/HomePage/Homepage.jsx 9:0-122 |
@Testing07102001 How do you import styles? |
thanks for your reply @supersnager . i have a another doubt , the UI is responsive but if the screen size is changed to mobile , the chat container is displaying first which contain a arrow or back button to go back to sidebar where the users list is present . but i want to display the sidebar first ,the when the screen size changed which contains the users . |
I ran into the same problem. What worked for me is conditionally applying styles in an useEffect directly to the cs-message__content div: interface MessageWrapper {
messageModel: MessageModel,
isHighLighted?: boolean,
// Other props
}
const [messages, setMessages] = useState<MessageWrapper[]>([]);
// ... Other code
const applyContentStyles = useCallback(() => {
document.querySelectorAll('.cs-message .cs-message__content').forEach((content, index) => {
const highlightedStyle = 'border: 2px solid #6ea9d7 !important; background-color: #b0d4f1 !important;';
const currentStyle = content.getAttribute('style');
if (messages[index].isHighLighted) {
if (currentStyle !== highlightedStyle) {
content.setAttribute('style', highlightedStyle);
}
} else {
if (currentStyle) {
content.setAttribute('style', '');
}
}
});
}, [messages]);
useEffect(() => {
applyContentStyles();
}, [messages, applyContentStyles]); |
In your chat component:
|
Refer to documentation, there is no props to change the color of message bubble.
So if i want to implement this feature, which way is better? loading custom css file or using style tag in javascript? And is there some basic examples?
I know little about those front-end things. But i think it can be done by adding a param to
Message
orMessageGroup
component. like:The text was updated successfully, but these errors were encountered: