"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { useEffect, useState } from "react";
import Logo from "./logo";
import { localeNames, localeShort, locales, localize, splitLocale, type Locale } from "@/i18n/config";
import type { NavItem } from "@/i18n/nav";
export type HeaderLabels = {
primary: string;
bookCall: string;
openMenu: string;
closeMenu: string;
language: string;
logoHome: string;
};
function LanguageSwitch({ locale, path, label }: { locale: Locale; path: string; label: string }) {
return (
);
}
export default function SiteHeader({ locale, nav, t }: { locale: Locale; nav: NavItem[]; t: HeaderLabels }) {
const pathname = usePathname();
const { path } = splitLocale(pathname);
const current = localize(path, locale);
const [scrolled, setScrolled] = useState(false);
const [hidden, setHidden] = useState(false);
const [open, setOpen] = useState(false);
const [menuPath, setMenuPath] = useState(pathname);
// Close the mobile menu whenever the route changes.
if (menuPath !== pathname) {
setMenuPath(pathname);
setOpen(false);
}
// Drop focus left on a nav link after navigating so its dropdown doesn't stay pinned open.
useEffect(() => {
const el = document.activeElement;
if (el instanceof HTMLElement && el.closest(".site-header")) el.blur();
}, [pathname]);
useEffect(() => {
let lastY = window.scrollY;
const onScroll = () => {
const y = window.scrollY;
setScrolled(y > 24);
setHidden(y > 480 && y > lastY + 4);
if (y < lastY - 4 || y < 480) setHidden(false);
lastY = y;
};
onScroll();
window.addEventListener("scroll", onScroll, { passive: true });
return () => window.removeEventListener("scroll", onScroll);
}, []);
useEffect(() => {
document.body.style.overflow = open ? "hidden" : "";
const onKey = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false);
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [open]);
const isActive = (href: string) => current === href || current.startsWith(href + "/");
const contactHref = localize("/contact", locale);
const classes = ["site-header", scrolled || open ? "is-scrolled" : "", hidden && !open ? "is-hidden" : ""]
.filter(Boolean)
.join(" ");
return (
<>