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

fix: Added scroll to top button #1737

Merged
merged 4 commits into from
Jul 17, 2023
Merged
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
3 changes: 2 additions & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "./globals.css";
import { Inter } from "next/font/google";
import { MobileNavBar } from "@/components/MobileNavbar";
import { Navbar, Footer } from "@/components";
import { Navbar, Footer, ScrollToTop } from "@/components";


const inter = Inter({ subsets: ["latin"] });
Expand All @@ -25,6 +25,7 @@ export default function RootLayout({
{children}

<MobileNavBar />
<ScrollToTop />
<Footer />
</body>
</html>
Expand Down
35 changes: 35 additions & 0 deletions components/ScrollToTop.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use client"
import React, { useEffect, useState } from 'react';
priyankarpal marked this conversation as resolved.
Show resolved Hide resolved
import { FaArrowUp } from 'react-icons/fa';

function BackToTopButton() {
const [btnVisiblity, setBtnVisiblity] = useState(false);

const handleScroll = () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
};
useEffect(() => {
const toggleVisiblity = () => {
window.scrollY > 250 ? setBtnVisiblity(true) : setBtnVisiblity(false);
};
window.addEventListener('scroll', toggleVisiblity);
return () => {
window.removeEventListener('scroll', toggleVisiblity);
};
}, []);

return (
<button onClick={handleScroll}>
<div
className={` ${!btnVisiblity ? 'translate-y-32 md:translate-y-20' : 'translate-y-0'
} fixed bottom-20 md:bottom-10 right-8 z-50 w-10 h-10 rounded-lg bg-primary text-white cursor-pointer flex justify-center items-center transform transition-all delay-100 duration-500 `}
>
<div className="button">
<FaArrowUp />
</div>
</div>
</button>
);
};

export default BackToTopButton;
1 change: 1 addition & 0 deletions components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as ProjectCard } from './ProjectCard';
export { default as Navbar } from './Navbar';
export { default as Footer } from './Footer';
export { default as ScrollToTop } from './ScrollToTop';