Skip to content

Commit

Permalink
Merge pull request #149 from AsmitaMishra24/main2
Browse files Browse the repository at this point in the history
feat: Added Back-To-Top Button
  • Loading branch information
dvjsharma authored Jul 29, 2024
2 parents dc795ff + 7d58342 commit 9a33a50
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"preview": "vite preview"
},
"dependencies": {
"@fortawesome/fontawesome-free": "^6.6.0",
"@reduxjs/toolkit": "^1.9.6",
"axios": "^1.6.2",
"hamburger-react": "^2.5.0",
Expand All @@ -21,6 +22,7 @@
"react-razorpay": "^2.0.1",
"react-redux": "^8.1.2",
"react-router-dom": "^6.16.0",
"react-scroll": "^1.9.0",
"redux-persist": "^6.0.0"
},
"devDependencies": {
Expand Down
45 changes: 45 additions & 0 deletions client/src/components/BackToTop/BackToTop.jsx
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;
2 changes: 2 additions & 0 deletions client/src/components/layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Navbar from "./Navbar/navbar";
import { Outlet } from "react-router-dom";
import Footer from "./Footer";
import Preloader from "./Preloader/Preloader";
import BackToTop from "./BackToTop/BackToTop";

const Layout = () => {
return (
Expand All @@ -13,6 +14,7 @@ const Layout = () => {
</div>
<Footer />
<Preloader />
<BackToTop/>
</>
);
};
Expand Down

0 comments on commit 9a33a50

Please sign in to comment.