SPB Git

Repository Public

SectionNav.tsx 2.9 KB 79 lines tsx

1// Navigation de sections collante, synchronisée avec le défilement.23import { useEffect, useRef, useState } from "react";4import { LayoutGroup } from "motion/react";5import { cn } from "@/lib/cn";6import { SlideMark } from "@/lib/motion";78export interface NavItem {9  id: string;10  label: string;11}1213export function SectionNav({ items }: { items: NavItem[] }) {14  const [active, setActive] = useState(items[0]?.id ?? "");15  const bar = useRef<HTMLDivElement>(null);16  const key = items.map((i) => i.id).join("|");1718  useEffect(() => {19    const els = items.map((i) => document.getElementById(i.id)).filter(Boolean) as HTMLElement[];20    if (!els.length) return;21    let raf = 0;22    const update = () => {23      raf = 0;24      const line = 150; // sous l'en-tête collant (64 px) et la barre de sections25      let cur = els[0]!.id;26      for (const el of els) if (el.getBoundingClientRect().top <= line) cur = el.id;27      setActive(cur);28    };29    const on = () => {30      if (!raf) raf = requestAnimationFrame(update);31    };32    update();33    window.addEventListener("scroll", on, { passive: true });34    window.addEventListener("resize", on);35    return () => {36      window.removeEventListener("scroll", on);37      window.removeEventListener("resize", on);38      if (raf) cancelAnimationFrame(raf);39    };40    // la liste des sections est stable pour une propriété donnée41    // eslint-disable-next-line react-hooks/exhaustive-deps42  }, [key]);4344  // garde l'onglet actif visible dans la barre défilante45  useEffect(() => {46    const el = bar.current?.querySelector<HTMLElement>(`[data-id="${active}"]`);47    const box = bar.current;48    if (el && box) box.scrollTo({ left: el.offsetLeft - box.clientWidth / 2 + el.clientWidth / 2, behavior: "smooth" });49  }, [active]);5051  return (52    <nav aria-label="Sections de la fiche" className="sticky top-16 z-30 -mx-[clamp(1rem,4vw,2.5rem)] mt-4 border-b border-line bg-bg lg:mt-6">53      <LayoutGroup id="sections">54      <div ref={bar} className="no-scrollbar flex overflow-x-auto pl-[calc(clamp(1rem,4vw,2.5rem)-0.75rem)] pr-[clamp(1rem,4vw,2.5rem)]">55        {items.map((it) => (56          <a57            key={it.id}58            href={`#${it.id}`}59            data-id={it.id}60            aria-current={active === it.id ? "true" : undefined}61            onClick={(e) => {62              e.preventDefault();63              document.getElementById(it.id)?.scrollIntoView({ behavior: "smooth", block: "start" });64            }}65            className={cn(66              "relative inline-flex min-h-11 shrink-0 items-center px-3 text-[0.88rem] transition-colors duration-150",67              active === it.id ? "font-medium text-ink" : "text-ink-3 hover:text-ink",68            )}69          >70            {it.label}71            {active === it.id && <SlideMark id="section-mark" className="absolute inset-x-3 -bottom-px h-0.5 bg-accent" />}72          </a>73        ))}74      </div>75      </LayoutGroup>76    </nav>77  );78}79
Files main
Menu

Theme