Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 1x 2x 2x 2x 2x 2x 2x | import { MyAppSectionLink } from "@/components/app/MyAppSections";
import SectionLink from "@/components/buttons/SectionLink/SectionLink";
import { ChevronDown } from "lucide-react";
import { useState } from "react";
import { useLocation } from "react-router-dom";
interface NavItemProps {
link: MyAppSectionLink;
}
const NavItem = ({ link }: NavItemProps) => {
const location = useLocation();
const hasSubpages = !!link.subpages?.length;
const isChildActive =
link.subpages?.some((sub) => location.pathname.startsWith(sub.route)) ??
false;
const [expanded, setExpanded] = useState(isChildActive);
Eif (!hasSubpages) {
return <SectionLink href={link.route}>{link.title}</SectionLink>;
}
return (
<div>
<div className="flex items-center justify-between gap-1">
<SectionLink href={link.route}>{link.title}</SectionLink>
<button
onClick={() => setExpanded((e) => !e)}
className="shrink-0 p-1 opacity-40 hover:opacity-80 transition-opacity cursor-pointer"
aria-label={expanded ? "Collapse" : "Expand"}
>
<ChevronDown
className={`size-3 transition-transform duration-200 ${
expanded ? "rotate-180" : ""
}`}
/>
</button>
</div>
{expanded && (
<div className="ml-3 mt-0.5 border-l border-current/25 pl-2 flex flex-col">
{(link.subpages ?? []).map((sub) => {
const subpageLinks = (sub.subpages ?? []).map((subSub) => (
<SectionLink
href={subSub.route}
key={subSub.route}
indentLevel={2}
>
{subSub.title}
</SectionLink>
));
return [
<SectionLink href={sub.route} key={sub.route} indentLevel={1}>
{sub.title}
</SectionLink>,
].concat(subpageLinks);
})}
</div>
)}
</div>
);
};
export default NavItem;
|