import { useEffect, useRef, useState } from 'react'; import { createPortal } from 'react-dom'; import { Link, NavLink, useLocation } from 'react-router-dom'; import { LayoutDashboard, MessageCircle, Phone, ShoppingBag } from 'lucide-react'; import ThemeToggle from './ThemeToggle.jsx'; import { useAuth } from '../context/AuthContext.jsx'; import { useIsAreaHidden, useSiteSettings } from '../context/SiteSettingsContext.jsx'; import { useLanguage } from '../context/LanguageContext.jsx'; import logoDark from '../assets/logo-dark.png'; import { telHref, waHref } from '../utils/contactLinks.js'; import { trackEvent } from '../utils/analytics.js'; const SHOP_URL = 'https://www.audio4cars.de/'; const ITEM_DELAY_MS = 70; const FLY_DURATION_MS = 500; function FlagDE({ className }) { return ( ); } function FlagGB({ className }) { return ( ); } const LANGUAGE_OPTIONS = [ { code: 'de', label: 'Deutsch', Flag: FlagDE }, { code: 'en', label: 'English', Flag: FlagGB }, ]; function LanguageSwitcher({ overHero = false }) { const { language, setLanguage, t } = useLanguage(); const [isOpen, setIsOpen] = useState(false); const containerRef = useRef(null); const current = LANGUAGE_OPTIONS.find((opt) => opt.code === language) || LANGUAGE_OPTIONS[0]; useEffect(() => { if (!isOpen) return undefined; const handleClickOutside = (e) => { if (containerRef.current && !containerRef.current.contains(e.target)) setIsOpen(false); }; const handleKeyDown = (e) => { if (e.key === 'Escape') setIsOpen(false); }; document.addEventListener('mousedown', handleClickOutside); document.addEventListener('keydown', handleKeyDown); return () => { document.removeEventListener('mousedown', handleClickOutside); document.removeEventListener('keydown', handleKeyDown); }; }, [isOpen]); return (
{isOpen && (
{LANGUAGE_OPTIONS.map((opt) => ( ))}
)}
); } export default function Navbar() { const [open, setOpen] = useState(false); const [mounted, setMounted] = useState(false); const [menuVisible, setMenuVisible] = useState(false); const { user } = useAuth(); const { phone, whatsapp, logo_dark_path: logoDarkPath, has_downloads: hasDownloads } = useSiteSettings(); const isHidden = useIsAreaHidden(); const { t } = useLanguage(); // Hauptlinks, die im Dashboard ausgeblendet werden koennen, fallen hier raus. const showVehicles = !isHidden('page.vehicles'); const showServices = !isHidden('page.services'); const showGallery = !isHidden('page.gallery'); const showDownloads = hasDownloads && !isHidden('page.downloads'); // Kontakt + Trennlinie + Shop, dazu die optionalen Eintraege. const totalItems = 3 + [showVehicles, showServices, showGallery, showDownloads, phone, whatsapp].filter(Boolean).length; const location = useLocation(); const isHome = location.pathname === '/'; const [scrolled, setScrolled] = useState(!isHome); // Auf der Startseite beginnt der Header transparent, verschmolzen mit dem Hero-Bild, // und wird erst zur weissen/dunklen Leiste, sobald man zu scrollen beginnt. useEffect(() => { if (!isHome) { setScrolled(true); return; } const handleScroll = () => setScrolled(window.scrollY > 24); handleScroll(); window.addEventListener('scroll', handleScroll, { passive: true }); return () => window.removeEventListener('scroll', handleScroll); }, [isHome]); const transparent = isHome && !scrolled; // Menue-Overlay als echter Dialog: Escape schliesst, Tab bleibt im Menue, die Seite // dahinter scrollt nicht mit, und der Fokus springt beim Oeffnen auf "Schliessen". const overlayRef = useRef(null); const burgerRef = useRef(null); useEffect(() => { if (!open) return undefined; const previousOverflow = document.body.style.overflow; document.body.style.overflow = 'hidden'; const focusTimer = setTimeout(() => overlayRef.current?.querySelector('button, a')?.focus(), 50); const handleKeyDown = (e) => { if (e.key === 'Escape') { setOpen(false); return; } if (e.key !== 'Tab' || !overlayRef.current) return; const focusable = [...overlayRef.current.querySelectorAll('a[href], button')]; if (!focusable.length) return; const first = focusable[0]; const last = focusable[focusable.length - 1]; if (e.shiftKey && document.activeElement === first) { e.preventDefault(); last.focus(); } else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); } }; document.addEventListener('keydown', handleKeyDown); return () => { clearTimeout(focusTimer); document.body.style.overflow = previousOverflow; document.removeEventListener('keydown', handleKeyDown); burgerRef.current?.focus({ preventScroll: true }); }; }, [open]); const desktopLinkClass = ({ isActive }) => `rounded-md px-2.5 py-1.5 text-sm font-semibold transition ${ isActive ? 'text-brand-400' : transparent ? 'text-white/90 hover:text-white' : 'text-neutral-300 hover:text-white' }`; useEffect(() => { if (open) { setMounted(true); // Doppeltes rAF: nach dem Mounten braucht der Browser einen echten Paint mit der // "versteckten" Ausgangsposition, bevor wir auf "sichtbar" umschalten - sonst // fasst er beide Zustandsänderungen in einem Frame zusammen und es gibt keinen // sichtbaren Übergang (kein Fade-in). let rafInner; const rafOuter = requestAnimationFrame(() => { rafInner = requestAnimationFrame(() => setMenuVisible(true)); }); return () => { cancelAnimationFrame(rafOuter); if (rafInner) cancelAnimationFrame(rafInner); }; } setMenuVisible(false); const closeDuration = FLY_DURATION_MS + (totalItems - 1) * ITEM_DELAY_MS; const id = setTimeout(() => setMounted(false), closeDuration); return () => clearTimeout(id); }, [open, totalItems]); const flyIn = () => `transition-all duration-500 ease-out ${menuVisible ? 'opacity-100' : 'opacity-0'}`; const flyInStyle = (index) => { const delayIndex = menuVisible ? index : totalItems - 1 - index; return { transitionDelay: `${delayIndex * ITEM_DELAY_MS}ms`, transform: menuVisible ? 'translateY(0)' : 'translateY(-140px)', }; }; const AdminIcon = user && ( ); // Fortlaufender Index fuer die gestaffelte Einflug-Animation - optionale Eintraege // (Downloads, Telefon, WhatsApp) verschieben so die nachfolgenden automatisch. let flyIndex = 0; const nextFly = () => flyInStyle(flyIndex++); return ( // Die Hintergrund-/Text-/Rahmenfarben hier sind bewusst die "dunklen" Werte fest verdrahtet // statt per dark:-Utility bedingt (Kundenwunsch: die Navbar soll immer im Dark-Theme // bleiben, unabhaengig vom Light-/Dark-Mode-Toggle des Besuchers).
{/* Ab Desktop-Breite die wichtigsten Seiten direkt sichtbar, ohne Umweg ueber das Menue. */}
{/* Immer die Dark-Mode-Logovariante (weisse Wortmarke) - passend zur immer dunklen Navbar, unabhaengig vom Light-/Dark-Mode-Toggle des Besuchers. */} HifiPlanet
{phone && ( trackEvent('contact_call')} className={`mr-1 hidden items-center gap-2 rounded-full border px-3.5 py-1.5 text-sm font-semibold transition lg:inline-flex ${ transparent ? 'border-white/40 text-white hover:bg-white/10' : 'border-neutral-700 text-neutral-200 hover:bg-neutral-800' }`} > {phone} )} {AdminIcon} {!open && }
{mounted && createPortal(
{showVehicles && ( setOpen(false)} className={`text-3xl font-extrabold tracking-tight text-neutral-900 hover:text-brand-600 ${flyIn()}`} style={nextFly()}> {t('nav.vehicles')} )} {showServices && ( setOpen(false)} className={`text-3xl font-extrabold tracking-tight text-neutral-900 hover:text-brand-600 ${flyIn()}`} style={nextFly()}> {t('nav.services')} )} {showGallery && ( setOpen(false)} className={`text-3xl font-extrabold tracking-tight text-neutral-900 hover:text-brand-600 ${flyIn()}`} style={nextFly()}> {t('nav.gallery')} )} setOpen(false)} className={`text-3xl font-extrabold tracking-tight text-neutral-900 hover:text-brand-600 ${flyIn()}`} style={nextFly()}> {t('nav.contact')} {showDownloads && ( setOpen(false)} className={`text-3xl font-extrabold tracking-tight text-neutral-900 hover:text-brand-600 ${flyIn()}`} style={nextFly()}> {t('nav.downloads')} )}
{phone && ( trackEvent('contact_call')} className={`flex items-center gap-2 text-sm font-semibold uppercase tracking-wide text-neutral-600 hover:text-brand-600 ${flyIn()}`} style={nextFly()}> {phone} )} {whatsapp && ( trackEvent('contact_whatsapp')} target="_blank" rel="noopener noreferrer" className={`flex items-center gap-2 text-sm font-semibold uppercase tracking-wide text-neutral-600 hover:text-brand-600 ${flyIn()}`} style={nextFly()} > {t('nav.whatsapp')} )} {t('nav.shop')}
, document.body )}
); }