Skip to documentation
Design system / Sources & standards
Current repository source
section-navigation.tsx
src/components/navigation/section-navigation.tsx
This view contains the escaped source read during the deployment build. Code and policy text are unchanged. Markdown is shown as source, not rendered as a document. A new deployment refreshes the content.
"use client";
import { useId, useState } from "react";
import { ChevronDown } from "lucide-react";
import { cn } from "@/lib/utils";
import type { SectionNavigationItem, SectionNavigationProps } from "./types";
import { navLinkStyles } from "./nav-link-styles";
import { SectionNavigationLink } from "./section-navigation-link";
import { SectionNavigationSubmenu } from "./section-navigation-submenu";
const orientations = {
vertical: "grid grid-cols-1 gap-1",
horizontal: "",
responsive: "md:grid md:grid-cols-1 md:gap-1",
} as const;
/** Navigation-owned organism: supplied destinations/selection, local disclosure.
* No URL reads, product copy, authorization, effects or transport. Mobile panels
* sit below the scrolling primary strip; desktop children follow their owner in
* DOM and visual order. CSS exposes one presentation, with shared disclosure and
* distinct control IDs; no viewport effect or scripted focus reordering. */
export function SectionNavigation({
items,
activeId,
activeChildId,
orientation = "vertical",
className,
"aria-label": ariaLabel = "Sections",
...props
}: SectionNavigationProps) {
const id = useId();
const [disclosure, setDisclosure] = useState({
scope: activeId,
ids: activeId ? [activeId] : ([] as string[]),
});
if (disclosure.scope !== activeId) {
setDisclosure({ scope: activeId, ids: activeId ? [activeId] : [] });
}
// Reset only when entering a different supplied section, not between child
// pages. Updating this component's render state avoids a post-paint effect.
const expanded =
disclosure.scope === activeId ? disclosure.ids : activeId ? [activeId] : [];
function toggle(itemId: string) {
setDisclosure({
scope: activeId,
ids: expanded.includes(itemId)
? expanded.filter((value) => value !== itemId)
: [...expanded, itemId],
});
}
function row(item: SectionNavigationItem, presentation: string) {
return (
<div
key={`row-${item.id}`}
data-slot="section-navigation-row"
className="flex min-w-0 shrink-0 items-center gap-1"
>
<SectionNavigationLink
item={item}
current={activeId === item.id && !activeChildId}
branch={activeId === item.id && !!activeChildId}
/>
{!!item.children?.length && (
<button
type="button"
aria-label={`${item.label} submenu`}
aria-expanded={expanded.includes(item.id)}
aria-controls={`${id}-${presentation}-${item.id}`}
onClick={() => toggle(item.id)}
className={cn(
"shrink-0 rounded-md p-2 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
navLinkStyles(),
)}
>
<ChevronDown
aria-hidden
className={cn(
"size-4",
expanded.includes(item.id) && "rotate-180",
)}
/>
</button>
)}
</div>
);
}
function submenu(
item: SectionNavigationItem,
presentation: string,
className?: string,
) {
return (
!!item.children?.length && (
<SectionNavigationSubmenu
key={`submenu-${item.id}`}
id={`${id}-${presentation}-${item.id}`}
aria-label={`${item.label} submenu`}
hidden={!expanded.includes(item.id)}
className={className}
items={item.children}
activeId={activeId === item.id ? activeChildId : undefined}
/>
)
);
}
function compact(responsive: boolean) {
return (
<div
data-navigation-layout="compact"
className={responsive ? "md:hidden" : undefined}
>
<div
data-slot="section-navigation-list"
className="flex gap-1 overflow-x-auto pb-2"
>
{items.map((item) => row(item, "compact"))}
</div>
{items.map((item) =>
submenu(item, "compact", "max-h-[30dvh] overflow-y-auto"),
)}
</div>
);
}
function nested(responsive: boolean) {
return (
<div
data-navigation-layout="nested"
className={responsive ? "hidden md:block" : undefined}
>
<div
data-slot="section-navigation-list"
className="grid grid-cols-1 gap-1"
>
{items.map((item) => (
<div key={item.id} className="min-w-0 space-y-1">
{row(item, "nested")}
{submenu(item, "nested")}
</div>
))}
</div>
</div>
);
}
return (
<nav
{...props}
aria-label={ariaLabel}
data-slot="section-navigation"
className={cn("min-w-0", orientations[orientation], className)}
>
{orientation !== "vertical" && compact(orientation === "responsive")}
{orientation !== "horizontal" && nested(orientation === "responsive")}
</nav>
);
}
All sources and standards