Skip to content

Commit e5c4eb8

Browse files
committed
Back to top button added
1 parent 6c6bc3e commit e5c4eb8

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

src/components/Backtotop.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React, { useEffect, useState } from "react";
2+
import { ArrowUp } from "lucide-react";
3+
4+
const BackToTopButton: React.FC = () => {
5+
const [isVisible, setIsVisible] = useState(false);
6+
7+
useEffect(() => {
8+
const toggleVisibility = () => {
9+
if (window.scrollY > 300) {
10+
setIsVisible(true);
11+
} else {
12+
setIsVisible(false);
13+
}
14+
};
15+
16+
window.addEventListener("scroll", toggleVisibility);
17+
18+
return () => {
19+
window.removeEventListener("scroll", toggleVisibility);
20+
};
21+
}, []);
22+
23+
const scrollToTop = () => {
24+
window.scrollTo({
25+
top: 0,
26+
behavior: "smooth",
27+
});
28+
};
29+
30+
return (
31+
<>
32+
{isVisible && (
33+
<button
34+
onClick={scrollToTop}
35+
aria-label="Back to top"
36+
className="fixed bottom-6 right-6 z-50 flex h-12 w-12 items-center justify-center rounded-full bg-green-500 text-white shadow-lg transition-all duration-300 hover:scale-110 hover:bg-green-600"
37+
>
38+
<ArrowUp size={22} />
39+
</button>
40+
)}
41+
</>
42+
);
43+
};
44+
45+
export default BackToTopButton;

src/pages/Contributors/Contributors.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { FaGithub } from "react-icons/fa";
1515
import { Link } from "react-router-dom";
1616
import axios from "axios";
1717
import { GITHUB_REPO_CONTRIBUTORS_URL } from "../../utils/constants";
18+
import BackToTopButton from "../../components/Backtotop";
1819

1920
interface Contributor {
2021
id: number;
@@ -136,6 +137,7 @@ const ContributorsPage = () => {
136137
</Grid>
137138
))}
138139
</Grid>
140+
<BackToTopButton/>
139141
</Container>
140142
</div>
141143
);

src/pages/Home/Home.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import Hero from "../../components/Hero";
22
import HowItWorks from "../../components/HowItWorks";
33
import Features from "../../components/Features";
4+
import BackToTopButton from "../../components/Backtotop";
45

56
function Home() {
67
return (
78
<div className="">
89
<Hero />
910
<Features />
1011
<HowItWorks />
11-
12+
<BackToTopButton/>
1213
</div>
1314
)
1415
}

src/pages/Tracker/Tracker.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { useTheme } from "@mui/material/styles";
3333
import { useGitHubAuth } from "../../hooks/useGitHubAuth";
3434
import { useGitHubData } from "../../hooks/useGitHubData";
3535
import { KeyIcon } from "lucide-react";
36+
import BackToTopButton from "../../components/Backtotop";
3637

3738
const ROWS_PER_PAGE = 10;
3839

@@ -396,6 +397,7 @@ const Home: React.FC = () => {
396397
</TableContainer>
397398
</Box>
398399
)}
400+
<BackToTopButton/>
399401
</Container>
400402
);
401403
};

0 commit comments

Comments
 (0)