feat: add accessible mobile portal menu
This commit is contained in:
1 parent
a98c7bc8ee
commit
ec5ed8b22a
2 files changed
+167
No files matched your search
@@ -0,0 +1,104 @@
|
||||
"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="absolute right-0 top-[calc(100%+0.75rem)] z-50 w-[min(22rem,calc(100vw-2rem))] animate-in fade-in slide-in-from-top-2 rounded-xl border border-border bg-background p-2 shadow-xl duration-150 motion-reduce:animate-none"
|
||||
>
|
||||
<p className="px-3 py-2 text-xs font-bold uppercase tracking-widest text-muted-foreground">
|
||||
{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
|
||||
? "flex min-h-11 items-center rounded-lg bg-foreground px-3 font-semibold text-background"
|
||||
: "flex min-h-11 items-center rounded-lg px-3 font-medium hover:bg-muted"}
|
||||
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">
|
||||
<span className="text-sm text-muted-foreground">Darstellung</span>
|
||||
<ThemeToggle />
|
||||
</div>
|
||||
<div className="mt-2 border-t border-border px-3 pt-2">
|
||||
<LogoutButton />
|
||||
</div>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user