forked from priyankarpal/projectshut
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrollToTop.tsx
35 lines (31 loc) · 1.08 KB
/
ScrollToTop.tsx
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
"use client"
import React, { useEffect, useState } from 'react';
import { FaArrowUp } from 'react-icons/fa';
function BackToTopButton() {
const [btnVisiblity, setBtnVisiblity] = useState(false);
const handleScroll = () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
};
useEffect(() => {
const toggleVisiblity = () => {
window.scrollY > 250 ? setBtnVisiblity(true) : setBtnVisiblity(false);
};
window.addEventListener('scroll', toggleVisiblity);
return () => {
window.removeEventListener('scroll', toggleVisiblity);
};
}, []);
return (
<button onClick={handleScroll} aria-label="smooth scroll">
<div
className={` ${!btnVisiblity ? 'translate-y-32 md:translate-y-20' : 'translate-y-0'
} fixed bottom-20 md:bottom-10 right-8 z-50 w-10 h-10 rounded-lg bg-primary text-white cursor-pointer flex justify-center items-center transform transition-all delay-100 duration-500 `}
>
<div className="button">
<FaArrowUp />
</div>
</div>
</button>
);
};
export default BackToTopButton;