PratikWebsite / components /Navbar /FloatingNav.tsx
Keep-Triangle's picture
Upload 35 files
5d33320 verified
Raw History Blame Contribute Delete
7.19 kB
import React, { useState } from 'react';
import { motion, useScroll, useMotionValueEvent, AnimatePresence, LayoutGroup } from 'framer-motion';
import { NAV_ITEMS } from '../../data/mockData';
import { NavItem } from '../../types';
interface FloatingNavProps {
activeTab: string;
onTabChange: (id: string) => void;
}
const FloatingNav: React.FC<FloatingNavProps> = ({ activeTab, onTabChange }) => {
const { scrollY } = useScroll();
const [isScrollingDown, setIsScrollingDown] = useState(false);
const [hasScrolled, setHasScrolled] = useState(false);
useMotionValueEvent(scrollY, "change", (latest) => {
const previous = scrollY.getPrevious() || 0;
const isDown = latest > previous;
// Add a threshold to prevent jitter
if (Math.abs(latest - previous) > 5) {
setIsScrollingDown(isDown);
}
setHasScrolled(latest > 100);
});
const heavyEase: [number, number, number, number] = [0.22, 1, 0.36, 1];
// Find active label for the dynamic indicator
const activeLabel = NAV_ITEMS.find(n => n.id === activeTab)?.label || "";
// --- Sliding Window Logic ---
// We want to show 4 items.
// We try to keep the active item at index 1 (2nd position) of the window if possible,
// to allow seeing one previous item and two future items.
const WINDOW_SIZE = 4;
const activeIndex = NAV_ITEMS.findIndex(n => n.id === activeTab);
let startIndex = activeIndex - 1;
// Clamp start index
if (startIndex < 0) startIndex = 0;
// Ensure we don't go out of bounds at the end
if (startIndex + WINDOW_SIZE > NAV_ITEMS.length) {
startIndex = NAV_ITEMS.length - WINDOW_SIZE;
}
// Safety check if total items < WINDOW_SIZE
if (startIndex < 0) startIndex = 0;
const visibleItems = NAV_ITEMS.slice(startIndex, startIndex + WINDOW_SIZE);
return (
<div className="fixed bottom-8 left-0 right-0 z-40 flex flex-col items-center pointer-events-none px-4 gap-4">
{/* Dynamic Current Section Indicator (Appears when scrolling) */}
<AnimatePresence>
{hasScrolled && !isScrollingDown && (
<motion.div
initial={{ opacity: 0, y: 10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: 10 }}
className="px-4 py-1.5 rounded-full bg-[#0e0303] border border-brand-gold/20 shadow-lg"
>
<span className="text-[10px] uppercase tracking-[0.2em] text-brand-gold font-medium">
Viewing: {activeLabel}
</span>
</motion.div>
)}
</AnimatePresence>
{/* Main Nav Container */}
<motion.div
className="pointer-events-auto relative rounded-full border border-white/10 bg-[#0e0303] shadow-2xl"
animate={{
scale: isScrollingDown ? 0.9 : 1,
y: isScrollingDown ? 20 : 0,
opacity: isScrollingDown ? 0.6 : 1,
}}
transition={{
duration: 0.4,
ease: heavyEase
}}
style={{
boxShadow: '0 18px 55px -30px rgba(0,0,0,0.9)',
}}
>
{/*
Using LayoutGroup to automatically animate the "slide" effect
when items are removed/added from the DOM
*/}
<div className="flex items-center px-1.5 py-1.5 gap-1">
<LayoutGroup>
<AnimatePresence mode='popLayout' initial={false}>
{visibleItems.map((item: NavItem) => {
const isActive = activeTab === item.id;
return (
<motion.button
layout
key={item.id}
onClick={() => onTabChange(item.id)}
initial={{ opacity: 0, scale: 0.8, filter: "blur(4px)" }}
animate={{ opacity: 1, scale: 1, filter: "blur(0px)" }}
exit={{ opacity: 0, scale: 0.8, filter: "blur(4px)" }}
transition={{ duration: 0.4, ease: "easeOut" }}
className="relative flex-shrink-0 px-5 py-2.5 rounded-full text-sm font-medium outline-none group overflow-hidden whitespace-nowrap focus-visible:ring-2 focus-visible:ring-brand-gold/40"
>
{/* Active Pill Background */}
{isActive && (
<motion.div
layoutId="active-pill"
className="absolute inset-0 rounded-full"
style={{
backgroundColor: '#2a0a0a',
boxShadow: 'inset 0 1px 1px rgba(255, 255, 255, 0.1), 0 4px 10px rgba(0,0,0,0.5)'
}}
transition={{
duration: 0.35,
ease: heavyEase
}}
>
<div className="absolute inset-0 rounded-full border border-white/10" />
</motion.div>
)}
{/* Text Label */}
<motion.span
className={`relative z-10 block transition-colors duration-200 tracking-wide ${
isActive
? 'text-brand-gold'
: 'text-gray-400 group-hover:text-white'
}`}
>
{item.label}
</motion.span>
</motion.button>
);
})}
</AnimatePresence>
</LayoutGroup>
</div>
</motion.div>
</div>
);
};
export default FloatingNav;