forked from fahrPool/fahrPool
61 lines
2.2 KiB
TypeScript
61 lines
2.2 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
export function MobileNav({ children }: { children: React.ReactNode }) {
|
|
const [open, setOpen] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
function onKeyDown(event: KeyboardEvent) {
|
|
if (event.key === "Escape") setOpen(false);
|
|
}
|
|
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] 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>
|
|
);
|
|
}
|