40 lines
987 B
TypeScript
40 lines
987 B
TypeScript
import React, { useState, useEffect } from 'react';
|
|
import { ChevronUp } from 'lucide-react';
|
|
|
|
const BackToTop: React.FC = () => {
|
|
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',
|
|
});
|
|
};
|
|
|
|
if (!isVisible) return null;
|
|
|
|
return (
|
|
<button
|
|
onClick={scrollToTop}
|
|
className="fixed bottom-6 right-6 p-3 bg-primary text-primary-foreground rounded-full shadow-lg transition-all duration-300 hover:scale-110 active:scale-95 z-50"
|
|
aria-label="Back to top"
|
|
>
|
|
<ChevronUp className="w-6 h-6" />
|
|
</button>
|
|
);
|
|
};
|
|
|
|
export default BackToTop; |