-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
18 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,48 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { FaArrowUp } from "react-icons/fa"; | ||
import React, { useEffect, useState } from "react" | ||
import { FaArrowUp } from "react-icons/fa" | ||
|
||
const GoToTop = () => { | ||
// state to display toggler | ||
const [isVisible, setIsVisible] = useState(false); | ||
const [isVisible, setIsVisible] = useState(false) | ||
|
||
const goToBtn = () => { | ||
window.scrollTo({ top: 0, left: 0, behavior: "smooth" }); | ||
}; | ||
window.scrollTo({ top: 0, left: 0, behavior: "smooth" }) | ||
} | ||
|
||
const listenToScroll = () => { | ||
const heightToHidden = 20; | ||
const heightToHidden = 20 | ||
const winScroll = | ||
document.body.scrollTop || document.documentElement.scrollTop; | ||
document.body.scrollTop || document.documentElement.scrollTop | ||
|
||
if (winScroll > heightToHidden) { | ||
setIsVisible(true); | ||
setIsVisible(true) | ||
} else { | ||
setIsVisible(false); | ||
setIsVisible(false) | ||
} | ||
}; | ||
} | ||
|
||
useEffect(() => { | ||
window.addEventListener("scroll", listenToScroll); | ||
return () => window.removeEventListener("scroll", listenToScroll); | ||
}, []); | ||
window.addEventListener("scroll", listenToScroll) | ||
return () => window.removeEventListener("scroll", listenToScroll) | ||
}, []) | ||
|
||
// classes for Back to Top button | ||
// classes for gototop button | ||
|
||
return ( | ||
<div | ||
onClick={goToBtn} | ||
onKeyDown={goToBtn} | ||
role="button" | ||
tabIndex={0} | ||
className={`fixed bg-gradient-to-b from-green-400 to-blue-600 animate-bounce cursor-pointer rounded-full p-3 right-12 bottom-12 ${ | ||
!isVisible ? "hidden" : "" | ||
}`} | ||
className={`fixed bg-gradient-to-b from-green-400 to-blue-600 animate-bounce cursor-pointer rounded-full p-3 right-12 bottom-12 ${!isVisible ? "hidden" : ""}`} | ||
> | ||
{isVisible && ( | ||
<div> | ||
<FaArrowUp className="text-black" /> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
) | ||
} | ||
|
||
export default GoToTop; | ||
export default GoToTop |