import { NavigationMenu, NavigationMenuContent, NavigationMenuItem, NavigationMenuLink, NavigationMenuList, NavigationMenuTrigger } from '@components/common/ui/NavigationMenu.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { cn } from '@evershop/evershop/lib/util/cn'; import { shiftIntoView } from '@evershop/evershop/lib/util/shiftIntoView'; import { ChevronDown, Menu, X } from 'lucide-react'; import React from 'react'; /** * Coerce a list setting into an array. Menu settings normally arrive as a * real array, but a half-configured widget can persist them as a JSON * string (the legacy editor seeds list fields as stringified hidden * inputs) — guard so `.map`/`.length` never run on a string. */ function toMenuArray(value: unknown): T[] { if (Array.isArray(value)) return value as T[]; if (typeof value === 'string') { try { const parsed = JSON.parse(value); return Array.isArray(parsed) ? (parsed as T[]) : []; } catch { return []; } } return []; } interface BasicMenuLinkFlags { newTab?: boolean; nofollow?: boolean; noReferrer?: boolean; } /** * Compose anchor `rel`. New-tab links always carry noopener+noreferrer * (tab-nabbing protection); nofollow and same-tab noreferrer are independent * opt-ins. Undefined when no token applies so we don't emit an empty rel. */ function relFor(link: BasicMenuLinkFlags): string | undefined { const tokens = [ link.newTab ? 'noopener' : null, link.newTab || link.noReferrer ? 'noreferrer' : null, link.nofollow ? 'nofollow' : null ].filter(Boolean); return tokens.length > 0 ? tokens.join(' ') : undefined; } interface BasicMenuProps { basicMenuWidget: { menus: ({ id: string; name: string; url: string; type: string; uuid: string; children: ({ id: string; name: string; url: string; type: string; uuid: string; } & BasicMenuLinkFlags)[]; } & BasicMenuLinkFlags)[]; isMain: boolean; className: string; }; } type BasicMenuNode = BasicMenuProps['basicMenuWidget']['menus'][number]; /** * A plain anchor that applies a link's new-tab / SEO flags. Split into two * literal branches so `react/jsx-no-target-blank` can statically verify the * new-tab branch carries `noopener noreferrer`. */ function MenuAnchor({ link, className, children }: { link: BasicMenuLinkFlags & { url: string }; className?: string; children: React.ReactNode; }) { return link.newTab ? ( {children} ) : ( {children} ); } /** * The mobile list of every root item + its sub-items. Used both inside the * hamburger panel (main menus) and rendered inline (non-main menus, e.g. a * simple footer link list). */ function MobileItemList({ items, isActive }: { items: BasicMenuNode[]; isActive: (url: string) => boolean; }) { return ( ); } export default function BasicMenu({ basicMenuWidget: { menus, isMain, className } }: BasicMenuProps) { const [mobileOpen, setMobileOpen] = React.useState(false); const mobilePanelRef = React.useRef(null); const [currentPath, setCurrentPath] = React.useState(''); // The panel hangs from the toggle's left edge (`left-0`). That is right for a toggle at the page's // left and runs off the right of a phone for any other (a centred logo is the commonest header // there is; theme-lab FINDINGS #53). So once it is open, measure where it landed and move it back // inside the viewport. The `translate` property, not `transform`: a theme that positions the panel // with its own `transform` keeps it, and a panel that already fits is left exactly where it was. React.useLayoutEffect(() => { const panel = mobilePanelRef.current; if (!mobileOpen || !panel) { return undefined; } const fit = () => { panel.style.translate = ''; const { left, right } = panel.getBoundingClientRect(); const dx = shiftIntoView(left, right, document.documentElement.clientWidth); if (dx !== 0) { panel.style.translate = `${dx}px 0`; } }; fit(); window.addEventListener('resize', fit); return () => window.removeEventListener('resize', fit); }, [mobileOpen]); React.useEffect(() => { setCurrentPath(window.location.pathname); }, []); const menuItems = toMenuArray(menus).map((item) => ({ ...item, children: toMenuArray(item.children) })); const isActive = (url: string) => url === '/' ? currentPath === '/' : !!url && currentPath.startsWith(url); return (
{/* Desktop (md+): horizontal menu; a parent with children opens a hover/focus dropdown. The trigger itself is a real link. */} {/* Mobile ( {isMain ? ( <> {mobileOpen && (
)} ) : (
)}
); } export const query = ` query Query($settings: JSON) { basicMenuWidget(settings: $settings) { menus { id name url type uuid newTab nofollow noReferrer children { id name url type uuid newTab nofollow noReferrer } } isMain className } } `; export const variables = `{ settings: getWidgetSetting() }`;