"use client"; import { use, useRef } from "react"; import { usePathname } from "next/navigation"; import { IsInsideMobileNavigationContext } from "@/components/MobileNavigation"; import { useSectionStore } from "@/components/SectionProvider"; import { Tag } from "@/components/Tag"; import { remToPx } from "@/lib/remToPx"; import { NavGroup, navigation } from "@/site-config"; import clsx from "clsx"; import { AnimatePresence, motion, useIsPresent } from "motion/react"; import { Link } from "next-view-transitions"; function useInitialValue(value: T, condition = true) { let initialValue = useRef(value).current; return condition ? initialValue : value; } function TopLevelNavItem({ href, children, }: { href: string; children: React.ReactNode; }) { return (
  • {children}
  • ); } export function NavLink({ href, children, tag, active = false, isAnchorLink = false, }: { href: string; children: React.ReactNode; tag?: string; active?: boolean; isAnchorLink?: boolean; }) { return ( {children} {tag && ( {tag} )} ); } export function VisibleSectionHighlight({ group, pathname, noHeading, }: { group: NavGroup; pathname: string; noHeading?: boolean; }) { let [sections, visibleSections] = useInitialValue( [ useSectionStore((s) => s.sections), useSectionStore((s) => s.visibleSections), ], use(IsInsideMobileNavigationContext), ); let isPresent = useIsPresent(); let firstVisibleSectionIndex = Math.max( 0, [{ id: "_top" }, ...sections].findIndex( (section) => section.id === visibleSections[0], ), ); let itemHeight = remToPx(2); let height = isPresent ? Math.max(1, visibleSections.length) * itemHeight : itemHeight; if (noHeading) { height -= itemHeight; } let top = group.links.findIndex((link) => link.href === pathname) * itemHeight + firstVisibleSectionIndex * itemHeight; return ( ); } function ActivePageMarker({ group, pathname, }: { group: NavGroup; pathname: string; }) { let itemHeight = remToPx(2); let offset = remToPx(0.25); let activePageIndex = group.links.findIndex((link) => link.href === pathname); let top = offset + activePageIndex * itemHeight; return ( ); } function NavigationGroup({ group, className, }: { group: NavGroup; className?: string; }) { // If this is the mobile navigation then we always render the initial // state, so that the state does not change during the close animation. // The state will still update when we re-open (re-render) the navigation. let isInsideMobileNavigation = use(IsInsideMobileNavigationContext); let [pathname, sections] = useInitialValue( [usePathname(), useSectionStore((s) => s.sections)], isInsideMobileNavigation, ); let isActiveGroup = group.links.findIndex((link) => link.href === pathname) !== -1; return (
  • {group.title}
    {isActiveGroup && ( )} {isActiveGroup && ( )}
      {group.links.map((link) => ( {link.title} {link.href === pathname && sections.length > 0 && ( {sections.map((section) => (
    • {section.title}
    • ))}
      )}
      ))}
  • ); } export function Navigation(props: React.ComponentPropsWithoutRef<"nav">) { return ( ); }