105 lines
3.8 KiB
TypeScript
105 lines
3.8 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
import Link from "next/link";
|
|
import { usePathname } from "next/navigation";
|
|
import { Menu, X } from "lucide-react";
|
|
|
|
import type { NavItem } from "@/components/layout/portal-shell";
|
|
import { LogoutButton } from "@/components/layout/logout-button";
|
|
import { ThemeToggle } from "@/components/theme-toggle";
|
|
|
|
type MobilePortalMenuProps = {
|
|
nav: NavItem[];
|
|
sectionLabel: string;
|
|
};
|
|
|
|
export function MobilePortalMenu({ nav, sectionLabel }: Readonly<MobilePortalMenuProps>) {
|
|
const pathname = usePathname();
|
|
const [open, setOpen] = useState(false);
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
const triggerRef = useRef<HTMLButtonElement>(null);
|
|
const menuId = "mobile-portal-menu";
|
|
|
|
const activeHref = useMemo(
|
|
() => nav
|
|
.filter(({ href }) => pathname === href || pathname.startsWith(`${href}/`))
|
|
.sort((left, right) => right.href.length - left.href.length)[0]?.href,
|
|
[nav, pathname],
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
|
|
function closeOnOutsidePointer(event: PointerEvent) {
|
|
if (!containerRef.current?.contains(event.target as Node)) setOpen(false);
|
|
}
|
|
|
|
function closeOnEscape(event: KeyboardEvent) {
|
|
if (event.key !== "Escape") return;
|
|
setOpen(false);
|
|
triggerRef.current?.focus();
|
|
}
|
|
|
|
document.addEventListener("pointerdown", closeOnOutsidePointer);
|
|
document.addEventListener("keydown", closeOnEscape);
|
|
return () => {
|
|
document.removeEventListener("pointerdown", closeOnOutsidePointer);
|
|
document.removeEventListener("keydown", closeOnEscape);
|
|
};
|
|
}, [open]);
|
|
|
|
return (
|
|
<div ref={containerRef} className="relative sm:hidden">
|
|
<button
|
|
ref={triggerRef}
|
|
type="button"
|
|
aria-controls={menuId}
|
|
aria-expanded={open}
|
|
aria-label={open ? "Menü schließen" : "Menü öffnen"}
|
|
className="inline-flex min-h-11 items-center gap-2 rounded-md border border-border bg-background px-3 text-sm font-semibold"
|
|
onClick={() => setOpen((current) => !current)}
|
|
>
|
|
{open ? <X aria-hidden="true" className="size-5" /> : <Menu aria-hidden="true" className="size-5" />}
|
|
Menü
|
|
</button>
|
|
|
|
{open ? (
|
|
<div
|
|
id={menuId}
|
|
className="mobile-portal-menu-panel absolute right-0 top-[calc(100%+0.75rem)] z-50 max-h-[calc(100dvh-5.5rem)] w-[min(22rem,calc(100vw-2rem))] overflow-y-auto overscroll-contain rounded-xl border border-border bg-background p-2 shadow-xl"
|
|
>
|
|
<p className="px-3 py-2 text-xs font-bold uppercase tracking-widest text-[var(--muted)]">
|
|
{sectionLabel}
|
|
</p>
|
|
<nav aria-label="Mobile Navigation" className="grid gap-1">
|
|
{nav.map((item) => {
|
|
const active = item.href === activeHref;
|
|
return (
|
|
<Link
|
|
key={item.href}
|
|
href={item.href}
|
|
aria-current={active ? "page" : undefined}
|
|
className={active
|
|
? "mobile-portal-menu-active flex min-h-11 items-center rounded-lg px-3 font-semibold"
|
|
: "flex min-h-11 items-center rounded-lg px-3 font-medium hover:bg-[var(--soft)]"}
|
|
onClick={() => setOpen(false)}
|
|
>
|
|
{item.label}
|
|
</Link>
|
|
);
|
|
})}
|
|
</nav>
|
|
<div className="mt-2 flex min-h-11 items-center justify-between border-t border-border px-3 pt-2 [&>button]:min-h-11 [&>button]:min-w-11">
|
|
<span className="text-sm text-[var(--muted)]">Darstellung</span>
|
|
<ThemeToggle />
|
|
</div>
|
|
<div className="mt-2 border-t border-border px-3 pt-2 [&>button]:min-h-11">
|
|
<LogoutButton />
|
|
</div>
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|