{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scrollspy-anchor-links",
  "title": "Scrollspy Anchor Links",
  "description": "A sticky horizontal bar of section links (Overview, Features, Specs, Reviews) that follows the section in view with a sliding pill or underline, keeps the current link scrolled into view on phones, and fills a progress line as you read.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/scrollspy-anchor-links/scrollspy-anchor-links.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type ScrollspyVariant = \"pills\" | \"underline\";\nexport type ScrollspySize = \"sm\" | \"md\" | \"lg\";\n\nexport interface ScrollspyItem {\n  /** The id of the section this link scrolls to. */\n  id: string;\n  label: string;\n}\n\nexport interface ScrollspyAnchorLinksProps {\n  items: ScrollspyItem[];\n  /** The element that scrolls; defaults to the page. */\n  scrollRef?: React.RefObject<HTMLElement | null>;\n  /** Distance from the top at which a section counts as current, usually the bar's own height plus any header. */\n  offset?: number;\n  variant?: ScrollspyVariant;\n  /** Stick the bar to the top while its sections scroll past. */\n  sticky?: boolean;\n  stickyTop?: number;\n  /** A thin line along the bottom that fills as you move through the sections. */\n  showProgress?: boolean;\n  updateHash?: boolean;\n  size?: ScrollspySize;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.55)\", bar: \"rgba(12,12,12,0.78)\", border: \"rgba(255,255,255,0.1)\", pill: \"#F5F4F1\", pillText: \"#0A0A0A\", line: \"#F5F4F1\", track: \"rgba(255,255,255,0.08)\", focus: \"rgba(245,244,241,0.85)\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.55)\", bar: \"rgba(255,255,255,0.82)\", border: \"rgba(10,10,10,0.1)\", pill: \"#0A0A0A\", pillText: \"#FFFFFF\", line: \"#0A0A0A\", track: \"rgba(10,10,10,0.08)\", focus: \"rgba(10,10,10,0.85)\" },\n};\n\nconst SIZES: Record<ScrollspySize, { height: number; font: number; padX: number }> = {\n  sm: { height: 44, font: 13, padX: 12 },\n  md: { height: 52, font: 14, padX: 14 },\n  lg: { height: 60, font: 15.5, padX: 16 },\n};\n\nexport function ScrollspyAnchorLinks({\n  items,\n  scrollRef,\n  offset = 72,\n  variant = \"pills\",\n  sticky = true,\n  stickyTop = 0,\n  showProgress = true,\n  updateHash = false,\n  size = \"md\",\n  theme = \"dark\",\n  className,\n}: ScrollspyAnchorLinksProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const reduce = Boolean(useReducedMotion());\n  const trackRef = React.useRef<HTMLUListElement>(null);\n  const [activeId, setActiveId] = React.useState(items[0]?.id ?? null);\n  const [progress, setProgress] = React.useState(0);\n\n  // Current section: the last one whose top has crossed the offset line (the last one at the very\n  // bottom). Progress runs from the first section's top to the end of the scroll area.\n  React.useEffect(() => {\n    if (!items.length) return;\n    const root = scrollRef?.current ?? null;\n    const target: HTMLElement | Window = root ?? window;\n    let raf = 0;\n    const update = () => {\n      raf = 0;\n      const top = root ? root.getBoundingClientRect().top : 0;\n      const scrollTop = root ? root.scrollTop : window.scrollY;\n      const max = root ? root.scrollHeight - root.clientHeight : document.documentElement.scrollHeight - window.innerHeight;\n      let current = items[0].id;\n      for (const it of items) {\n        const el = document.getElementById(it.id);\n        if (el && el.getBoundingClientRect().top - top <= offset + 1) current = it.id;\n      }\n      if (max > 0 && scrollTop >= max - 2) current = items[items.length - 1].id;\n      setActiveId(current);\n      const first = document.getElementById(items[0].id);\n      const scale = root ? root.getBoundingClientRect().height / root.offsetHeight || 1 : 1;\n      const start = first ? scrollTop + (first.getBoundingClientRect().top - top) / scale - offset : 0;\n      setProgress(max > start ? Math.min(1, Math.max(0, (scrollTop - start) / (max - start))) : 0);\n    };\n    const onScroll = () => {\n      if (!raf) raf = requestAnimationFrame(update);\n    };\n    update();\n    target.addEventListener(\"scroll\", onScroll, { passive: true });\n    window.addEventListener(\"resize\", onScroll);\n    return () => {\n      target.removeEventListener(\"scroll\", onScroll);\n      window.removeEventListener(\"resize\", onScroll);\n      if (raf) cancelAnimationFrame(raf);\n    };\n  }, [items, scrollRef, offset]);\n\n  // Keep the current link in view when the bar is narrower than its links (phones).\n  React.useEffect(() => {\n    const track = trackRef.current;\n    const link = track?.querySelector<HTMLElement>(`[data-spy-id=\"${CSS.escape(activeId ?? \"\")}\"]`);\n    if (!track || !link) return;\n    const left = link.offsetLeft - (track.clientWidth - link.offsetWidth) / 2;\n    track.scrollTo({ left: Math.max(0, left), behavior: reduce ? \"auto\" : \"smooth\" });\n  }, [activeId, reduce]);\n\n  const go = (e: React.MouseEvent<HTMLAnchorElement>, id: string) => {\n    const el = document.getElementById(id);\n    if (!el) return;\n    e.preventDefault();\n    const root = scrollRef?.current;\n    if (root) {\n      const scale = root.getBoundingClientRect().height / root.offsetHeight || 1;\n      root.scrollTo({ top: root.scrollTop + (el.getBoundingClientRect().top - root.getBoundingClientRect().top) / scale - offset, behavior: reduce ? \"auto\" : \"smooth\" });\n    } else {\n      window.scrollTo({ top: window.scrollY + el.getBoundingClientRect().top - offset, behavior: reduce ? \"auto\" : \"smooth\" });\n    }\n    if (updateHash) history.replaceState(null, \"\", `#${id}`);\n    setActiveId(id);\n  };\n\n  const pills = variant === \"pills\";\n\n  return (\n    <nav\n      aria-label=\"On this page\"\n      className={cn(sticky ? \"sticky\" : \"relative\", \"z-30 w-full\", className)}\n      style={{ top: sticky ? stickyTop : undefined, fontFamily: \"Inter, sans-serif\", background: p.bar, backdropFilter: \"blur(16px) saturate(160%)\", WebkitBackdropFilter: \"blur(16px) saturate(160%)\", borderBottom: `1px solid ${p.border}` }}\n    >\n      <ul ref={trackRef} className=\"m-0 flex list-none items-center overflow-x-auto p-0\" style={{ height: s.height, gap: pills ? 4 : 18, padding: `0 ${pills ? 8 : 16}px`, scrollbarWidth: \"none\" }}>\n        {items.map((it) => {\n          const active = it.id === activeId;\n          return (\n            <li key={it.id} data-spy-id={it.id} className=\"relative flex h-full shrink-0 items-center\">\n              <a\n                href={`#${it.id}`}\n                onClick={(e) => go(e, it.id)}\n                aria-current={active ? \"location\" : undefined}\n                className=\"relative flex items-center whitespace-nowrap no-underline outline-none focus-visible:ring-2\"\n                style={{\n                  height: pills ? s.height - 16 : \"100%\",\n                  padding: pills ? `0 ${s.padX}px` : 0,\n                  borderRadius: 999,\n                  fontSize: s.font,\n                  fontWeight: active ? 600 : 500,\n                  color: active ? (pills ? p.pillText : p.text) : p.muted,\n                  transition: \"color 0.2s ease\",\n                  [\"--tw-ring-color\" as string]: p.focus,\n                }}\n              >\n                {active && pills && <motion.span layoutId={reduce ? undefined : `${uid}-pill`} className=\"absolute inset-0\" style={{ borderRadius: 999, background: p.pill }} transition={{ type: \"spring\", stiffness: 480, damping: 38 }} />}\n                {active && !pills && <motion.span layoutId={reduce ? undefined : `${uid}-line`} className=\"absolute inset-x-0 bottom-0\" style={{ height: 2, borderRadius: 2, background: p.line }} transition={{ type: \"spring\", stiffness: 480, damping: 38 }} />}\n                <span className=\"relative\">{it.label}</span>\n              </a>\n            </li>\n          );\n        })}\n      </ul>\n      {showProgress && (\n        <div aria-hidden=\"true\" className=\"absolute inset-x-0 bottom-[-1px]\" style={{ height: 1, background: p.track }}>\n          <div className=\"h-full\" style={{ width: \"100%\", background: p.line, opacity: 0.6, transformOrigin: \"0 50%\", transform: `scaleX(${progress})` }} />\n        </div>\n      )}\n    </nav>\n  );\n}\n\nexport default ScrollspyAnchorLinks;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}