Skip to content

Commit

Permalink
Merge pull request #224 from SurajPrakash24/scroll-to-top
Browse files Browse the repository at this point in the history
closes: #211 
Added scroll to top button
  • Loading branch information
dhairyagothi authored Oct 12, 2024
2 parents eafb76c + 9d4a9a2 commit eaa5d93
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
28 changes: 28 additions & 0 deletions frontend/src/components/scrollToTop.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.scroll-to-top {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 1000;
}

.scroll-btn {
/* background-color: #007bff; */
background-color: #723aeb;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
font-size: 20px;
font-weight: bold;
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.4);
opacity: 0.9;
}

.scroll-btn:hover {
opacity: 1;
}

.scroll-btn:focus {
outline: none;
}
42 changes: 42 additions & 0 deletions frontend/src/components/scrollToTop.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React, { useState, useEffect } from 'react';
import './scrollToTop.css';

const ScrollToTopButton = () => {
const [isVisible, setIsVisible] = useState(false);

// Show or hide the button based on scroll position
const toggleVisibility = () => {
if (window.scrollY > 200) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};

// Scroll the window back to the top
const scrollToTop = () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
};

useEffect(() => {
window.addEventListener('scroll', toggleVisibility);
return () => {
window.removeEventListener('scroll', toggleVisibility);
};
}, []);

return (
<div className="scroll-to-top">
{isVisible && (
<button onClick={scrollToTop} className="scroll-btn">
</button>
)}
</div>
);
};

export default ScrollToTopButton;
2 changes: 2 additions & 0 deletions frontend/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import ScrollToTopButton from './components/scrollToTop.jsx'
import { GoogleOAuthProvider } from "@react-oauth/google"

const clientId = import.meta.env.VITE_OAUTH_CLIENT_ID
Expand All @@ -10,6 +11,7 @@ createRoot(document.getElementById('root')).render(
<StrictMode>
<GoogleOAuthProvider clientId={clientId}>
<App />
<ScrollToTopButton />
</GoogleOAuthProvider>
</StrictMode>,
)

0 comments on commit eaa5d93

Please sign in to comment.