|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useRef, useState } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { t, type Locale } from "@/lib/i18n"; |
| 6 | + |
| 7 | +const links = [ |
| 8 | + { href: "/how-it-works", key: "nav.howItWorks" }, |
| 9 | + { href: "/check-image", key: "nav.checkImage" }, |
| 10 | + { href: "/resources", key: "nav.resources" }, |
| 11 | + { href: "/faq", key: "nav.faq" }, |
| 12 | + { href: "/privacy", key: "nav.privacy" }, |
| 13 | +] as const; |
| 14 | + |
| 15 | +export function MobileMenu({ locale }: { locale: Locale }) { |
| 16 | + const [open, setOpen] = useState(false); |
| 17 | + const wrapRef = useRef<HTMLDivElement>(null); |
| 18 | + |
| 19 | + // Close on outside tap or Escape. (Each link also closes the menu on click.) |
| 20 | + useEffect(() => { |
| 21 | + if (!open) return; |
| 22 | + function onPointer(e: PointerEvent) { |
| 23 | + if (wrapRef.current && !wrapRef.current.contains(e.target as Node)) { |
| 24 | + setOpen(false); |
| 25 | + } |
| 26 | + } |
| 27 | + function onKey(e: KeyboardEvent) { |
| 28 | + if (e.key === "Escape") setOpen(false); |
| 29 | + } |
| 30 | + document.addEventListener("pointerdown", onPointer); |
| 31 | + document.addEventListener("keydown", onKey); |
| 32 | + return () => { |
| 33 | + document.removeEventListener("pointerdown", onPointer); |
| 34 | + document.removeEventListener("keydown", onKey); |
| 35 | + }; |
| 36 | + }, [open]); |
| 37 | + |
| 38 | + return ( |
| 39 | + <div className="relative" ref={wrapRef}> |
| 40 | + <button |
| 41 | + type="button" |
| 42 | + className="btn btn-secondary px-2.5" |
| 43 | + aria-expanded={open} |
| 44 | + aria-controls="mobile-menu" |
| 45 | + aria-label={t(locale, "nav.menu")} |
| 46 | + onClick={() => setOpen((v) => !v)} |
| 47 | + > |
| 48 | + <svg |
| 49 | + width="22" |
| 50 | + height="22" |
| 51 | + viewBox="0 0 24 24" |
| 52 | + fill="none" |
| 53 | + stroke="currentColor" |
| 54 | + strokeWidth="2" |
| 55 | + strokeLinecap="round" |
| 56 | + strokeLinejoin="round" |
| 57 | + aria-hidden |
| 58 | + > |
| 59 | + {open ? ( |
| 60 | + <> |
| 61 | + <path d="M18 6 6 18" /> |
| 62 | + <path d="m6 6 12 12" /> |
| 63 | + </> |
| 64 | + ) : ( |
| 65 | + <> |
| 66 | + <path d="M3 6h18" /> |
| 67 | + <path d="M3 12h18" /> |
| 68 | + <path d="M3 18h18" /> |
| 69 | + </> |
| 70 | + )} |
| 71 | + </svg> |
| 72 | + </button> |
| 73 | + {open && ( |
| 74 | + <nav |
| 75 | + id="mobile-menu" |
| 76 | + className="panel absolute right-0 top-14 z-30 grid w-56 gap-3 p-4 text-sm font-bold text-[var(--muted)]" |
| 77 | + > |
| 78 | + {links.map((link) => ( |
| 79 | + <Link key={link.href} href={link.href} onClick={() => setOpen(false)}> |
| 80 | + {t(locale, link.key)} |
| 81 | + </Link> |
| 82 | + ))} |
| 83 | + <Link href="/start" className="btn btn-primary" onClick={() => setOpen(false)}> |
| 84 | + {t(locale, "nav.start")} |
| 85 | + </Link> |
| 86 | + </nav> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + ); |
| 90 | +} |
0 commit comments