@@ -0,0 +1,198 @@
|
||||
import { Link, usePage } from '@inertiajs/react';
|
||||
import { useEffect, useId, useRef, useState } from 'react';
|
||||
import {
|
||||
portalNavigation,
|
||||
currentNavigationGroup,
|
||||
currentNavigationItem,
|
||||
type PortalNavItem,
|
||||
} from '@/lib/portal-navigation';
|
||||
|
||||
export function PortalGroupedMenu() {
|
||||
const { props, url } = usePage<{
|
||||
auth: { user: unknown; canManagePortal: boolean };
|
||||
existingBusinessEnabled?: boolean;
|
||||
educationEnabled?: boolean;
|
||||
aiEnabled?: boolean;
|
||||
portalOperator?: string | null;
|
||||
}>();
|
||||
const groups = portalNavigation({
|
||||
authenticated: !!props.auth?.user,
|
||||
manager: !!props.auth?.canManagePortal,
|
||||
businesses: props.existingBusinessEnabled,
|
||||
education: props.educationEnabled,
|
||||
ai: props.aiEnabled,
|
||||
});
|
||||
const current = currentNavigationGroup(groups, url);
|
||||
const currentItem = currentNavigationItem(groups, url);
|
||||
const [open, setOpen] = useState<string | null>(null);
|
||||
const [branch, setBranch] = useState<string | null>(null);
|
||||
const [offset, setOffset] = useState(0);
|
||||
const root = useRef<HTMLElement>(null);
|
||||
const id = useId();
|
||||
useEffect(() => {
|
||||
const position = () => {
|
||||
const button = open
|
||||
? document.getElementById(`${id}-${open}-button`)
|
||||
: null;
|
||||
if (!button || window.innerWidth < 640) {
|
||||
setOffset(0);
|
||||
return;
|
||||
}
|
||||
const rect = button.getBoundingClientRect();
|
||||
const width = Math.min(288, window.innerWidth - 32);
|
||||
setOffset(
|
||||
Math.max(
|
||||
16,
|
||||
Math.min(rect.left, window.innerWidth - width - 16),
|
||||
) - rect.left,
|
||||
);
|
||||
};
|
||||
position();
|
||||
window.addEventListener('resize', position);
|
||||
return () => window.removeEventListener('resize', position);
|
||||
}, [open, id]);
|
||||
useEffect(() => {
|
||||
const outside = (event: PointerEvent) => {
|
||||
if (
|
||||
event.target instanceof Node &&
|
||||
!root.current?.contains(event.target)
|
||||
) {
|
||||
setOpen(null);
|
||||
setBranch(null);
|
||||
}
|
||||
};
|
||||
document.addEventListener('pointerdown', outside);
|
||||
return () => document.removeEventListener('pointerdown', outside);
|
||||
}, []);
|
||||
const close = () => {
|
||||
setOpen(null);
|
||||
setBranch(null);
|
||||
};
|
||||
const items = (children: PortalNavItem[]) =>
|
||||
children.map((item) => (
|
||||
<li key={item.id}>
|
||||
{item.children ? (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
id={`${id}-${item.id}-button`}
|
||||
className="flex w-full items-center justify-between rounded-lg px-3 py-2 text-left font-semibold hover:bg-emerald-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-emerald-700"
|
||||
aria-expanded={branch === item.id}
|
||||
aria-controls={`${id}-${item.id}`}
|
||||
onClick={() =>
|
||||
setBranch(branch === item.id ? null : item.id)
|
||||
}
|
||||
>
|
||||
{item.label}
|
||||
<span aria-hidden="true">
|
||||
{branch === item.id ? '−' : '+'}
|
||||
</span>
|
||||
</button>
|
||||
{branch === item.id && (
|
||||
<ul
|
||||
id={`${id}-${item.id}`}
|
||||
className="ml-3 space-y-1 border-l-2 border-emerald-200 pl-2"
|
||||
>
|
||||
{items(item.children)}
|
||||
</ul>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<Link
|
||||
href={item.href!}
|
||||
onClick={close}
|
||||
aria-current={
|
||||
currentItem === item.id ? 'page' : undefined
|
||||
}
|
||||
className="block rounded-lg px-3 py-2 hover:bg-emerald-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-emerald-700"
|
||||
>
|
||||
{item.label}
|
||||
</Link>
|
||||
)}
|
||||
</li>
|
||||
));
|
||||
return (
|
||||
<header
|
||||
ref={root}
|
||||
className="relative z-40 border-b border-slate-200 bg-white text-slate-900"
|
||||
onKeyDown={(event) => {
|
||||
if (event.key !== 'Escape') return;
|
||||
event.preventDefault();
|
||||
if (branch) {
|
||||
document.getElementById(`${id}-${branch}-button`)?.focus();
|
||||
setBranch(null);
|
||||
} else if (open) {
|
||||
document.getElementById(`${id}-${open}-button`)?.focus();
|
||||
close();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<div className="mx-auto max-w-7xl px-4 py-4 sm:px-6">
|
||||
<div className="mb-3 flex flex-wrap items-center justify-between gap-2">
|
||||
<Link
|
||||
href="/"
|
||||
className="text-xl font-extrabold tracking-tight text-emerald-900"
|
||||
>
|
||||
InvesteMoz
|
||||
</Link>
|
||||
<p className="max-w-2xl text-xs leading-relaxed text-slate-600">
|
||||
Iniciativa privada e independente de apoio ao
|
||||
investimento. Orientação e acompanhamento internos;
|
||||
decisões oficiais cabem às instituições competentes.
|
||||
{props.portalOperator && (
|
||||
<> Operador: {props.portalOperator}.</>
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
<nav aria-label="Áreas do portal">
|
||||
<ul className="flex flex-wrap gap-2">
|
||||
{groups.map((group) => (
|
||||
<li
|
||||
key={group.id}
|
||||
className="relative w-full sm:w-auto"
|
||||
>
|
||||
<button
|
||||
id={`${id}-${group.id}-button`}
|
||||
type="button"
|
||||
className={`flex min-h-11 w-full items-center justify-between gap-2 rounded-lg border px-3 py-2 text-left text-sm font-semibold focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-emerald-700 ${current === group.id ? 'border-emerald-700 bg-emerald-50 text-emerald-950' : 'border-slate-200 hover:bg-slate-50'}`}
|
||||
aria-expanded={open === group.id}
|
||||
aria-controls={`${id}-${group.id}`}
|
||||
data-current-group={
|
||||
current === group.id || undefined
|
||||
}
|
||||
onClick={() => {
|
||||
setOpen(
|
||||
open === group.id ? null : group.id,
|
||||
);
|
||||
setBranch(null);
|
||||
}}
|
||||
>
|
||||
{group.label}
|
||||
<span aria-hidden="true">
|
||||
{open === group.id ? '▴' : '▾'}
|
||||
</span>
|
||||
{current === group.id && (
|
||||
<span className="sr-only">
|
||||
Grupo actual
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
{open === group.id && (
|
||||
<ul
|
||||
id={`${id}-${group.id}`}
|
||||
style={{
|
||||
transform: `translateX(${offset}px)`,
|
||||
}}
|
||||
className="mt-2 max-h-[min(70vh,32rem)] w-full space-y-1 overflow-y-auto rounded-xl border border-slate-200 bg-white p-2 text-sm shadow-xl sm:absolute sm:top-full sm:left-0 sm:z-40 sm:w-72 sm:max-w-[calc(100vw-2rem)]"
|
||||
>
|
||||
{items(group.children ?? [])}
|
||||
</ul>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user