Skip to content

Commit e28f7b5

Browse files
authored
Merge pull request #37 from jc-kirthi/main
Navigation : Scroll-to-Top & Bottom Button Integration to all pages
2 parents acae0c2 + 4f232ef commit e28f7b5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/App.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { SubmitChallenge } from './components/Submit/SubmitChallenge';
1010
import { Community } from './components/Community/Community';
1111
import { Challenge } from './components/Challenges/ChallengeCard';
1212
import { ThemeProvider } from './themeContent'; // Import the new ThemeProvider
13+
import ScrollToTop from './components/ScrollToTop/ScrollToTop';
14+
1315

1416
function App() {
1517
const [currentPage, setCurrentPage] = useState<string>('home');
@@ -119,6 +121,7 @@ function App() {
119121
</div>
120122
</footer>
121123
)}
124+
<ScrollToTop />
122125
</div>
123126
</ThemeProvider>
124127
);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { useEffect, useState } from "react";
2+
import { ArrowUp, ArrowDown } from "lucide-react";
3+
4+
const ScrollButton: React.FC = () => {
5+
const [atTop, setAtTop] = useState(true);
6+
const [atBottom, setAtBottom] = useState(false);
7+
8+
useEffect(() => {
9+
const handleScroll = () => {
10+
const scrollY = window.scrollY;
11+
const scrollHeight = document.documentElement.scrollHeight;
12+
const windowHeight = window.innerHeight;
13+
14+
setAtTop(scrollY < 100); // near top
15+
setAtBottom(scrollY + windowHeight >= scrollHeight - 100); // near bottom
16+
};
17+
18+
window.addEventListener("scroll", handleScroll);
19+
handleScroll(); // run on mount
20+
21+
return () => window.removeEventListener("scroll", handleScroll);
22+
}, []);
23+
24+
const scrollToTop = () => {
25+
window.scrollTo({ top: 0, behavior: "smooth" });
26+
};
27+
28+
const scrollToBottom = () => {
29+
window.scrollTo({ top: document.documentElement.scrollHeight, behavior: "smooth" });
30+
};
31+
32+
return (
33+
<button
34+
onClick={atBottom ? scrollToTop : scrollToBottom}
35+
className={`fixed bottom-6 right-6 p-3 rounded-full shadow-lg transition-all duration-300
36+
bg-gradient-to-r from-purple-600 to-blue-600 text-white hover:opacity-90
37+
${atTop && atBottom ? "opacity-0 pointer-events-none" : "opacity-100"}`}
38+
>
39+
{atBottom ? <ArrowUp size={30} /> : <ArrowDown size={30} />}
40+
</button>
41+
);
42+
};
43+
44+
export default ScrollButton;

0 commit comments

Comments
 (0)