"use client"; import Link from "next/link"; import { usePathname } from "next/navigation"; import { useEffect, useId, useRef, useState } from "react"; export interface MenuSection { /** A small heading above the group (e.g. "Ingyenes"). */ title?: string; links: { href: string; label: string }[]; } /** * The menu behind the ☰ button on narrow screens (the header's link rows are * hidden there, see globals.css). Closes on a click outside, Escape, or * moving to another page. `className` sets the width from which it shows. */ export function MobileMenu({ sections, className }: { sections: MenuSection[]; className: string }) { const [open, setOpen] = useState(false); const box = useRef(null); const button = useRef(null); const panelId = useId(); const pathname = usePathname(); useEffect(() => setOpen(false), [pathname]); useEffect(() => { if (!open) return; const outside = (e: PointerEvent) => { if (box.current && !box.current.contains(e.target as Node)) setOpen(false); }; const escape = (e: KeyboardEvent) => { if (e.key === "Escape") { setOpen(false); button.current?.focus(); } }; document.addEventListener("pointerdown", outside); document.addEventListener("keydown", escape); return () => { document.removeEventListener("pointerdown", outside); document.removeEventListener("keydown", escape); }; }, [open]); const current = (href: string) => !href.includes("#") && pathname.startsWith(href); return (
{open && ( )}
); }