generated from iqbalpa/nextjs-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use client'; | ||
|
||
import React, { useEffect, useState } from 'react'; | ||
import { CircleChevronUp } from 'lucide-react'; | ||
|
||
const GoTopButton: React.FC = () => { | ||
const [showGoTop, setShowGoTop] = useState<boolean>(false); | ||
|
||
const handleVisibleButton = () => { | ||
const position = window.scrollY; | ||
if (position > 50) { | ||
setShowGoTop(true); | ||
} else { | ||
setShowGoTop(false); | ||
} | ||
}; | ||
|
||
const handleClick = () => { | ||
window.scrollTo({ top: 0, behavior: 'smooth' }); | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener('scroll', handleVisibleButton); | ||
return () => { | ||
window.removeEventListener('scroll', handleVisibleButton); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
showGoTop && ( | ||
<button | ||
onClick={handleClick} | ||
className="fixed bottom-10 right-10 rounded-full bg-slate-600 bg-opacity-80 p-2 text-white duration-100 hover:scale-105 hover:cursor-pointer hover:bg-slate-500 hover:bg-opacity-95" | ||
> | ||
<CircleChevronUp size={32} /> | ||
</button> | ||
) | ||
); | ||
}; | ||
|
||
export default GoTopButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters