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

Add Scroll to top #264

Closed
wants to merge 4 commits into from
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
9 changes: 7 additions & 2 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import ContributorDetail from "./pages/contributor/ContributorDetail";
import Login from "./pages/auth/Login";
import SignUp from "./pages/auth/SignUp";
import ChanakyaGpt from "./pages/resources/ChanakyaGpt";

import ScrollToTop from "./components/shared/ScrollToTop";
import ScrollProgressBar from "./components/shared/ScrollProgressBar";
function App() {
const { progress, isDarkMode } = useContext(Context); // Assuming isDarkMode is provided in your context

Expand All @@ -32,7 +33,10 @@ function App() {
return (
<div className={`d-flex flex-column ${isDarkMode ? 'dark' : ''}`}>
<Router>
<Navbar />
<div>
<Navbar />
<ScrollProgressBar/>
</div>
<LoadingBar height={3} color="#f11946" progress={progress} />

<main className="container flex-grow-1 mt-4">
Expand All @@ -57,6 +61,7 @@ function App() {
</main>

<Footer />
<ScrollToTop/>
</Router>
</div>
);
Expand Down
7 changes: 6 additions & 1 deletion src/components/shared/Footer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "../../css/Footer.css";

const Footer = () => {
const { isDarkMode } = useContext(Context);

const year = new Date().getFullYear();
return (
<footer className="footer font-small pt-4" style={{ backgroundColor: `${isDarkMode ? 'rgba(0, 0, 0, 0.4)' : 'rgba(223, 223, 176, 0.4)'}` }}>
<div className="container pt-2">
Expand Down Expand Up @@ -52,8 +52,13 @@ const Footer = () => {
</div>
</div>


<div className="text-center">
<p>&copy; {year} Chanakya Niti. All rights reserved.</p>

<div className="text-center" style={{ color: isDarkMode ? 'white' : 'black' }}>
<p>&copy; {new Date().getFullYear()} Chanakya Niti. All rights reserved.</p>

</div>
</footer>
);
Expand Down
23 changes: 23 additions & 0 deletions src/components/shared/ScrollProgressBar.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, { useEffect,useState } from 'react'
const ScrollProgressBar = () => {
const [scrollWidth,setScrollWidth]=useState(0)
useEffect(()=>{
const fillScrollLine=()=>{
const windowHeight=window.innerHeight;
const fullHeight=document.body.clientHeight;
const scrolled=window.scrollY;
const percentScrolled=(scrolled/(fullHeight-windowHeight))*100;
setScrollWidth(percentScrolled);
}
window.addEventListener('scroll',fillScrollLine);
return ()=>window.removeEventListener('scroll',fillScrollLine);
},[]);
return (
<div className={`scroll-progress-bar ${scrollWidth>0 ?'visible':''}`
} style={{width:`${scrollWidth}%`}}>

</div>
)
}

export default ScrollProgressBar
31 changes: 31 additions & 0 deletions src/components/shared/ScrollToTop.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React,{useState,useEffect} from 'react'
import { FaArrowUp } from "react-icons/fa";
const ScrollToTop = () => {
const [isVisible,setIsVisible]=useState(false)
useEffect(()=>{
const toggleVisibility=()=>{
if(window.pageYOffset>300){
setIsVisible(true)
}
else{
setIsVisible(false);
}
};
window.addEventListener('scroll',toggleVisibility);
return ()=>window.removeEventListener('scroll',toggleVisibility);
},[]);
const ScrollToTop=()=>{
window.scrollTo({
top:0,
behavior:'smooth'
})
}
return (
<button onClick={ScrollToTop}
className={`scroll-to-top ${isVisible?'show':""}`}>
<FaArrowUp size={30} className='m-2'/>
</button>
)
}

export default ScrollToTop
44 changes: 44 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,48 @@ body {
background-color: #2c2a2a;
color: white;
transition: 0.25s;
}
.scroll-to-top{
position: fixed;
bottom: 20px;
right: 20px;
width: 50px;
height: 50px;
border-radius:50%;
background-color: #333;
color: #fff;
border: none;
outline: none;
display: flex;
justify-content: center;
cursor: pointer;
transition: opacity 0.4s;
opacity: 0;
visibility: hidden;
z-index: 1000;
}
.scroll-to-top.show{
opacity: 1;
visibility: visible;
}
.scroll-to-top:hover{
background-color: #555;
}
.navbar{
position:relative;
z-index: 1;
}
.scroll-progress-bar{
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 8px;
background-color: #d1a3a4;
z-index: 2;
transition: opacity 0.3s;
opacity: 0;
}
.scroll-progress-bar.visible{
opacity: 1;
}