52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect } from 'react'
|
|
import styles from './ScrollToTop.module.css'
|
|
|
|
export default function ScrollToTop() {
|
|
const [isVisible, setIsVisible] = useState(false)
|
|
|
|
const toggleVisibility = () => {
|
|
if (window.scrollY > 200) {
|
|
setIsVisible(true)
|
|
} else {
|
|
setIsVisible(false)
|
|
}
|
|
}
|
|
|
|
const scrollToTop = () => {
|
|
window.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth',
|
|
})
|
|
}
|
|
|
|
useEffect(() => {
|
|
window.addEventListener('scroll', toggleVisibility)
|
|
return () => {
|
|
window.removeEventListener('scroll', toggleVisibility)
|
|
}
|
|
}, [])
|
|
|
|
return (
|
|
<button
|
|
type="button"
|
|
onClick={scrollToTop}
|
|
className={`${styles.scrollToTopBtn} ${isVisible ? styles.visible : ''}`}
|
|
aria-label="Scroll to top"
|
|
>
|
|
<svg
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
className={styles.icon}
|
|
>
|
|
<path d="M18 15l-6-6-6 6" />
|
|
</svg>
|
|
</button>
|
|
)
|
|
}
|