Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: scroll to top button #944

Merged
merged 9 commits into from
Jul 30, 2023
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;
Loading