"use client"; import * as React from "react"; import type { Falsy } from "./data-slots"; import { cn } from "../lib/cn"; import { UiLink } from "./link"; export interface SectionNavItem { id: string; label: React.ReactNode; /** The section's address. Without one the item is a button that calls * `onClick` (a view that switches in the page, not a link to "#"), or only * words when it has no handler either. */ href?: string; /** How many things the section holds, in a pill after its name. */ count?: React.ReactNode; onClick?: (e: React.MouseEvent) => void; } export interface SectionNavProps { /** tabs: underlined links (default). pills: the links in a white bar, the * current one navy. */ variant?: "tabs" | "pills"; /** Accessible name: "Sezioni del Tracker". */ label: string; items: Array; /** The id of the section the page shows. */ current?: string; className?: string; } // The design system's SectionNav: links between the pages of one area, drawn // like Tabs. An item with an address is a link, so the back button and a // shared link work. One without is a button that calls its handler, for the // two or three views a page switches by itself; with neither it is only // words. Panels with their own content inside one page are `Tabs`. // // `pills` sets the links in a white bar with the current one in navy, for the // two or three views of one page, where an underline is too faint. // // As in `Tabs` the rule of the tabs is an inset shadow, not a border: the row // scrolls sideways when it does not fit, and the current link's line has to // sit on the rule inside the box, where the scroll does not clip it. // // A row that scrolls is brought to its current link when the page opens and // when the section changes: four tabs do not fit a phone, and a page reached // on the last one showed a row with no current tab in sight. function SectionNav({ variant = "tabs", label, items, current, className, }: SectionNavProps) { const pills = variant === "pills"; const navRef = React.useRef(null); React.useEffect(() => { const nav = navRef.current; const on = nav?.querySelector('[aria-current="page"]'); if (!nav || !on) return; // Only the row moves: `scrollIntoView` would scroll the page too. const row = nav.getBoundingClientRect(); const link = on.getBoundingClientRect(); if (link.left < row.left) nav.scrollLeft -= row.left - link.left; else if (link.right > row.right) nav.scrollLeft += link.right - row.right; }, [current]); return ( ); } export { SectionNav };