Skip to content

Commit

Permalink
added scroll-top feature and navbar feature (code100x#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidhuiwnl authored Sep 3, 2024
1 parent 041b85b commit a3c7c9c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Metadata } from 'next';
import { Inter as FontSans } from 'next/font/google';
import './globals.css';
import TopLoader from '@/components/Toploader';
import ScrollToTop from '@/components/ScrollToTop';

const fontSans = FontSans({
subsets: ['latin'],
Expand Down Expand Up @@ -36,6 +37,7 @@ export default async function RootLayout({
<main className="grow grid">{children}</main>
<Footer />
</Providers>
<ScrollToTop />
</body>
</html>
);
Expand Down
41 changes: 41 additions & 0 deletions src/components/ScrollToTop.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use client';
import { Button } from './ui/button';
import { MoveUp } from 'lucide-react';
import { useEffect, useState } from 'react';

export default function ScrollToTop() {
const [isVisible, setIsVisible] = useState(false);

useEffect(() => {
const handleScroll = () => {
if (window.scrollY > 200) {
setIsVisible(true);
} else {
setIsVisible(false);
}
};
window.addEventListener('scroll', handleScroll);

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

function toUp() {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
}
return (
<Button
className={`fixed bottom-5 right-5 w-[50px] h-[50px] rounded-full bg-white text-black flex items-center justify-center shadow-lg hover:bg-gray-200 transition-opacity duration-300 ${
isVisible ? 'opacity-100' : 'opacity-0 pointer-events-none'
}`}
aria-label="Scroll to top"
onClick={toUp}
>
<MoveUp className="text-black" />
</Button>
);
}
1 change: 1 addition & 0 deletions src/components/job-landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import APP_PATHS from '@/config/path.config';
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import { redirect } from 'next/navigation';

dayjs.extend(relativeTime);

export const calculateTimeSincePosted = (postedAt: Date): string => {
Expand Down
28 changes: 27 additions & 1 deletion src/layouts/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Link from 'next/link';
import { NavItem } from '@/components/navitem';
import Image from 'next/image';
import { Skeleton } from '@/components/ui/skeleton';
import { useState, useEffect } from 'react';

const CompanyLogo = () => {
return (
Expand All @@ -26,9 +27,34 @@ const CompanyLogo = () => {

const Header = () => {
const session = useSession();
const [opacity, setOpacity] = useState(1);

useEffect(() => {
const handleScroll = () => {
const scrollPosition = window.scrollY;
const windowHeight = window.innerHeight;

const fadeStart = 0;
const fadeEnd = windowHeight * 0.3;

if (scrollPosition <= fadeStart) {
setOpacity(1);
} else if (scrollPosition >= fadeEnd) {
setOpacity(0);
} else {
setOpacity(1 - (scrollPosition - fadeStart) / (fadeEnd - fadeStart));
}
};

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

return (
<header className="sticky top-6 z-50 md:w-auto mx-auto w-full px-5">
<header
className="sticky top-6 z-50 md:w-auto mx-auto w-full px-6 transition-opacity duration-300"
style={{ opacity }}
>
<div className="container flex h-14 max-w-screen-xl items-center md:border-2 rounded-full border-border/40 sm:bg-none sm:bg-background/60 ">
<Link href="/" className="p-2.5 mr-4">
<CompanyLogo />
Expand Down

0 comments on commit a3c7c9c

Please sign in to comment.