'use client';
import { useEffect, useState } from 'react';
import { useTheme } from 'next-themes';
import { cn } from '@/lib/utils';
export default function ScrollButton() {
const [showScrollButton, setShowScrollButton] = useState(false);
const { theme } = useTheme();
useEffect(() => {
const handleScroll = () => {
if (window.pageYOffset > 300) {
setShowScrollButton(true);
} else {
setShowScrollButton(false);
}
};
window.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
};
}, []);
const scrollToTop = () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
};
return (
<>
{showScrollButton && (
<button
className={cn(
theme === 'dark'
? 'bg-gray-200 text-gray-800'
: 'bg-gray-800 text-white',
'fixed bottom-4 right-4 p-2 rounded-full print:hidden'
)}
onClick={scrollToTop}
>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
strokeWidth={1.5}
stroke="currentColor"
className="w-6 h-6"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
d="M4.5 15.75l7.5-7.5 7.5 7.5"
/>
</svg>
</button>
)}
</>
);
}