forked from fahrPool/fahrPool
79 lines
3.1 KiB
TypeScript
79 lines
3.1 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { usePathname } from "next/navigation";
|
|
import { MENUE_OEFFNEN } from "@/components/navigation/UntereLeiste";
|
|
|
|
export function MobileNav({ children }: { children: React.ReactNode }) {
|
|
const pfad = usePathname();
|
|
// Das Menue merkt sich die Seite, auf der es geoeffnet wurde: nach einem
|
|
// Seitenwechsel ueber einen Menue-Link gilt es damit automatisch als geschlossen.
|
|
const [offenAuf, setOffenAuf] = useState<string | null>(null);
|
|
const open = offenAuf !== null && offenAuf === pfad;
|
|
const setOpen = (wert: boolean | ((alt: boolean) => boolean)) =>
|
|
setOffenAuf((alt) => {
|
|
const war = alt !== null && alt === pfad;
|
|
return (typeof wert === "function" ? wert(war) : wert) ? pfad : null;
|
|
});
|
|
|
|
// "Mehr" in der unteren Leiste oeffnet dieses Menue.
|
|
useEffect(() => {
|
|
const oeffnen = () => setOffenAuf(pfad);
|
|
window.addEventListener(MENUE_OEFFNEN, oeffnen);
|
|
return () => window.removeEventListener(MENUE_OEFFNEN, oeffnen);
|
|
}, [pfad]);
|
|
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
function onKeyDown(event: KeyboardEvent) {
|
|
if (event.key === "Escape") setOffenAuf(null);
|
|
}
|
|
window.addEventListener("keydown", onKeyDown);
|
|
return () => window.removeEventListener("keydown", onKeyDown);
|
|
}, [open]);
|
|
|
|
return (
|
|
<div className="lg:hidden">
|
|
<button
|
|
type="button"
|
|
aria-label={open ? "Menü schließen" : "Menü öffnen"}
|
|
aria-expanded={open}
|
|
onClick={() => setOpen((value) => !value)}
|
|
className="flex min-h-11 min-w-11 items-center justify-center rounded-md text-asphalt-grey focus-visible:outline focus-visible:outline-2 focus-visible:outline-performance-blue"
|
|
>
|
|
<svg viewBox="0 0 24 24" className="h-6 w-6" fill="none" stroke="currentColor" strokeWidth={2}>
|
|
{open ? (
|
|
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
|
|
) : (
|
|
<path strokeLinecap="round" strokeLinejoin="round" d="M4 6h16M4 12h16M4 18h16" />
|
|
)}
|
|
</svg>
|
|
</button>
|
|
|
|
{open && (
|
|
<div className="fixed inset-0 z-50">
|
|
<button
|
|
type="button"
|
|
aria-label="Menü schließen"
|
|
onClick={() => setOpen(false)}
|
|
className="absolute inset-0 bg-black/40"
|
|
/>
|
|
<div className="absolute right-0 top-0 h-full w-72 max-w-[85vw] overflow-y-auto bg-white p-4 shadow-lg">
|
|
<button
|
|
type="button"
|
|
aria-label="Menü schließen"
|
|
onClick={() => setOpen(false)}
|
|
className="mb-4 ml-auto flex min-h-11 min-w-11 items-center justify-center rounded-md text-asphalt-grey focus-visible:outline focus-visible:outline-2 focus-visible:outline-performance-blue"
|
|
>
|
|
<svg viewBox="0 0 24 24" className="h-6 w-6" fill="none" stroke="currentColor" strokeWidth={2}>
|
|
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
|
|
</svg>
|
|
</button>
|
|
{children}
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|