/**
* @license
* Copyright 2025-present Ariakit FZ-LLC. All Rights Reserved.
*
* This software is proprietary. See the license.md file in the root of this
* package for licensing terms.
*
* SPDX-License-Identifier: UNLICENSED
*/
import * as ak from "@ariakit/react";
import {
HovercardAnchor,
HovercardProvider,
} from "@ariakit/ui/components/hovercard.ariakit.react";
import {
Nav,
NavDisclosure,
NavDisclosureGroup,
NavGroup,
NavGroupLabel,
NavLink,
NavLinkLabel,
NavList,
NavSlot,
} from "@ariakit/ui/components/nav.ariakit.react";
import { useId, useState } from "react";
import { AppNavigationCard } from "#app/components/app-navigation-card.react.tsx";
import type { AppLink, AppNavigationGroup } from "#app/lib/app.ts";
import { isCurrentLink } from "#app/lib/app.ts";
import { recipeCategories } from "#app/lib/recipe-categories.ts";
import type { RecipeCategory } from "#app/lib/recipe-categories.ts";
import { recipeIcons } from "#app/lib/recipe-icons.ts";
import { usePathname } from "#app/lib/use-page.react.ts";
// An island that stays across pages gets the props of the next page a little
// after the client router shows that page. This returns the path of the page
// that shows while the props are for another page, given the path of the page
// that the props are for.
function useNextPathname(pathname: string) {
const pagePathname = usePathname();
if (pagePathname === pathname) return null;
return pagePathname;
}
// Until the props of the next page come, the links follow the path of that
// page, so the bar of the current link moves with the page.
function getPageLinks(links: AppLink[], nextPathname: string | null) {
if (nextPathname == null) return links;
return links.map((link) => {
const current = isCurrentLink(link, nextPathname);
if (current === link.current) return link;
return { ...link, current };
});
}
export interface AppNavigationProps {
groups: AppNavigationGroup[];
/**
* The path of the page that the current links of the groups are for.
*/
pathname: string;
/**
* Links listed before the groups, such as the site sections. They are current
* only as their `current` says.
*/
links?: AppLink[];
/**
* Whether each group shows in a disclosure. When `false`, all groups show
* their links under their labels. Defaults to `true`.
*/
collapsible?: boolean;
/**
* Whether the navigation fills the height of its flex column container. The
* open group then takes the space that the closed groups leave, and scrolls
* its links when they need more space. Use it only with collapsible groups.
*/
fill?: boolean;
}
/**
* The site's navigation links, with each group in a disclosure by default. Only
* one group opens at a time, starting with the group of the current page.
* Render it inside a labelled navigation landmark, such as a `ShellSidebar`
* rendered as `nav`. When the mouse pointer rests on a link with `examples`, a
* card next to it shows them. The links share the card, which slides from one
* link to the next.
*/
export function AppNavigation({
groups: groupsProp,
pathname,
links,
collapsible = true,
fill,
}: AppNavigationProps) {
const nextPathname = useNextPathname(pathname);
const groups = groupsProp.map((group) => {
return { ...group, links: getPageLinks(group.links, nextPathname) };
});
const currentGroup = groups.find((group) =>
group.links.some((link) => link.current),
);
const currentLabel = currentGroup?.label;
const [openLabel, setOpenLabel] = useState(currentLabel ?? groups[0]?.label);
// The navigation stays across pages. The group of the next page opens when it
// differs from the group of the last page.
const [lastCurrentLabel, setLastCurrentLabel] = useState(currentLabel);
if (currentLabel !== lastCurrentLabel) {
setLastCurrentLabel(currentLabel);
setOpenLabel(currentLabel ?? groups[0]?.label);
}
const cardLinks = groups
.flatMap((group) => group.links)
.filter((link) => !!link.examples?.length);
const nav = (
);
if (!cardLinks.length) {
return nav;
}
return (
// The card opens beside the sidebar, centered on the link. It shows after
// half the default timeout of a hovercard, and hides a little sooner than a
// hovercard does by default.
{nav}
);
}
export interface AppSectionNavigationProps {
links: AppLink[];
/**
* The path of the page that the current links are for.
*/
pathname: string;
}
/**
* The links to the site sections in the header. The bar travels from the last
* current section to the next when the links change without a page load.
*/
export function AppSectionNavigation({
links,
pathname,
}: AppSectionNavigationProps) {
const pageLinks = getPageLinks(links, useNextPathname(pathname));
return (
);
}
interface AppNavigationLinksProps {
links: AppLink[];
}
/**
* Renders the links of a navigation group. Adjacent links with the same
* category show in a nav group under the category label.
*/
function AppNavigationLinks({ links }: AppNavigationLinksProps) {
const categories = getLinkCategories(links);
return categories.map(({ category, links }) => {
const list = (
{links.map((link) => (
))}
);
if (!category) {
return list;
}
return (
{recipeCategories[category]}
{list}
);
});
}
interface AppLinkCategory {
category?: RecipeCategory;
links: AppLink[];
}
/** Splits the links into runs of adjacent links with the same category. */
function getLinkCategories(links: AppLink[]) {
const categories: AppLinkCategory[] = [];
for (const link of links) {
const last = categories.at(-1);
if (last && last.category === link.category) {
last.links.push(link);
continue;
}
categories.push({ category: link.category, links: [link] });
}
return categories;
}
interface AppNavigationLinkProps {
link: AppLink;
}
function AppNavigationLink({ link }: AppNavigationLinkProps) {
const countId = useId();
// A link with examples opens the card. A scroll of the sidebar that brings
// the link under a resting mouse pointer counts as hovering it, so the card
// follows the pointer while the links scroll. A click on the link hides the
// card, since the page of the link loads next.
const anchor = link.examples?.length ? (
) : undefined;
// The link of the card looks hovered while the pointer moves onto the card.
// Links outside a card provider, such as in the drawer, have no store.
const store = ak.useHovercardContext();
const showsCard = ak.useStoreState(store, (state) => {
if (!state?.open) return false;
return state.anchorElement?.getAttribute("href") === link.href;
});
const Icon = link.icon ? recipeIcons[link.icon] : undefined;
return (
{Icon && (
)}
{link.label}
{link.count != null && (
// A text slot keeps the count at the end of the first line. The count
// describes the link, so it stays out of the name, which is the page
// title. A hidden element still describes the link.
{link.count}
{link.count === 1 ? " example" : " examples"}
)}
);
}