"use client"; import Link from "next/link"; import { ChevronDown, Headphones, LayoutTemplate, Megaphone, Palette, Search, type LucideIcon, } from "lucide-react"; import { AnimatePresence, motion, useReducedMotion } from "motion/react"; import * as React from "react"; import { createPortal } from "react-dom"; import { cn } from "@/lib/utils"; import { type } from "@/lib/typography"; export type MegaMenuItem = { name: string; href: string; description: string; icon: LucideIcon; }; /** Experimental: service pages not built yet — hrefs stay inert. */ export const serviceMegaItems: MegaMenuItem[] = [ { name: "طراحی سایت در اصفهان", href: "#", description: "وب‌سایت حرفه‌ای با تجربه کاربری مدرن", icon: LayoutTemplate, }, { name: "سئو در اصفهان", href: "#", description: "رتبه بهتر در گوگل و جذب مشتری هدفمند", icon: Search, }, { name: "پشتیبانی سایت", href: "#", description: "نگه‌داری، امنیت و به‌روزرسانی مداوم", icon: Headphones, }, { name: "طراحی گرافیک", href: "#", description: "هویت بصری، لوگو و محتوای گرافیکی", icon: Palette, }, { name: "ارسال انبوه", href: "#", description: "ارسال انبوه پیامک و کمپین‌های دیجیتال", icon: Megaphone, }, ]; const CLOSE_DELAY_MS = 140; const PANEL_ANIMATION_MS = 220; function isLinkActive(pathname: string, href: string): boolean { if (href === "/") return pathname === "/"; if (href.startsWith("/#")) return pathname === "/"; return pathname === href || pathname.startsWith(`${href}/`); } function MegaMenuCard({ item, active, onNavigate, }: { item: MegaMenuItem; active: boolean; onNavigate?: () => void; }) { const Icon = item.icon; return ( {item.name} {item.description} ); } type ServicesMegaMenuProps = { label?: string; pathname: string; triggerClassName?: string; active?: boolean; }; export function ServicesMegaMenu({ label = "خدمات", pathname, triggerClassName, active = false, }: ServicesMegaMenuProps) { const reduceMotion = useReducedMotion(); const [open, setOpen] = React.useState(false); const [mounted, setMounted] = React.useState(false); const [panelStyle, setPanelStyle] = React.useState({}); const rootRef = React.useRef(null); const triggerRef = React.useRef(null); const panelRef = React.useRef(null); const closeTimerRef = React.useRef(null); const groupActive = active || serviceMegaItems.some((item) => isLinkActive(pathname, item.href)); React.useEffect(() => { setMounted(true); }, []); const clearCloseTimer = React.useCallback(() => { if (closeTimerRef.current !== null) { window.clearTimeout(closeTimerRef.current); closeTimerRef.current = null; } }, []); const openMenu = React.useCallback(() => { clearCloseTimer(); setOpen(true); }, [clearCloseTimer]); const closeMenu = React.useCallback(() => { clearCloseTimer(); setOpen(false); }, [clearCloseTimer]); const scheduleClose = React.useCallback(() => { clearCloseTimer(); closeTimerRef.current = window.setTimeout(() => { setOpen(false); }, CLOSE_DELAY_MS); }, [clearCloseTimer]); const updatePanelPosition = React.useCallback(() => { const trigger = triggerRef.current; if (!trigger) return; const rect = trigger.getBoundingClientRect(); const viewportPadding = 16; const panelWidth = Math.min(352, window.innerWidth - viewportPadding * 2); const isTablet = window.innerWidth < 1280; if (isTablet) { setPanelStyle({ top: rect.bottom + 8, left: viewportPadding, right: viewportPadding, width: "auto", }); return; } const inlineStart = rect.right - panelWidth; const clampedStart = Math.max( viewportPadding, Math.min(inlineStart, window.innerWidth - panelWidth - viewportPadding) ); setPanelStyle({ top: rect.bottom + 8, left: clampedStart, width: panelWidth, }); }, []); React.useLayoutEffect(() => { if (!open) return; updatePanelPosition(); const handleLayout = () => updatePanelPosition(); window.addEventListener("resize", handleLayout); window.addEventListener("scroll", handleLayout, { passive: true }); return () => { window.removeEventListener("resize", handleLayout); window.removeEventListener("scroll", handleLayout); }; }, [open, updatePanelPosition]); React.useEffect(() => { if (!open) return; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === "Escape") { event.preventDefault(); closeMenu(); triggerRef.current?.focus(); } }; document.addEventListener("keydown", handleKeyDown); return () => document.removeEventListener("keydown", handleKeyDown); }, [closeMenu, open]); React.useEffect(() => { return () => clearCloseTimer(); }, [clearCloseTimer]); const handleTriggerKeyDown = (event: React.KeyboardEvent) => { if (event.key === "ArrowDown" || event.key === "Enter" || event.key === " ") { event.preventDefault(); openMenu(); requestAnimationFrame(() => { panelRef.current ?.querySelector("a[href]") ?.focus(); }); } }; const panelMotion = reduceMotion ? { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: 0.15, ease: "easeOut" as const }, } : { initial: { opacity: 0, y: -10, scale: 0.98, filter: "blur(10px)" }, animate: { opacity: 1, y: 0, scale: 1, filter: "blur(0px)" }, exit: { opacity: 0, y: -6, scale: 0.99, filter: "blur(4px)" }, transition: { duration: PANEL_ANIMATION_MS / 1000, ease: "easeOut" as const }, }; const overlayMotion = reduceMotion ? { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: 0.15 }, } : { initial: { opacity: 0 }, animate: { opacity: 1 }, exit: { opacity: 0 }, transition: { duration: PANEL_ANIMATION_MS / 1000, ease: "easeOut" as const }, }; const portal = mounted && typeof document !== "undefined" ? createPortal( {open ? ( <>
    {serviceMegaItems.map((item) => (
  • ))}
) : null}
, document.body ) : null; return ( {/* Hover bridge — eliminates gap between trigger and floating panel */} {portal} ); } /** Compact mobile links — title only, no description cards. */ export function MegaMenuMobileCards({ items, pathname, onNavigate, }: { items: MegaMenuItem[]; pathname: string; onNavigate?: () => void; }) { return (
    {items.map((item) => { const active = isLinkActive(pathname, item.href); const Icon = item.icon; return (
  • {item.name}
  • ); })}
); }