Skip to content

Commit 3eb3b85

Browse files
committed
Change sending animation, fix rerender, clean-up
*Change sending animation from keyframes to react-spring *Fix rerender component to display sending animation correctly by wrap component by React.memo *Remove unnused scss lines, change name of svg component
1 parent 8120d3d commit 3eb3b85

File tree

2 files changed

+42
-70
lines changed

2 files changed

+42
-70
lines changed
Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,59 @@
11
import React from "react";
22
import { useSelector } from "react-redux";
3+
import { useTransition, animated } from "react-spring";
34

45
// Assets
5-
import { ReactComponent as Plane } from "../../../assets/paper-plane.svg";
6+
import { ReactComponent as PlaneSVG } from "../../../assets/paper-plane.svg";
7+
8+
// Components
69
import UserImg from "../../UserImg/UserImg";
710

811
// Style
912
import classes from "./Message.module.scss";
1013

1114
function Message({ content, senderId, isSending, avatar }) {
1215
const userId = useSelector((state) => state.auth.user._id);
16+
const transitions = useTransition(isSending, null, {
17+
from: {
18+
opacity: 0,
19+
transform: "translate(-5px, -10px)",
20+
},
21+
enter: {
22+
opacity: 1,
23+
width: "15px",
24+
transform: "translateX(0px, 0px)",
25+
},
26+
leave: {
27+
width: "0px",
28+
transform: "translate(20px, -30px)",
29+
},
30+
config: {
31+
duration: 150,
32+
},
33+
trail: 1000,
34+
});
1335

1436
return (
1537
<li
1638
className={`${classes.Message} ${
17-
userId === senderId ? classes.MessageSender : null
39+
userId === senderId ? classes.MessageSender : ""
1840
}`}
1941
>
2042
{userId !== senderId && <UserImg avatarUrl={avatar} small />}
2143
<p className={classes.Content}>{content}</p>
22-
{isSending && (
23-
<div
24-
className={`${classes.BoxIcon} ${
25-
!isSending && classes.BoxIconAnimate
26-
}`}
27-
>
28-
<Plane
29-
className={`${classes.sendingIcon} ${
30-
!isSending && classes.sendingIconAnimate
31-
}`}
32-
/>
33-
</div>
34-
)}
44+
{transitions.map(({ item, key, props }) => {
45+
return (
46+
item && (
47+
<animated.div key={key} style={props}>
48+
<div className={`${classes.BoxSendingIcon}`}>
49+
<PlaneSVG className={`${classes.SendingIcon}`} />
50+
</div>
51+
</animated.div>
52+
)
53+
);
54+
})}
3555
</li>
3656
);
3757
}
3858

39-
export default Message;
59+
export default React.memo(Message);

src/components/Messages/Message/Message.module.scss

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
align-items: flex-end;
55
margin-bottom: 1rem;
66
transition: all 0.3s;
7+
overflow: hidden;
78

89
&Sender {
910
grid-column: 3 / 5;
1011
justify-self: end;
11-
12-
& .MessageContent {
13-
background-color: green;
14-
}
1512
}
1613
}
1714

@@ -21,62 +18,17 @@
2118
font-weight: 500;
2219
border-radius: 1.8rem;
2320
padding: 0.9rem;
24-
// white-space: initial;
2521
white-space: pre-wrap;
2622
overflow: hidden;
2723
word-break: break-all;
2824
}
2925

30-
.sendingIcon {
31-
height: 1.5rem;
32-
fill: var(--c-font);
33-
position: relative;
34-
35-
&Animate {
36-
animation-duration: 0.6s;
37-
animation-name: flyAway;
38-
}
39-
}
40-
41-
.BoxIcon {
26+
.BoxSendingIcon {
4227
overflow: hidden;
43-
44-
&Animate {
45-
animation-duration: 0.6s;
46-
animation-name: shrink;
47-
}
4828
}
4929

50-
@keyframes flyAway {
51-
0% {
52-
transform: translate(0, 0);
53-
}
54-
55-
100% {
56-
transform: translate(3rem, -3rem);
57-
}
58-
}
59-
60-
@keyframes shrink {
61-
0% {
62-
width: 1rem;
63-
display: auto;
64-
}
65-
66-
100% {
67-
width: 0;
68-
display: none;
69-
}
70-
}
71-
72-
@keyframes slideIn {
73-
0% {
74-
opacity: 0;
75-
transform: scale(0);
76-
}
77-
78-
100% {
79-
opacity: 1;
80-
transform: scale(1);
81-
}
30+
.SendingIcon {
31+
height: 1.5rem;
32+
fill: var(--c-font);
33+
position: relative;
8234
}

0 commit comments

Comments
 (0)