-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathscroll-to-top.jsx
85 lines (74 loc) · 2.21 KB
/
scroll-to-top.jsx
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
import React, { useState, useEffect } from "react"
const ScrollToTopButton = () => {
const [isVisible, setIsVisible] = useState(false)
const [hideTimeout, setHideTimeout] = useState(null)
const styles = {
button: {
position: "fixed",
bottom: "20px",
right: "20px",
padding: "10px 20px",
fontSize: "18px",
backgroundColor: "rgba(125, 132, 146, 0.8)",
color: "#fff",
border: "2px solid rgba(125, 132, 146, 0.8)",
borderRadius: "5px",
cursor: "pointer",
boxShadow: "0px 4px 6px rgba(0, 0, 0, 0.1), 0 0 15px rgba(125, 132, 146, 0.6)",
transition: "opacity 0.5s ease, box-shadow 0.3s ease",
opacity: isVisible ? 1 : 0,
pointerEvents: isVisible ? "auto" : "none",
},
icon: {
width: "20px",
height: "20px",
fill: "#fff",
},
}
const toggleVisibility = () => {
if (window.scrollY > 300) {
setIsVisible(true)
if (hideTimeout) {
clearTimeout(hideTimeout)
}
const timeout = setTimeout(() => {
setIsVisible(false)
}, 3000)
setHideTimeout(timeout)
} else {
setIsVisible(false)
}
}
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
})
}
useEffect(() => {
window.addEventListener("scroll", toggleVisibility)
return () => {
window.removeEventListener("scroll", toggleVisibility)
if (hideTimeout) {
clearTimeout(hideTimeout)
}
}
}, [hideTimeout])
return (
<div>
{isVisible && (
<button
onClick={scrollToTop}
style={styles.button}
title="Go to top"
aria-label="Go to top"
>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" style={styles.icon} aria-hidden="true">
<path d="M 17.418 14.908 C 17.69 15.176 18.127 15.176 18.397 14.908 C 18.667 14.64 18.668 14.207 18.397 13.939 L 10.489 6.109 C 10.219 5.841 9.782 5.841 9.51 6.109 L 1.602 13.939 C 1.332 14.207 1.332 14.64 1.602 14.908 C 1.873 15.176 2.311 15.176 2.581 14.908 L 10 7.767 L 17.418 14.908 Z"></path>
</svg>
</button>
)}
</div>
)
}
export default ScrollToTopButton