|
1 | | -import React, { useEffect, useState } from 'react'; |
2 | | -import Image from 'next/image'; |
| 1 | +/* eslint-disable react/react-in-jsx-scope */ |
| 2 | +import { useEffect, useState } from 'react'; |
| 3 | +import { Button } from '~/components/ui/button'; |
| 4 | +import { ArrowUp } from 'lucide-react'; |
3 | 5 |
|
4 | 6 | export default function ScrollButton() { |
5 | | - const [backToTopButton, setBackToTopButton] = useState(false); |
| 7 | + const [showButton, setShowButton] = useState(false); |
6 | 8 |
|
7 | 9 | useEffect(() => { |
8 | 10 | const handleScroll = () => { |
9 | | - // Check the scroll position |
10 | | - setBackToTopButton(window.scrollY > 150); |
| 11 | + if (typeof window !== 'undefined') { |
| 12 | + setShowButton(window.scrollY > 150); |
| 13 | + } |
11 | 14 | }; |
12 | 15 |
|
13 | | - // Add scroll event listener to window |
14 | | - window.addEventListener('scroll', handleScroll); |
15 | | - |
16 | | - // Cleanup function to remove the event listener when the component unmounts |
17 | | - /* istanbul ignore next : can't test cleanup function */ |
18 | | - return () => window.removeEventListener('scroll', handleScroll); |
| 16 | + if (typeof window !== 'undefined') { |
| 17 | + window.addEventListener('scroll', handleScroll); |
| 18 | + // Initial check |
| 19 | + handleScroll(); |
| 20 | + return () => window.removeEventListener('scroll', handleScroll); |
| 21 | + } |
19 | 22 | }, []); |
20 | 23 |
|
21 | 24 | const scrollUp = () => { |
22 | | - window.scrollTo({ |
23 | | - top: 1, |
24 | | - left: 0, |
25 | | - }); |
| 25 | + if (typeof window !== 'undefined') { |
| 26 | + window.scrollTo({ top: 0, left: 0, behavior: 'smooth' }); |
| 27 | + } |
26 | 28 | }; |
27 | 29 |
|
28 | 30 | return ( |
29 | | - <div className='fixed bottom-14 right-4 h-16 w-12 z-40'> |
30 | | - {backToTopButton && ( |
31 | | - <button |
| 31 | + <div className='fixed bottom-14 right-4 h-12 w-12 z-40'> |
| 32 | + {showButton && ( |
| 33 | + <Button |
32 | 34 | onClick={scrollUp} |
33 | | - className='rounded-full transition-transform hover:-translate-y-2 duration-150 ease-in-out shadow-lg bg-white flex items-center justify-center' |
| 35 | + variant='outline' |
| 36 | + size='icon' |
| 37 | + className='rounded-full transition-all duration-200 ease-in-out shadow-lg |
| 38 | + bg-white dark:bg-gray-800 border-gray-200 dark:border-gray-600 |
| 39 | + hover:bg-gray-50 dark:hover:bg-gray-700 hover:-translate-y-1 |
| 40 | + flex items-center justify-center h-full w-full' |
34 | 41 | aria-label='Scroll to top' |
35 | 42 | data-test='scroll-button' |
36 | 43 | > |
37 | | - {/* Ensure the image is in your public/img directory */} |
38 | | - <Image |
39 | | - alt='Scroll to top' |
40 | | - width={40} |
41 | | - height={40} |
42 | | - src='/img/scroll.svg' |
43 | | - /> |
44 | | - </button> |
| 44 | + <ArrowUp className='h-6 w-6 text-gray-700 dark:text-gray-300' /> |
| 45 | + </Button> |
45 | 46 | )} |
46 | 47 | </div> |
47 | 48 | ); |
|
0 commit comments