-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGoToTop.jsx
More file actions
89 lines (73 loc) · 1.8 KB
/
Copy pathGoToTop.jsx
File metadata and controls
89 lines (73 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import React, { useEffect, useState } from "react";
import styled from "styled-components";
import { FaArrowUp } from "react-icons/fa";
import { ThemeProvider } from 'styled-components';
const GoToTop = () => {
const [isVisible, setIsVisible] = useState(false);
const goToBtn = () => {
window.scrollTo({ top: 0, left: 0, behavior: "smooth" });
};
const listenToScroll = () => {
let heightToHidden = 250;
const winScroll =
document.body.scrollTop || document.documentElement.scrollTop;
if (winScroll > heightToHidden) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};
useEffect(() => {
window.addEventListener("scroll", listenToScroll);
return () => window.removeEventListener("scroll", listenToScroll);
}, []);
return (
<Wrapper>
{isVisible && (
<div className="top-btn" onClick={goToBtn}>
<FaArrowUp className="top-btn--icon" />
</div>
)}
</Wrapper>
);
};
const Wrapper = styled.section `
display: flex;
justify-content: center;
align-items: center;
.top-btn {
font-size: 2.4rem;
width: 4rem;
height: 4rem;
color: #fff;
background-color: #042c54;
box-shadow: ${({ theme }) => theme.colors.shadow};
border-radius: 50%;
position: fixed;
bottom: 5rem;
right: 1rem;
z-index: 999;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
&--icon {
animation: gototop 1.2s linear infinite alternate-reverse;
}
@keyframes gototop {
0% {
transform: translateY(-0.4rem);
}
100% {
transform: translateY(0.5rem);
}
}
}
@media (max-width: ${({ theme }) => theme.media.mobile}) {
.top-btn {
right: 0;
left: 90%;
}
}
`;
export default GoToTop;