Skip to content

Commit

Permalink
feat: scroll to top button (#944)
Browse files Browse the repository at this point in the history
Co-authored-by: Tamal Das <tamalcodes@gmail.com>
  • Loading branch information
karthiknadar1204 and tamalCodes committed Jul 30, 2023
1 parent 389a288 commit 4bc2c5a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import UserForgotpassword from "./pages/user/UserForgotpassword";
import "./styles/App.css";
import ShopCategory from "./pages/shops/ShopCategory";
import Error404 from "./pages/Error404";
import BacktoTop from "../src/components/Button/BacktoTop/BacktoTop.jsx";

const App = () => {
return (
Expand Down Expand Up @@ -74,6 +75,7 @@ const App = () => {
<Route path={"/*"} element={<Error404 />} />
</Routes>
</Router>
<BacktoTop />
</MilanState>
</>
);
Expand Down
28 changes: 28 additions & 0 deletions src/components/Button/BacktoTop/BacktoTop.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.wrapper {
display: flex;
justify-content: center;
align-items: center;
}

.top-btn {
font-size: 2.4rem;
width: 2.5rem;
height: 2.5rem;
color: black;
background-color: #e76f51;
border-radius: 10px;
position: fixed;
bottom: 5rem;
right: 5rem;
z-index: 999;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
transition: background-color 0.3s;
}

.top-btn:hover {
background-color: transparent;
border: 2px solid #e76f51;
}
43 changes: 43 additions & 0 deletions src/components/Button/BacktoTop/BacktoTop.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { useState } from "react";
import "../BacktoTop/BacktoTop.css";
import { BsArrowUpShort } from "react-icons/bs";
import { useEffect } from "react";

const GoToTop = () => {
const [isVisible, setIsVisible] = useState(false);

const goToBtn = () => {
window.scrollTo({ top: 0, left: 0, behavior: "smooth" });
return () => window.removeEventListener("scroll", listenToScroll);
};

const listenToScroll = () => {
let heightToHidden = 250;
const winScroll =
document.body.scrollTop || document.documentElement.scrollTop;
if (winScroll > heightToHidden) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};

useEffect(() => {
window.addEventListener("scroll", listenToScroll);
}, []);

return (
<div className="wrapper">
{isVisible && (
<div className="top-btn" onClick={goToBtn}>
<div className="icon-container">
{" "}
<BsArrowUpShort className="icon" />
</div>
</div>
)}
</div>
);
};

export default GoToTop;

0 comments on commit 4bc2c5a

Please sign in to comment.