-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from AsmitaMishra24/main2
feat: Added Back-To-Top Button
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { animateScroll } from 'react-scroll'; | ||
import '@fortawesome/fontawesome-free/css/all.min.css'; | ||
|
||
const BackToTop = () => { | ||
const [showButton, setShowButton] = useState(false); | ||
|
||
useEffect(() => { | ||
const handleScroll = () => { | ||
if (window.scrollY > 90) { | ||
setShowButton(true); | ||
} else { | ||
setShowButton(false); | ||
} | ||
}; | ||
|
||
window.addEventListener("scroll", handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener("scroll", handleScroll); | ||
}; | ||
}, []); | ||
|
||
const handleClick = () => { | ||
animateScroll.scrollToTop({ | ||
top: 0, | ||
behavior: "smooth", | ||
duration: 200 | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<button | ||
className={`fixed bottom-5 right-5 z-50 bg-black text-white border-2 border-black p-2 text-sm cursor-pointer transition-all duration-300 rounded-full flex items-center justify-center w-10 h-10 | ||
${showButton ? 'opacity-100' : 'opacity-0'} hover:bg-white hover:text-black hover:border-black focus:outline-none active:translate-y-1`} | ||
onClick={handleClick} | ||
> | ||
<i className="fas fa-arrow-up text-xl"></i> | ||
</button> | ||
</> | ||
); | ||
}; | ||
|
||
export default BackToTop; |
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