Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
feat: add scroll to top button (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
usmanfaki authored Dec 22, 2023
1 parent 5e0ca78 commit d42900e
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 35 deletions.
139 changes: 104 additions & 35 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.4.2",
"@fortawesome/free-brands-svg-icons": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.5.1",
"@fortawesome/react-fontawesome": "^0.2.0",
"@headlessui/react": "^1.7.17",
"@headlessui/tailwindcss": "^0.2.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PrimaryFeatures } from '@/components/PrimaryFeatures'
import { SecondaryFeatures } from '@/components/SecondaryFeatures'
import { Testimonials } from '@/components/Testimonials'
import { Founders } from '@/components/Founders'
import { ScrollToTop } from '@/components/ScrollToTop'

export default function Home() {
return (
Expand All @@ -24,6 +25,7 @@ export default function Home() {
<Faqs />
</main>
<Footer />
<ScrollToTop/>
</>
)
}
38 changes: 38 additions & 0 deletions src/components/ScrollToTop.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use client';
import { Button } from "./Button";
import {useState, useEffect} from "react"
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faArrowUp } from "@fortawesome/free-solid-svg-icons";
export function ScrollToTop(){
const [isVisible, setIsVisible] = useState(false)

const toggleVisibility = ()=> {
if (window.scrollY > 300){
setIsVisible(true)
}
else{
setIsVisible(false)
}
}
const scrollToTop = () =>{
window.scrollTo({
top:0,
behavior:"smooth",
})
}

useEffect(() => {
window.addEventListener("scroll", toggleVisibility)
return () => {
window.removeEventListener("scroll", toggleVisibility)
}
}, [])
return(
<div className="fixed bottom-2 right-2">
<Button className={isVisible ? 'opacity-100' : 'opacity-0'} variant={"solid"} onClick={() => scrollToTop()}>
<FontAwesomeIcon icon={faArrowUp} color="white" size="xl" className="h-6 w-6" aria-hidden="true"/>
</Button>
</div>
)

}

0 comments on commit d42900e

Please sign in to comment.