import React, { useState, useEffect, memo, useCallback } from "react"; import { motion, AnimatePresence, useReducedMotion } from "framer-motion"; import { Menu, X, Phone, Mail, Recycle } from "lucide-react"; import { Link, useLocation, useNavigate } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { Button } from "@/components/ui/button"; import LanguageSelector from "@/components/LanguageSelector"; const Navigation = memo(() => { const [isOpen, setIsOpen] = useState(false); const [scrolled, setScrolled] = useState(false); const location = useLocation(); const navigate = useNavigate(); const prefersReducedMotion = useReducedMotion(); const { t, i18n } = useTranslation(); useEffect(() => { let ticking = false; const handleScroll = () => { if (!ticking) { requestAnimationFrame(() => { setScrolled(window.scrollY > 50); ticking = false; }); ticking = true; } }; // Handle mobile viewport changes that affect sticky headers const handleResize = () => { if (!ticking) { requestAnimationFrame(() => { // Force recalculation on mobile viewport changes const nav = document.querySelector('nav'); if (nav) { nav.style.transform = 'translateZ(0)'; // Force hardware acceleration } ticking = false; }); ticking = true; } }; window.addEventListener("scroll", handleScroll, { passive: true }); window.addEventListener("resize", handleResize, { passive: true }); window.addEventListener("orientationchange", handleResize, { passive: true }); return () => { window.removeEventListener("scroll", handleScroll); window.removeEventListener("resize", handleResize); window.removeEventListener("orientationchange", handleResize); }; }, []); // Scroll behavior on route changes useEffect(() => { if (location.pathname === '/muanyagipar' || location.pathname === '/epitoipar') { // Always scroll to top when navigating to product pages window.scrollTo({ top: 0, behavior: 'smooth' }); } }, [location.pathname]); // Menu items with proper translation dependency const menuItems = React.useMemo(() => [ { name: t('navigation.home'), href: "/" }, { name: t('navigation.about'), href: "#about" }, { name: t('navigation.process'), href: "#process" }, { name: t('navigation.products'), href: "#products" }, { name: t('navigation.contact'), href: "#contact" } ], [t]); // Enhanced navigation handler with mobile-optimized smooth scrolling const handleNavigation = useCallback((href: string, closeMobileMenu: boolean = false) => { console.log('Navigation clicked:', href); // Always close mobile menu on any navigation setIsOpen(false); if (href === '/') { // Navigate to home page if (location.pathname !== '/') { navigate('/'); } else { // Already on home, scroll to top smoothly window.scrollTo({ top: 0, behavior: 'smooth' }); } } else if (href.startsWith('#')) { // Landing page sections with mobile-optimized smooth scrolling if (location.pathname !== '/') { // Navigate to home first, then handle scrolling navigate('/'); // Use longer timeout for mobile devices setTimeout(() => { scrollToSection(href.substring(1)); }, 500); } else { // Already on home page, scroll directly setTimeout(() => { scrollToSection(href.substring(1)); }, 100); // Small delay to ensure menu closes } } else { // External routes (like /muanyagipar, /epitoipar) navigate(href); } }, [location.pathname, navigate]); // Mobile-optimized scroll to section function const scrollToSection = useCallback((sectionId: string) => { const element = document.getElementById(sectionId); if (!element) return; // Enhanced mobile detection and offset calculation const isMobile = window.innerWidth < 1024; const navigationEl = document.querySelector('nav'); let headerHeight = 80; // default if (navigationEl) { headerHeight = navigationEl.offsetHeight; // Add extra padding for mobile to ensure content is visible headerHeight += isMobile ? 30 : 20; } // Use getBoundingClientRect for more accurate positioning const elementRect = element.getBoundingClientRect(); const currentScrollY = window.scrollY; const targetY = currentScrollY + elementRect.top - headerHeight; // Smooth scroll with fallback for older browsers try { window.scrollTo({ top: Math.max(0, targetY), behavior: 'smooth' }); } catch (error) { // Fallback for browsers that don't support smooth scrolling window.scrollTo(0, Math.max(0, targetY)); } // Update hash for browser navigation window.history.pushState(null, '', `#${sectionId}`); }, []); const handleToggleMenu = useCallback((e?: React.MouseEvent) => { e?.preventDefault(); setIsOpen(prev => !prev); }, []); // Close mobile menu when clicking outside or on overlay useEffect(() => { const handleClickOutside = (event: MouseEvent) => { const nav = document.querySelector('nav'); if (isOpen && nav && !nav.contains(event.target as Node)) { setIsOpen(false); } }; const handleEscapeKey = (event: KeyboardEvent) => { if (event.key === 'Escape' && isOpen) { setIsOpen(false); } }; if (isOpen) { document.addEventListener('mousedown', handleClickOutside); document.addEventListener('keydown', handleEscapeKey); // Prevent body scroll when menu is open on mobile document.body.style.overflow = 'hidden'; } else { document.body.style.overflow = 'unset'; } return () => { document.removeEventListener('mousedown', handleClickOutside); document.removeEventListener('keydown', handleEscapeKey); document.body.style.overflow = 'unset'; }; }, [isOpen]); return (
{/* Logo with optimized animations */} {/* Desktop Menu */}
{menuItems.map((item) => ( ))} {/* Product Pages */}
{/* Contact Info - Compact on laptop, full on desktop */}
{/* Language Selector */}
{/* Mobile Menu Button */}
{/* Mobile Menu Overlay */} {isOpen && ( <> {/* Overlay backdrop */} setIsOpen(false)} /> {/* Mobile Menu */}
{menuItems.map((item) => ( { e.preventDefault(); handleNavigation(item.href, true); }} className="text-left py-2 px-3 text-foreground hover:text-primary hover:bg-accent/50 transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-primary/50 rounded-lg w-full font-medium text-base active:bg-accent" aria-label={t('aria.go_to_section', { section: item.name })} > {item.name} ))}
setIsOpen(false)} className="w-full" > setIsOpen(false)} className="w-full" >
{/* Mobile Language Selector */}
)}
); }); Navigation.displayName = 'Navigation'; export default Navigation;