Skip to content

Commit a743727

Browse files
Merge pull request #222 from Vaishnavi22Thakur/feature/back-to-top-button
feat: add Back to Top button for better navigation on long pages
2 parents 76816a7 + 75f4dc0 commit a743727

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { useState, useEffect } from 'react';
2+
import { ArrowUp } from 'lucide-react';
3+
4+
function BackToTop() {
5+
const [isVisible, setIsVisible] = useState(false);
6+
const [isHovered, setIsHovered] = useState(false);
7+
8+
useEffect(() => {
9+
let ticking = false;
10+
11+
const handleScroll = () => {
12+
if (!ticking) {
13+
window.requestAnimationFrame(() => {
14+
setIsVisible(window.scrollY > 300);
15+
ticking = false;
16+
});
17+
ticking = true;
18+
}
19+
};
20+
21+
window.addEventListener('scroll', handleScroll);
22+
handleScroll();
23+
24+
return () => {
25+
window.removeEventListener('scroll', handleScroll);
26+
};
27+
}, []);
28+
29+
const handleClick = () => {
30+
window.scrollTo({
31+
top: 0,
32+
behavior: 'smooth'
33+
});
34+
};
35+
36+
if (!isVisible) {
37+
return null;
38+
}
39+
40+
return (
41+
<button
42+
onClick={handleClick}
43+
title="Back to Top"
44+
aria-label="Back to Top"
45+
onMouseEnter={() => setIsHovered(true)}
46+
onMouseLeave={() => setIsHovered(false)}
47+
style={{
48+
position: 'fixed',
49+
bottom: '2rem',
50+
right: '2rem',
51+
zIndex: 1000,
52+
backgroundColor: '#00f5d4',
53+
color: '#000',
54+
border: 'none',
55+
borderRadius: '50%',
56+
width: '45px',
57+
height: '45px',
58+
cursor: 'pointer',
59+
boxShadow: '0 0 15px rgba(0, 245, 212, 0.4)',
60+
display: 'flex',
61+
alignItems: 'center',
62+
justifyContent: 'center',
63+
transform: isHovered ? 'scale(1.1)' : 'scale(1)',
64+
transition: 'transform 0.2s ease',
65+
}}
66+
>
67+
<ArrowUp size={20} strokeWidth={2.5} />
68+
</button>
69+
);
70+
}
71+
72+
export default BackToTop;

apps/web-dashboard/src/components/Layout/MainLayout.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Sidebar from './Sidebar';
44
import Header from './Header';
55
import ProjectNavbar from './ProjectNavbar';
66
import { useLayout } from '../../context/LayoutContext';
7-
7+
import BackToTop from './BackToTop';
88
// Use the new official logo from public directory
99
const logoImage = "https://cdn.jsdelivr.net/gh/yash-pouranik/urBackend@main/frontend/public/logo.png";
1010

@@ -68,6 +68,7 @@ function MainLayout({ children }) {
6868
{children}
6969
</div>
7070
</div>
71+
<BackToTop />
7172
</div>
7273
);
7374
}

apps/web-dashboard/src/pages/LandingPage/index.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useState, useEffect, useRef, useCallback } from 'react';
22
import { Link, useNavigate } from 'react-router-dom';
33
import { useAuth } from '../../context/AuthContext';
4-
4+
import BackToTop from '../../components/Layout/BackToTop';
55
import {
66
Database,
77
Shield,
@@ -907,6 +907,7 @@ function LandingPage() {
907907
</div>
908908

909909
<Footer />
910+
<BackToTop />
910911
</div>
911912
);
912913
}

0 commit comments

Comments
 (0)