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 up button added #69

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import React from "react";
import { Route, Routes } from "react-router-dom";
import Home from "./pages/Home";
import Books from "./pages/Books";
import Contact from "./pages/Contact";
import About from "./pages/About";
import PageNotFound from "./pages/PageNotFound";
import ScrollToTop from "./components/ScrollToTopButton"; // Import the ScrollToTop component
import SingleBook from "./pages/SingleBook.jsx";
import Login from "./pages/Login.jsx";

function App() {
return (
<>
Expand All @@ -19,6 +20,7 @@ function App() {
<Route path="/login" element={<Login />} />
<Route path="*" element={<PageNotFound />} />
</Routes>
<ScrollToTop /> {/* Add the ScrollToTop component */}
</>
);
}
Expand Down
18 changes: 18 additions & 0 deletions src/assets/styles/ScrollToTopButton.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.scroll-to-top {
display: none;
position: fixed;
bottom: 20px;
right: 20px;
background-color: #00aaff;
color: #fff;
border: none;
border-radius: 50%;
padding: 20px 25px;
font-size: 16px;
cursor: pointer;
}

.scroll-to-top.visible {
display: block;
}

1 change: 0 additions & 1 deletion src/components/HomeSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const HomeSearch = () => {
return (
<>
<div className="home-search">

<div className="left-side">
<h1>Your Gateway to a Universe of Books</h1>
<p>Explore a vast collection of books</p>
Expand Down
46 changes: 46 additions & 0 deletions src/components/ScrollToTopButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { useState, useEffect } from "react";
import '../assets/styles/ScrollToTopButton.css'
import { useLocation } from "react-router-dom";

const ScrollToTop = () => {
const location = useLocation();
const [isVisible, setIsVisible] = useState(false);

const toggleVisibility = () => {
if (window.pageYOffset > 300) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};

const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: "smooth",
});
};

useEffect(() => {
window.addEventListener("scroll", toggleVisibility);

return () => {
window.removeEventListener("scroll", toggleVisibility);
};
}, []);

useEffect(() => {
setIsVisible(false); // Hide the button when navigating to a new route
}, [location]);

return (
<button
className={`scroll-to-top ${isVisible ? "visible" : ""}`}
onClick={scrollToTop}
>
<i class="fa-solid fa-angle-up"></i>
</button>
);
};

export default ScrollToTop;