33 lines
912 B
TypeScript
33 lines
912 B
TypeScript
import { useState, useEffect } from 'react'
|
|
|
|
export default function ScrollToTop() {
|
|
const [isVisible, setIsVisible] = useState(false)
|
|
|
|
useEffect(() => {
|
|
const handleScroll = () => {
|
|
setIsVisible(window.scrollY > 200)
|
|
}
|
|
window.addEventListener('scroll', handleScroll)
|
|
return () => window.removeEventListener('scroll', handleScroll)
|
|
}, [])
|
|
|
|
const scrollToTop = () => {
|
|
window.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth',
|
|
})
|
|
}
|
|
|
|
return (
|
|
<button
|
|
onClick={scrollToTop}
|
|
className={`fixed bottom-4 right-4 w-10 h-10 bg-secondary hover:bg-secondary/80 text-white rounded shadow-lg flex items-center justify-center transition-all duration-300 z-50 ${
|
|
isVisible ? 'opacity-100 translate-y-0' : 'opacity-0 translate-y-4 pointer-events-none'
|
|
}`}
|
|
aria-label="Back to top"
|
|
>
|
|
<span className="fa fa-angle-up" />
|
|
</button>
|
|
)
|
|
}
|