Files
webb/src/components/ui/Navbar.tsx
T

306 lines
13 KiB
TypeScript
Raw Normal View History

'use client';
import React, { useState, useEffect, useRef } from 'react';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { Menu, X, Smartphone, Layers, ArrowUpRight } from 'lucide-react';
import { SiteConfig, resolveLocalised } from '@/types';
import { BrandLogo } from './BrandLogo';
interface NavbarProps {
config: SiteConfig;
currentLocale: string;
}
export function Navbar({ config, currentLocale }: NavbarProps) {
const [isScrolled, setIsScrolled] = useState(false);
const [mobileMenuOpen, setMobileMenuOpen] = useState(false);
const [mousePos, setMousePos] = useState({ x: 50, y: 50, active: false });
const pathname = usePathname();
const navRef = useRef<HTMLDivElement>(null);
const mirrorRef = useRef<HTMLDivElement>(null);
const chamberRef = useRef<HTMLDivElement>(null);
const waveOffsetRef = useRef<SVGFEOffsetElement>(null);
const dispMapRef = useRef<SVGFEDisplacementMapElement>(null);
useEffect(() => {
const handleScroll = () => {
setIsScrolled(window.scrollY > 20);
};
window.addEventListener('scroll', handleScroll, { passive: true });
return () => window.removeEventListener('scroll', handleScroll);
}, []);
// Optical Inverted Liquid Glass Refraction Pipeline (Synchronized 120 FPS on all devices)
useEffect(() => {
let rafId: number;
let lastY = 0;
let lastTime = performance.now();
let velocity = 0;
let wobblePhase = 0;
let navTop = 20;
let navLeft = 0;
// Cache navbar rect outside of scroll handler to avoid synchronous layout reflows on mobile
const measureNav = () => {
if (navRef.current) {
const rect = navRef.current.getBoundingClientRect();
navTop = rect.top;
navLeft = rect.left;
}
};
measureNav();
const updateMirror = () => {
if (!mirrorRef.current) return;
// Direct hardware scroll position: 100% synchronized with native touch & desktop scroll
const scrollY = window.scrollY || window.pageYOffset || document.documentElement.scrollTop;
const now = performance.now();
const dt = Math.max(now - lastTime, 16);
const dist = Math.abs(scrollY - lastY);
const instantVelocity = (dist / dt) * 1000;
lastY = scrollY;
lastTime = now;
// Smooth velocity interpolation
velocity += (instantVelocity - velocity) * 0.35;
// Exact 1:1 speed matching parity with hardware GPU translate3d
mirrorRef.current.style.left = `-${navLeft}px`;
mirrorRef.current.style.transform = `translate3d(0, -${scrollY + navTop}px, 0)`;
// Dynamic liquid wobble: 14 at rest, ramps up to 44 when scrolling (completely wobbles)
const currentWobble = Math.min(14 + velocity * 0.038, 44);
if (dispMapRef.current) {
dispMapRef.current.setAttribute('scale', currentWobble.toFixed(1));
}
// Wobbly phase oscillation
wobblePhase = (wobblePhase + dist * 0.4 + (velocity > 12 ? 2.2 : 0.4)) % 360;
if (waveOffsetRef.current) {
const dy = Math.sin(wobblePhase * 0.05) * 26 + ((scrollY * 0.35) % 150);
const dx = Math.cos(wobblePhase * 0.04) * (currentWobble * 0.35);
waveOffsetRef.current.setAttribute('dy', dy.toFixed(1));
waveOffsetRef.current.setAttribute('dx', dx.toFixed(1));
}
// 3D liquid fluid slosh & wobble on the chamber itself
if (chamberRef.current) {
const wobbleSkew = Math.sin(wobblePhase * 0.06) * Math.min(velocity * 0.007, 3.6);
chamberRef.current.style.transform = `perspective(380px) rotateX(6deg) scaleY(-1) skewX(${wobbleSkew.toFixed(2)}deg)`;
}
// Smooth deceleration loop
if (velocity > 1.2) {
cancelAnimationFrame(rafId);
rafId = requestAnimationFrame(updateMirror);
}
};
// Populate inverted reflection mirror with a lightweight visual clone of <main>
const setupMirror = () => {
const mainEl = document.querySelector('main');
if (mainEl && mirrorRef.current) {
mirrorRef.current.innerHTML = '';
const clone = mainEl.cloneNode(true) as HTMLElement;
// Strip heavy non-visual, interactive, and duplicated elements
clone.querySelectorAll('[id]').forEach((el) => el.removeAttribute('id'));
clone.querySelectorAll('canvas, script, noscript, iframe, video, audio').forEach((el) => el.remove());
clone.querySelectorAll('a, button, input, select, textarea').forEach((el) => {
el.setAttribute('tabindex', '-1');
(el as HTMLElement).style.pointerEvents = 'none';
});
mirrorRef.current.appendChild(clone);
measureNav();
updateMirror();
}
};
// Allow DOM to settle on mount or route transition
const timer = setTimeout(setupMirror, 60);
const onScroll = () => {
cancelAnimationFrame(rafId);
rafId = requestAnimationFrame(updateMirror);
};
const lenis = (window as any).lenis;
if (lenis && typeof lenis.on === 'function') {
lenis.on('scroll', onScroll);
}
window.addEventListener('scroll', onScroll, { passive: true });
window.addEventListener('touchmove', onScroll, { passive: true });
window.addEventListener('resize', () => {
measureNav();
onScroll();
}, { passive: true });
return () => {
clearTimeout(timer);
if (lenis && typeof lenis.off === 'function') {
lenis.off('scroll', onScroll);
}
window.removeEventListener('scroll', onScroll);
window.removeEventListener('touchmove', onScroll);
window.removeEventListener('resize', onScroll);
cancelAnimationFrame(rafId);
};
}, [pathname]);
const handleMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
const rect = e.currentTarget.getBoundingClientRect();
const x = ((e.clientX - rect.left) / rect.width) * 100;
const y = ((e.clientY - rect.top) / rect.height) * 100;
setMousePos({ x, y, active: true });
};
const handleMouseLeave = () => {
setMousePos((prev) => ({ ...prev, active: false }));
};
return (
<header className="fixed top-3 sm:top-5 left-0 right-0 z-50 px-3 sm:px-6 lg:px-8 pointer-events-none transition-all duration-300">
{/* Dynamic Liquid Glass Optical Wobble & Refraction Filter (120 FPS on all devices) */}
<svg className="absolute w-0 h-0 pointer-events-none overflow-hidden" aria-hidden="true" focusable="false">
<defs>
<filter id="liquid-glass-vertical-refraction" x="-5%" y="-10%" width="110%" height="120%" colorInterpolationFilters="sRGB">
{/* Broad organic fluid liquid wave turbulence */}
<feTurbulence type="turbulence" baseFrequency="0.014 0.036" numOctaves="1" result="wobbleNoise" />
{/* Dynamic scroll-synchronized phase wave offset as content moves */}
<feOffset ref={waveOffsetRef} in="wobbleNoise" dx="0" dy="0" result="movingWobble" />
{/* Active velocity-amplified liquid wobble displacement */}
<feDisplacementMap ref={dispMapRef} in="SourceGraphic" in2="movingWobble" scale="16" xChannelSelector="R" yChannelSelector="G" result="displaced" />
</filter>
</defs>
</svg>
<div
ref={navRef}
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
className={`relative max-w-6xl mx-auto liquid-glass-nav rounded-2xl sm:rounded-[22px] px-3.5 sm:px-6 py-2.5 sm:py-3 flex items-center justify-between transition-all duration-300 pointer-events-auto ${
isScrolled ? 'nav-scrolled' : ''
}`}
>
{/* Optical Inverted Liquid Glass Refraction Chamber */}
<div ref={chamberRef} className="liquid-refraction-chamber" aria-hidden="true">
<div ref={mirrorRef} className="liquid-refraction-mirror" />
<div className="liquid-caustic-refraction" />
</div>
{/* Dynamic Specular Caustic Glint - Crystal clear at rest, subtle gloss on cursor interaction */}
<div
className="absolute inset-0 pointer-events-none transition-opacity duration-300 z-[2]"
style={{
opacity: mousePos.active ? 0.7 : 0,
background: `radial-gradient(ellipse 260px 80px at ${mousePos.x}% ${mousePos.y}%, rgba(255, 255, 255, 0.45) 0%, rgba(255, 255, 255, 0.1) 40%, transparent 70%)`,
}}
/>
{/* Brand Logo */}
<BrandLogo size="md" />
{/* Center Navigation Links - Floating Glass Track */}
<nav className="hidden md:flex items-center gap-1 px-1.5 py-1 rounded-full bg-white/40 backdrop-blur-sm border border-white/80 shadow-inner">
{config.nav.header.map((item) => {
const active = pathname === item.href;
return (
<Link
key={item.href}
href={item.href}
className={`relative px-4 py-2 rounded-full text-xs font-semibold tracking-wide transition-all duration-200 ${
active
? 'text-blue-700 bg-white shadow-sm border border-slate-900/[0.06]'
: 'text-slate-700 hover:text-slate-950 hover:bg-white/60'
}`}
>
{resolveLocalised(item.label, currentLocale)}
</Link>
);
})}
{/* Direct Anchor to Studio Suite */}
<Link
href="/#studio-suite"
className="flex items-center gap-1.5 px-4 py-2 rounded-full text-xs font-semibold tracking-wide text-blue-700 hover:text-blue-900 hover:bg-blue-50/80 transition-colors"
>
<Layers className="w-3.5 h-3.5 text-blue-600" />
<span>Creative Suite</span>
</Link>
</nav>
{/* Right CTA Button */}
<div className="hidden md:flex items-center space-x-3">
<Link
href={config.nav.cta.href}
className="group relative inline-flex items-center gap-1.5 lg:gap-2 px-3.5 py-2 lg:px-5 lg:py-2.5 rounded-full text-xs font-bold text-white bg-gradient-to-r from-blue-600 via-indigo-600 to-blue-600 hover:from-blue-700 hover:to-indigo-700 shadow-md shadow-blue-500/25 hover:shadow-lg hover:shadow-blue-500/35 transition-all duration-300 hover:scale-105 active:scale-95"
>
<Smartphone className="w-3.5 h-3.5 lg:w-4 lg:h-4 text-white group-hover:rotate-6 transition-transform" />
<span>{resolveLocalised(config.nav.cta.label, currentLocale)}</span>
<ArrowUpRight className="w-3.5 h-3.5 opacity-80 group-hover:translate-x-0.5 group-hover:-translate-y-0.5 transition-transform" />
</Link>
</div>
{/* Mobile Menu Trigger */}
<div className="flex md:hidden items-center space-x-2">
<button
onClick={() => setMobileMenuOpen(!mobileMenuOpen)}
className="p-2 rounded-xl text-slate-800 bg-white/70 hover:bg-white border border-white/80 shadow-sm transition-colors"
aria-label="Open menu"
>
{mobileMenuOpen ? <X className="w-5 h-5 text-blue-600" /> : <Menu className="w-5 h-5" />}
</button>
</div>
</div>
{/* Mobile Drawer (Matching Liquid Glass Card) */}
{mobileMenuOpen && (
<div className="max-w-6xl mx-auto md:hidden mt-2 rounded-2xl liquid-glass-dropdown p-5 space-y-3 pointer-events-auto shadow-2xl animate-in slide-in-from-top-3 duration-200">
<div className="space-y-1">
{config.nav.header.map((item) => {
const active = pathname === item.href;
return (
<Link
key={item.href}
href={item.href}
onClick={() => setMobileMenuOpen(false)}
className={`flex items-center justify-between px-4 py-3 rounded-xl text-sm font-semibold transition-colors ${
active
? 'text-blue-700 bg-white shadow-sm border border-slate-900/[0.06]'
: 'text-slate-800 hover:bg-white/60'
}`}
>
<span>{resolveLocalised(item.label, currentLocale)}</span>
</Link>
);
})}
<Link
href="/#studio-suite"
onClick={() => setMobileMenuOpen(false)}
className="flex items-center justify-between px-4 py-3 rounded-xl text-sm font-semibold text-blue-700 bg-blue-50/70 hover:bg-blue-100/70"
>
<span className="flex items-center gap-2">
<Layers className="w-4 h-4 text-blue-600" />
Creative Suite
</span>
</Link>
</div>
<div className="pt-3 border-t border-slate-900/[0.06]">
<Link
href={config.nav.cta.href}
onClick={() => setMobileMenuOpen(false)}
className="flex items-center justify-center gap-2 w-full py-3.5 rounded-xl text-sm font-bold text-white bg-gradient-to-r from-blue-600 to-indigo-600 shadow-md shadow-blue-500/25"
>
<Smartphone className="w-4 h-4" />
<span>{resolveLocalised(config.nav.cta.label, currentLocale)}</span>
</Link>
</div>
</div>
)}
</header>
);
}