forked from Aditya-Karmalkar/RAKTDAAN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollToTopButton.tsx
More file actions
38 lines (31 loc) · 921 Bytes
/
ScrollToTopButton.tsx
File metadata and controls
38 lines (31 loc) · 921 Bytes
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
import { useState, useEffect } from "react";
import { ArrowUp } from "lucide-react";
const ScrollToTopButton: React.FC = () => {
const [visible, setVisible] = useState<boolean>(false);
useEffect(() => {
const toggleVisibility = () => {
setVisible(window.scrollY > 300);
};
window.addEventListener("scroll", toggleVisibility);
return () => {
window.removeEventListener("scroll", toggleVisibility);
};
}, []);
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth"
});
};
if (!visible) return null;
return (
<button
onClick={scrollToTop}
className="fixed bottom-6 right-6 bg-red-600 hover:bg-red-700 text-white p-3 rounded-full shadow-lg z-50 transition"
aria-label="Scroll to top"
>
<ArrowUp size={20} />
</button>
);
};
export default ScrollToTopButton;