{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scroll-progress-bar",
  "title": "Scroll Progress Bar",
  "description": "Reading progress in three forms: a thin spring-smoothed bar, a corner ring with the percentage that becomes a back-to-top button at the end, or a track split into your page's sections that labels the current one and jumps to any other.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/scroll-progress-bar/scroll-progress-bar.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useMotionValue, useReducedMotion, useSpring, useTransform } 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 interface ScrollProgressBarProps {\n  /** bar: thin line. circle: ring with a percentage that turns into back-to-top. sections: one segment per heading. */\n  variant?: \"bar\" | \"circle\" | \"sections\";\n  /** Where the bar or the sections sit. */\n  position?: \"top\" | \"bottom\";\n  /** Headings that split the page into sections (sections variant). */\n  sectionSelector?: string;\n  height?: number;\n  color?: string;\n  /** Follow the nearest scrollable ancestor instead of the window. */\n  contained?: boolean;\n  /** Follow this scroll box instead of the window; place the indicator in a positioned parent around it. */\n  container?: React.RefObject<HTMLElement | null>;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { fill: \"#F5F4F1\", track: \"rgba(255,255,255,0.1)\", text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.55)\", panel: \"rgba(14,14,14,0.85)\", border: \"rgba(255,255,255,0.12)\", focus: \"rgba(245,244,241,0.85)\" },\n  light: { fill: \"#0A0A0A\", track: \"rgba(10,10,10,0.1)\", text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.55)\", panel: \"rgba(255,255,255,0.88)\", border: \"rgba(10,10,10,0.12)\", focus: \"rgba(10,10,10,0.85)\" },\n};\n\nfunction scrollParent(el: HTMLElement | null): HTMLElement | null {\n  for (let n = el?.parentElement; n; n = n.parentElement) {\n    const o = getComputedStyle(n).overflowY;\n    if ((o === \"auto\" || o === \"scroll\") && n.scrollHeight > n.clientHeight) return n;\n  }\n  return null;\n}\n\ntype Section = { label: string; start: number; end: number; el: HTMLElement };\n\nexport function ScrollProgressBar({ variant = \"bar\", position = \"top\", sectionSelector = \"h2\", height = 3, color, contained = false, container, theme = \"dark\", className }: ScrollProgressBarProps) {\n  const p = PALETTES[theme];\n  const reduce = Boolean(useReducedMotion());\n  const ref = React.useRef<HTMLDivElement>(null);\n  const boxRef = React.useRef<HTMLElement | null>(null);\n  const raw = useMotionValue(0);\n  const smooth = useSpring(raw, { stiffness: 220, damping: 32, restDelta: 0.0005 });\n  const progress = reduce ? raw : smooth;\n  const [pct, setPct] = React.useState(0);\n  const [sections, setSections] = React.useState<Section[]>([]);\n  const fill = color ?? p.fill;\n\n  React.useEffect(() => {\n    const box = container?.current ?? (contained ? scrollParent(ref.current) : null);\n    boxRef.current = box;\n    const read = () => {\n      const y = box ? box.scrollTop : window.scrollY;\n      const max = box ? box.scrollHeight - box.clientHeight : document.documentElement.scrollHeight - window.innerHeight;\n      const v = max > 0 ? Math.min(1, Math.max(0, y / max)) : 0;\n      raw.set(v);\n      setPct(Math.round(v * 100));\n    };\n    const measure = () => {\n      if (variant !== \"sections\") return;\n      const root: ParentNode = box ?? document;\n      const heads = Array.from(root.querySelectorAll<HTMLElement>(sectionSelector));\n      const max = box ? box.scrollHeight - box.clientHeight : document.documentElement.scrollHeight - window.innerHeight;\n      if (!heads.length || max <= 0) return setSections([]);\n      const top = (el: HTMLElement) => (box ? el.getBoundingClientRect().top - box.getBoundingClientRect().top + box.scrollTop : el.getBoundingClientRect().top + window.scrollY);\n      const starts = heads.map((h) => Math.min(1, Math.max(0, (top(h) - 80) / max)));\n      starts[0] = 0;\n      setSections(heads.map((el, i) => ({ label: el.textContent?.trim() ?? `Section ${i + 1}`, start: starts[i], end: i < heads.length - 1 ? starts[i + 1] : 1, el })));\n    };\n    const target: HTMLElement | Window = box ?? window;\n    target.addEventListener(\"scroll\", read, { passive: true });\n    window.addEventListener(\"resize\", measure);\n    const ro = new ResizeObserver(measure);\n    if (box) ro.observe(box);\n    read();\n    measure();\n    return () => {\n      target.removeEventListener(\"scroll\", read);\n      window.removeEventListener(\"resize\", measure);\n      ro.disconnect();\n    };\n  }, [contained, container, variant, sectionSelector, raw]);\n\n  const scrollTo = (y: number) => {\n    const box = boxRef.current;\n    if (box) box.scrollTo({ top: y, behavior: reduce ? \"auto\" : \"smooth\" });\n    else window.scrollTo({ top: y, behavior: reduce ? \"auto\" : \"smooth\" });\n  };\n\n  const scaleX = useTransform(progress, [0, 1], [0, 1]);\n  const ring = useTransform(progress, [0, 1], [0, 1]);\n  // Inside a scroll box it has to be sticky to stay put; beside one (container) absolute is enough.\n  const pos = container ? \"absolute\" : contained ? \"sticky\" : \"fixed\";\n\n  if (variant === \"circle\") {\n    const done = pct >= 98;\n    return (\n      <div ref={ref} className={cn(pos, \"right-4 bottom-4 z-40\", className)}>\n        <motion.button\n          type=\"button\"\n          onClick={() => scrollTo(0)}\n          aria-label={done ? \"Back to top\" : `${pct}% read. Back to top`}\n          className=\"relative flex cursor-pointer items-center justify-center rounded-full outline-none focus-visible:ring-2\"\n          style={{ width: 52, height: 52, background: p.panel, border: `1px solid ${p.border}`, backdropFilter: \"blur(10px)\", color: p.text, fontFamily: \"Inter, sans-serif\", [\"--tw-ring-color\" as string]: p.focus }}\n          initial={false}\n          animate={{ scale: pct > 1 ? 1 : 0.6, opacity: pct > 1 ? 1 : 0 }}\n          whileHover={reduce ? undefined : { scale: 1.06 }}\n          transition={{ type: \"spring\", stiffness: 420, damping: 28 }}\n        >\n          <svg className=\"absolute inset-0 -rotate-90\" viewBox=\"0 0 52 52\" aria-hidden=\"true\">\n            <circle cx=\"26\" cy=\"26\" r=\"22\" fill=\"none\" stroke={p.track} strokeWidth=\"2.5\" />\n            <motion.circle cx=\"26\" cy=\"26\" r=\"22\" fill=\"none\" stroke={fill} strokeWidth=\"2.5\" strokeLinecap=\"round\" style={{ pathLength: ring }} />\n          </svg>\n          <AnimatePresence mode=\"popLayout\" initial={false}>\n            {done ? (\n              <motion.svg key=\"up\" width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\" initial={{ y: 8, opacity: 0 }} animate={{ y: 0, opacity: 1 }} exit={{ y: -8, opacity: 0 }}>\n                <path d=\"M12 19V5M6 11l6-6 6 6\" />\n              </motion.svg>\n            ) : (\n              <motion.span key=\"pct\" className=\"relative text-[12px] font-semibold tabular-nums\" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }}>\n                {pct}%\n              </motion.span>\n            )}\n          </AnimatePresence>\n        </motion.button>\n      </div>\n    );\n  }\n\n  if (variant === \"sections\") {\n    // The last headings can't reach the top of the screen, so the very end counts as the last section.\n    const atEnd = pct >= 99;\n    const activeIdx = atEnd ? sections.length - 1 : sections.findIndex((s) => pct / 100 >= s.start && pct / 100 < s.end);\n    return (\n      <div ref={ref} className={cn(pos, \"inset-x-0 z-40\", position === \"top\" ? \"top-0\" : \"bottom-0\", className)} style={{ padding: \"8px 12px\", fontFamily: \"Inter, sans-serif\", background: p.panel, backdropFilter: \"blur(12px)\", WebkitBackdropFilter: \"blur(12px)\", [position === \"top\" ? \"borderBottom\" : \"borderTop\"]: `1px solid ${p.border}` }}>\n        <div className=\"flex w-full\" style={{ gap: 4 }} role=\"navigation\" aria-label=\"Sections\">\n          {sections.map((s, i) => {\n            const local = atEnd ? 1 : sectionFill(pct / 100, s.start, s.end);\n            const on = i === activeIdx;\n            return (\n              <button\n                key={i}\n                type=\"button\"\n                onClick={() => {\n                  const box = boxRef.current;\n                  const top = box ? s.el.getBoundingClientRect().top - box.getBoundingClientRect().top + box.scrollTop - 60 : s.el.getBoundingClientRect().top + window.scrollY - 60;\n                  scrollTo(top);\n                }}\n                aria-current={on ? \"step\" : undefined}\n                aria-label={`${s.label}, ${Math.round(local * 100)}% read`}\n                className=\"group relative flex cursor-pointer flex-col items-start border-none bg-transparent p-0 outline-none focus-visible:ring-2\"\n                style={{ flex: `${Math.max(0.04, s.end - s.start)} 1 0`, gap: 6, [\"--tw-ring-color\" as string]: p.focus }}\n              >\n                <span className=\"relative block w-full overflow-hidden rounded-full\" style={{ height, background: p.track }}>\n                  <span className=\"absolute inset-y-0 left-0 block rounded-full\" style={{ width: `${local * 100}%`, background: fill, transition: reduce ? undefined : \"width 0.15s linear\" }} />\n                </span>\n                <span className=\"truncate text-[11.5px] font-medium transition-opacity duration-200\" style={{ maxWidth: \"100%\", color: on ? p.text : p.muted, opacity: on ? 1 : 0.7 }}>\n                  {s.label}\n                </span>\n              </button>\n            );\n          })}\n        </div>\n      </div>\n    );\n  }\n\n  return (\n    <div ref={ref} aria-hidden=\"true\" className={cn(pos, \"inset-x-0 z-40\", position === \"top\" ? \"top-0\" : \"bottom-0\", className)} style={{ height, background: \"transparent\" }}>\n      <motion.div className=\"h-full origin-left\" style={{ scaleX, background: fill, boxShadow: `0 0 12px ${fill}` }} />\n    </div>\n  );\n}\n\n// How much of one section has been read (0–1).\nfunction sectionFill(v: number, start: number, end: number) {\n  if (v <= start) return 0;\n  if (v >= end) return 1;\n  return (v - start) / Math.max(0.0001, end - start);\n}\n\nexport default ScrollProgressBar;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}