57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
export function ScrollToTop() {
|
|
const [isVisible, setIsVisible] = useState(false);
|
|
|
|
// Show button when page is scrolled down
|
|
useEffect(() => {
|
|
const toggleVisibility = () => {
|
|
if (window.pageYOffset > 300) {
|
|
setIsVisible(true);
|
|
} else {
|
|
setIsVisible(false);
|
|
}
|
|
};
|
|
|
|
window.addEventListener('scroll', toggleVisibility);
|
|
|
|
return () => window.removeEventListener('scroll', toggleVisibility);
|
|
}, []);
|
|
|
|
const scrollToTop = () => {
|
|
window.scrollTo({
|
|
top: 0,
|
|
behavior: 'smooth',
|
|
});
|
|
};
|
|
|
|
return (
|
|
<>
|
|
{isVisible && (
|
|
<button
|
|
onClick={scrollToTop}
|
|
className="fixed bottom-8 right-8 z-50 p-3 bg-primary-600 hover:bg-primary-700 text-white rounded-full shadow-lg transition-all duration-300 hover:scale-110 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2"
|
|
aria-label="Scroll to top"
|
|
>
|
|
<svg
|
|
className="w-6 h-6"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
viewBox="0 0 24 24"
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
>
|
|
<path
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
strokeWidth={2}
|
|
d="M5 10l7-7m0 0l7 7m-7-7v18"
|
|
/>
|
|
</svg>
|
|
</button>
|
|
)}
|
|
</>
|
|
);
|
|
}
|