40 lines
1004 B
TypeScript
40 lines
1004 B
TypeScript
'use client';
|
|
|
|
import { useState, useEffect } from 'react';
|
|
import { ArrowUp } from 'lucide-react';
|
|
import styles from './ScrollToTop.module.css';
|
|
|
|
export default function ScrollToTop() {
|
|
const [isVisible, setIsVisible] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const toggleVisibility = () => {
|
|
if (window.scrollY > 300) {
|
|
setIsVisible(true);
|
|
} else {
|
|
setIsVisible(false);
|
|
}
|
|
};
|
|
|
|
window.addEventListener('scroll', toggleVisibility);
|
|
return () => window.removeEventListener('scroll', toggleVisibility);
|
|
}, []);
|
|
|
|
const scrollToTop = () => {
|
|
window.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth',
|
|
});
|
|
};
|
|
|
|
return (
|
|
<button
|
|
className={`${styles.scrollToTop} ${isVisible ? styles.visible : ''}`}
|
|
onClick={scrollToTop}
|
|
aria-label="Scroll to top"
|
|
>
|
|
<ArrowUp size={24} />
|
|
</button>
|
|
);
|
|
}
|