forked from fahrPool/fahrPool
61 lines
2.3 KiB
TypeScript
61 lines
2.3 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef } from "react";
|
|
import { usePathname } from "next/navigation";
|
|
import { istAktiv, type NavZiel } from "@/lib/navigation";
|
|
import { AktiverLink } from "./AktiverLink";
|
|
|
|
const LINK =
|
|
"flex min-h-11 items-center text-asphalt-grey hover:text-performance-blue focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-performance-blue";
|
|
|
|
// Ausklappmenue auf Basis von <details>: schliesst nach Seitenwechsel, Klick
|
|
// auf einen Eintrag, Escape oder Klick ausserhalb (das Layout bleibt beim
|
|
// clientseitigen Navigieren erhalten, ohne das bliebe es offen stehen).
|
|
export function VerwaltungMenue({ ziele }: { ziele: NavZiel[] }) {
|
|
const ref = useRef<HTMLDetailsElement>(null);
|
|
const pfad = usePathname() ?? "";
|
|
const aktiv = ziele.some((z) => istAktiv(pfad, z.href));
|
|
|
|
useEffect(() => {
|
|
if (ref.current) ref.current.open = false;
|
|
}, [pfad]);
|
|
|
|
useEffect(() => {
|
|
function aussen(event: PointerEvent) {
|
|
if (ref.current?.open && !ref.current.contains(event.target as Node)) ref.current.open = false;
|
|
}
|
|
function taste(event: KeyboardEvent) {
|
|
if (event.key === "Escape" && ref.current?.open) {
|
|
ref.current.open = false;
|
|
ref.current.querySelector("summary")?.focus();
|
|
}
|
|
}
|
|
document.addEventListener("pointerdown", aussen);
|
|
document.addEventListener("keydown", taste);
|
|
return () => {
|
|
document.removeEventListener("pointerdown", aussen);
|
|
document.removeEventListener("keydown", taste);
|
|
};
|
|
}, []);
|
|
|
|
return (
|
|
<details ref={ref} className="relative">
|
|
<summary className={`${LINK} cursor-pointer list-none ${aktiv ? "font-semibold text-performance-blue" : ""}`}>
|
|
Verwaltung ▾
|
|
</summary>
|
|
<div
|
|
onClick={(event) => {
|
|
if ((event.target as HTMLElement).closest("a") && ref.current) ref.current.open = false;
|
|
}}
|
|
className="absolute right-0 z-50 mt-2 flex w-56 flex-col gap-1 rounded-md border border-aluminium-grey bg-white p-3 shadow-lg"
|
|
>
|
|
{ziele.map((ziel) => (
|
|
<AktiverLink key={ziel.href} href={ziel.href} className={LINK} aktivClassName="font-semibold text-performance-blue">
|
|
{ziel.label}
|
|
</AktiverLink>
|
|
))}
|
|
</div>
|
|
</details>
|
|
);
|
|
}
|