- Menu items with gold astrological marks (☉ ☽ ☌, card, box); the free pages (signs, famous people, moon calendar, retrogrades) in one "Felfedezés" dropdown with a line about each, opened by a click - A starry header band with a gold edge; the current item has a glowing gold underline - "Ma: Hold a ♉ Bikában" with the Moon's phase drawn as it is lit, to the moon calendar (narrower screens: the phase and the glyph) - The ☰ menu grouped: the Moon today, Saját, Felfedezés · ingyenes as tiles, Csomagok with the package; it covers the page now - Laptops (1440px) keep the row on one line Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
138 lines
3.6 KiB
TypeScript
138 lines
3.6 KiB
TypeScript
"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>
|
|
);
|
|
}
|