Spaces:
Sleeping
Sleeping
File size: 7,186 Bytes
5d33320 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 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; |