admin管理员组文章数量:1415645
I have some JSON data in dummyData
. I am not sure how can I place the chat bubbles on left and right according to the direction
. I am using Material UI and context API. Image for the reference. I don't want to use any library other than material UI.
Currently, every chat bubble is positioned to the left. How to position bubbles according to the direction
. Code so far (CodeSandbox):
import React from 'react';
import makeStyles from '@material-ui/core/styles/makeStyles';
const useStyles = makeStyles(theme => ({
container: {
bottom: 0,
position: 'fixed'
},
bubbleContainer: {
width: '100%'
},
bubble: {
border: '0.5px solid black',
borderRadius: '10px',
margin: '5px',
padding: '10px',
display: 'inline-block'
}
}));
const ChatLayout = () => {
const classes = useStyles();
const dummyData = [
{
message: '1: This should be in left',
direction: 'left'
},
{
message: '2: This should be in right',
direction: 'right'
},
{
message: '3: This should be in left again',
direction: 'left'
}
];
const chatBubbles = dummyData.map((obj, i = 0) => (
<div className={classes.bubbleContainer}>
<div key={i++} className={classes.bubble}>
<div className={classes.button}>{obj.message}</div>
</div>
</div>
));
return <div className={classes.container}>{chatBubbles}</div>;
};
export default ChatLayout;
I have some JSON data in dummyData
. I am not sure how can I place the chat bubbles on left and right according to the direction
. I am using Material UI and context API. Image for the reference. I don't want to use any library other than material UI.
Currently, every chat bubble is positioned to the left. How to position bubbles according to the direction
. Code so far (CodeSandbox):
import React from 'react';
import makeStyles from '@material-ui/core/styles/makeStyles';
const useStyles = makeStyles(theme => ({
container: {
bottom: 0,
position: 'fixed'
},
bubbleContainer: {
width: '100%'
},
bubble: {
border: '0.5px solid black',
borderRadius: '10px',
margin: '5px',
padding: '10px',
display: 'inline-block'
}
}));
const ChatLayout = () => {
const classes = useStyles();
const dummyData = [
{
message: '1: This should be in left',
direction: 'left'
},
{
message: '2: This should be in right',
direction: 'right'
},
{
message: '3: This should be in left again',
direction: 'left'
}
];
const chatBubbles = dummyData.map((obj, i = 0) => (
<div className={classes.bubbleContainer}>
<div key={i++} className={classes.bubble}>
<div className={classes.button}>{obj.message}</div>
</div>
</div>
));
return <div className={classes.container}>{chatBubbles}</div>;
};
export default ChatLayout;
Share
Improve this question
asked Sep 30, 2019 at 4:47
mevrickmevrick
1,0454 gold badges21 silver badges31 bronze badges
5
- 2 I have made some css and logical changes in your codesanbox .check updated one : codesandbox.io/s/practical-faraday-zmkdm-updated-zmkdm , zmkdm.csb.app – Kais Commented Sep 30, 2019 at 5:05
- This is perfect! Please make it an answer. How do I align "bubbleContainer" to the bottom? I tried adding "justify-content: flex-end !important" to "bubbleContainer" but doesn't work. – mevrick Commented Sep 30, 2019 at 6:12
- Did you mean you want bubble start from bottom to top? – Kais Commented Sep 30, 2019 at 7:18
- Yes. I had used "bottom: 0, position: 'fixed'" to make it happen. – mevrick Commented Sep 30, 2019 at 7:46
- check now, I have updated style.css – Kais Commented Sep 30, 2019 at 7:52
1 Answer
Reset to default 0To achieve the result you want, you need to use flexbox. Here is the updated code with ments noting added or updated lines (CodeSandbox):
import React from "react";
import makeStyles from "@material-ui/core/styles/makeStyles";
const useStyles = makeStyles(theme => ({
container: {
bottom: 0,
position: "fixed", // added line
display: "flex", // added line
flexDirection: "column" // added line
},
bubbleContainerLeft: {
marginRight: "auto" // updated line
},
bubbleContainerRight: {
marginLeft: "auto" // updated line
},
bubble: {
border: "0.5px solid black",
borderRadius: "10px",
margin: "5px",
padding: "10px",
display: "inline-block"
}
}));
const ChatLayout = () => {
const classes = useStyles();
const dummyData = [
{
message: "1: This should be in left",
direction: "left"
},
{
message: "2: This should be in right",
direction: "right"
},
{
message: "3: This should be in left again",
direction: "left"
}
];
const chatBubbles = dummyData.map((obj, i = 0) => (
// updated line
<div className={obj.direction === "left" ? classes.bubbleContainerLeft : classes.bubbleContainerRight}>
<div key={i++} className={classes.bubble}>
<div className={classes.button}>{obj.message}</div>
</div>
</div>
));
return <div className={classes.container}>{chatBubbles}</div>;
};
export default ChatLayout;
Explanation for the changes:
I set the parent container as a flexbox
.
To move the child to the left, set marginRight
to auto
.
This will make it so this child has the maximum possible right margin, thus making it left-aligned.
We do a similar thing to move the child to the right, but reversed: marginLeft
set to auto
.
本文标签: javascriptHow to make chat like UI with chat bubbles in React JSStack Overflow
版权声明:本文标题:javascript - How to make chat like UI with chat bubbles in React JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745241571a2649357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论