Skip to content

Commit 8492880

Browse files
author
unknown
committed
feat: Add scroll-to-top button component (#340)
1 parent 72d63b0 commit 8492880

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"use client"
2+
3+
import { useEffect, useState } from 'react'
4+
import { ArrowUp } from 'lucide-react'
5+
6+
export default function ScrollToTop() {
7+
const [isVisible, setIsVisible] = useState(false)
8+
9+
useEffect(() => {
10+
const toggleVisibility = () => {
11+
if (window.pageYOffset > 300) {
12+
setIsVisible(true)
13+
} else {
14+
setIsVisible(false)
15+
}
16+
}
17+
18+
window.addEventListener('scroll', toggleVisibility)
19+
return () => window.removeEventListener('scroll', toggleVisibility)
20+
}, [])
21+
22+
const scrollToTop = () => {
23+
window.scrollTo({
24+
top: 0,
25+
behavior: 'smooth'
26+
})
27+
}
28+
29+
return (
30+
<>
31+
{isVisible && (
32+
<button
33+
onClick={scrollToTop}
34+
aria-label="Scroll to top"
35+
className="fixed bottom-6 right-6 z-50 flex items-center justify-center w-12 h-12 bg-indigo-600 text-white rounded-full shadow-lg transition-all duration-300 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 cursor-pointer"
36+
tabIndex={0}
37+
>
38+
<ArrowUp className="w-6 h-6" />
39+
</button>
40+
)}
41+
</>
42+
)
43+
}

0 commit comments

Comments
 (0)