forked from Movalabs-crew/mova-store
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollToTop.jsx
More file actions
41 lines (36 loc) · 1 KB
/
Copy pathScrollToTop.jsx
File metadata and controls
41 lines (36 loc) · 1 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
"use client"
import { useState, useEffect } from "react";
import { FaAngleDoubleUp } from "react-icons/fa";
const ScrollToTop = () => {
const [isVisible, setIsVisible] = useState(false);
useEffect(() => {
const toggleVisibility = () => {
if (window.pageYOffset > 300) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};
window.addEventListener("scroll", toggleVisibility);
return () => window.removeEventListener("scroll", toggleVisibility);
}, []);
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
};
return (
<div className="fixed bottom-4 right-4">
{isVisible && (
<button
onClick={scrollToTop}
className="p-2 rounded-full bg-purple-600 text-white shadow-md hover:bg-purple-700 transition-all duration-300"
>
<FaAngleDoubleUp size={24} className="hover:animate-bounce"/>
</button>
)}
</div>
);
};
export default ScrollToTop;