Files
sorsfonalak/services/frontend/src/components/NavLinks.tsx
T

138 lines
3.6 KiB
TypeScript
Raw Normal View History

"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import { useEffect, useId, useRef, useState } from "react";
import { NavIcon, type NavIconName } from "./NavIcon";
export interface NavItem {
href: string;
label: string;
icon: NavIconName;
/** One line under the label in a dropdown. */
note?: string;
}
export interface NavGroup {
label: string;
icon: NavIconName;
/** The dropdown's small heading. */
title: string;
items: NavItem[];
}
const isGroup = (e: NavItem | NavGroup): e is NavGroup => "items" in e;
/**
* The app's menu row: items with a gold mark, and a group (the free pages)
* that opens on a click as a dropdown. Closes on a click outside, Escape or
* moving to another page.
*/
export function NavLinks({ entries }: { entries: (NavItem | NavGroup)[] }) {
const pathname = usePathname();
const here = (href: string) => pathname.startsWith(href);
return (
<nav className="nav app-nav" aria-label="Fő navigáció">
{entries.map((e) =>
isGroup(e) ? (
<Dropdown
key={e.label}
group={e}
active={e.items.some((i) => here(i.href))}
here={here}
/>
) : (
<Link
key={e.href}
href={e.href}
aria-current={here(e.href) ? "page" : undefined}
>
<NavIcon name={e.icon} />
{e.label}
</Link>
),
)}
</nav>
);
}
function Dropdown({
group,
active,
here,
}: {
group: NavGroup;
active: boolean;
here: (href: string) => boolean;
}) {
const [open, setOpen] = useState(false);
const box = useRef<HTMLDivElement>(null);
const button = useRef<HTMLButtonElement>(null);
const id = useId();
const pathname = usePathname();
useEffect(() => setOpen(false), [pathname]);
useEffect(() => {
if (!open) return;
const outside = (e: PointerEvent) => {
if (box.current && !box.current.contains(e.target as Node))
setOpen(false);
};
const escape = (e: KeyboardEvent) => {
if (e.key === "Escape") {
setOpen(false);
button.current?.focus();
}
};
document.addEventListener("pointerdown", outside);
document.addEventListener("keydown", escape);
return () => {
document.removeEventListener("pointerdown", outside);
document.removeEventListener("keydown", escape);
};
}, [open]);
return (
<div className="nav-group" ref={box}>
<button
ref={button}
type="button"
className={active ? "nav-group-button is-active" : "nav-group-button"}
aria-expanded={open}
aria-controls={id}
onClick={() => setOpen((o) => !o)}
>
<NavIcon name={group.icon} />
{group.label}
<svg
viewBox="0 0 24 24"
aria-hidden="true"
className="nav-group-chevron"
>
<path d="M6 9l6 6 6-6" />
</svg>
</button>
{open && (
<div id={id} className="nav-dropdown">
<span className="nav-dropdown-title">{group.title}</span>
{group.items.map((i) => (
<Link
key={i.href}
href={i.href}
className="nav-dropdown-item"
aria-current={here(i.href) ? "page" : undefined}
>
<NavIcon name={i.icon} />
<span className="nav-dropdown-text">
<span>{i.label}</span>
{i.note && <span className="hint">{i.note}</span>}
</span>
</Link>
))}
</div>
)}
</div>
);
}