Skip to content

Commit 63dbd78

Browse files
Added back to top button
1 parent d7ae94a commit 63dbd78

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@testing-library/user-event": "^13.5.0",
99
"react": "^18.3.1",
1010
"react-dom": "^18.3.1",
11+
"react-icons": "^5.5.0",
1112
"react-router-dom": "^7.3.0",
1213
"react-scripts": "5.0.1",
1314
"swr": "^2.3.3",

‎src/App.jsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import NotFound from './components/NotFound.jsx';
88
import './App.css';
99
import ReadingMode from './components/ReadingMode.jsx';
1010
import { useGlobalContext } from './Context.jsx';
11+
import BackToTopButton from './BackToTop.jsx';
1112

1213
const Home = lazy(() => import('./pages/Home.jsx'));
1314

@@ -77,6 +78,7 @@ export const App = () => {
7778

7879
</ReadingMode>
7980
</Suspense>
81+
<BackToTopButton />
8082
</Router>
8183
);
8284
}

‎src/BackToTop.css‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.back-to-top {
2+
position: fixed;
3+
bottom: 30px;
4+
right: 30px;
5+
background-color: #007BFF;
6+
color: white;
7+
border: none;
8+
padding: 15px 20px;
9+
border-radius: 50%;
10+
font-size: 18px;
11+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
12+
cursor: pointer;
13+
z-index: 999;
14+
transition: background-color 0.3s ease-in-out;
15+
}
16+
17+
.back-to-top:hover {
18+
background-color: #0056b3;
19+
}
20+

‎src/BackToTop.jsx‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React, { useState, useEffect } from "react";
2+
import { FaArrowUp } from "react-icons/fa";
3+
import './BackToTop.css';
4+
5+
const BackToTopButton = () => {
6+
const [isVisible, setIsVisible] = useState(false);
7+
8+
useEffect(() => {
9+
const handleScroll = () => {
10+
setIsVisible(window.pageYOffset > 300);
11+
};
12+
window.addEventListener("scroll", handleScroll);
13+
return () => window.removeEventListener("scroll", handleScroll);
14+
}, []);
15+
16+
const scrollToTop = () => {
17+
window.scrollTo({ top: 0, behavior: "smooth" });
18+
};
19+
20+
return (
21+
isVisible && (
22+
<button className="back-to-top" onClick={scrollToTop} aria-label="Back to top">
23+
<FaArrowUp />
24+
</button>
25+
)
26+
);
27+
};
28+
29+
export default BackToTopButton;

‎src/reducer.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,5 @@ const reducer = (state, action) => {
5454
};
5555

5656
export default reducer;
57+
58+

0 commit comments

Comments
 (0)