{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "site-toc-scrollspy",
  "type": "registry:component",
  "title": "Table of Contents Scrollspy",
  "description": "A sticky table of contents whose active item follows the page as you scroll, marked by an indicator that slides between entries. Clicking an entry scrolls to its section.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "site",
    "toc",
    "scrollspy",
    "navigation",
    "docs",
    "sticky",
    "intersection observer"
  ],
  "docs": "https://hyperblur.dev/effects/site-toc-scrollspy",
  "files": [
    {
      "path": "registry/hyperblur/site-toc-scrollspy.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/site-toc-scrollspy.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface TocItem {\n  id: string;\n  label: string;\n  level?: 1 | 2;\n}\n\nexport interface SiteTocScrollspyProps\n  extends Omit<React.HTMLAttributes<HTMLElement>, \"onChange\" | \"title\"> {\n  items?: TocItem[];\n  root?: React.RefObject<HTMLElement | null> | HTMLElement | null;\n  activeId?: string;\n  defaultActiveId?: string;\n  onActiveChange?: (id: string) => void;\n  offset?: number;\n  title?: string;\n  sticky?: boolean;\n  stickyTop?: number;\n  accent?: string;\n}\n\nconst defaultItems: TocItem[] = [\n  { id: \"overview\", label: \"Overview\" },\n  { id: \"install\", label: \"Installation\" },\n  { id: \"usage\", label: \"Usage\" },\n  { id: \"props\", label: \"Props\", level: 2 },\n  { id: \"faq\", label: \"FAQ\" },\n];\n\nconst css = `\n.hb-yf7 {\n  box-sizing: border-box;\n  width: 100%;\n  font-family: inherit;\n  font-size: 13px;\n  line-height: 1.35;\n  color: #ededed;\n}\n.hb-yf7 *, .hb-yf7 *::before, .hb-yf7 *::after { box-sizing: border-box; font-family: inherit; }\n.hb-yf7[data-sticky=\"true\"] { position: sticky; top: var(--hb-yf7-top, 24px); align-self: flex-start; }\n.hb-yf7-title { display: flex; align-items: center; gap: 7px; margin: 0 0 10px; font-size: 11px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: #8f8f8f; }\n.hb-yf7-title svg { width: 12px; height: 12px; }\n.hb-yf7-list { position: relative; }\n.hb-yf7-ul { margin: 0; padding: 0; list-style: none; }\n.hb-yf7-list::before { content: \"\"; position: absolute; left: 0; top: 0; bottom: 0; width: 1px; background: #262626; }\n.hb-yf7-ind {\n  position: absolute;\n  left: -1px;\n  top: 0;\n  width: 3px;\n  height: 0;\n  border-radius: 3px;\n  background: var(--hb-yf7-a, #2f7bff);\n  box-shadow: 0 0 12px color-mix(in srgb, var(--hb-yf7-a, #2f7bff) 70%, transparent);\n  opacity: 0;\n  transform: translateY(0);\n  transition: transform 0.35s cubic-bezier(0.34, 1.2, 0.5, 1), height 0.35s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.2s ease;\n}\n.hb-yf7-ind[data-ready=\"true\"] { opacity: 1; }\n.hb-yf7-link {\n  display: block;\n  padding: 5px 8px 5px 14px;\n  color: #8f8f8f;\n  text-decoration: none;\n  border-radius: 0 6px 6px 0;\n  transition: color 0.2s ease, background-color 0.2s ease, transform 0.25s ease;\n}\n.hb-yf7-link[data-level=\"2\"] { padding-left: 26px; font-size: 12.5px; }\n.hb-yf7-link:hover { color: #ededed; }\n.hb-yf7-link[aria-current=\"location\"] { color: #ededed; font-weight: 500; transform: translateX(2px); }\n.hb-yf7-link:focus-visible { outline: 2px solid var(--hb-yf7-a, #2f7bff); outline-offset: -2px; }\n@media (prefers-reduced-motion: reduce) {\n  .hb-yf7-ind, .hb-yf7-link { transition: none; }\n}\n`;\n\nfunction resolveRoot(root: SiteTocScrollspyProps[\"root\"]): HTMLElement | null {\n  if (!root) return null;\n  if (typeof (root as HTMLElement).getBoundingClientRect === \"function\") return root as HTMLElement;\n  return (root as React.RefObject<HTMLElement | null>).current ?? null;\n}\n\nconst thresholds = Array.from({ length: 21 }, (_, i) => i / 20);\n\nexport function SiteTocScrollspy({\n  items = defaultItems,\n  root,\n  activeId,\n  defaultActiveId,\n  onActiveChange,\n  offset = 16,\n  title = \"On this page\",\n  sticky = true,\n  stickyTop = 24,\n  accent = \"#2f7bff\",\n  className = \"\",\n  style,\n  ...rest\n}: SiteTocScrollspyProps) {\n  const [inner, setInner] = React.useState(defaultActiveId ?? items[0]?.id ?? \"\");\n  const controlled = activeId !== undefined;\n  const active = controlled ? activeId : inner;\n  const listRef = React.useRef<HTMLDivElement>(null);\n  const indRef = React.useRef<HTMLSpanElement>(null);\n  const last = React.useRef(active);\n  const notify = React.useRef(onActiveChange);\n  const linkRefs = React.useRef(new Map<string, HTMLAnchorElement>());\n\n  React.useEffect(() => {\n    notify.current = onActiveChange;\n  }, [onActiveChange]);\n\n  React.useEffect(() => {\n    last.current = active;\n  }, [active]);\n\n  React.useLayoutEffect(() => {\n    const list = listRef.current;\n    const ind = indRef.current;\n    if (!list || !ind) return;\n    function place() {\n      const el = linkRefs.current.get(active)?.parentElement;\n      if (!el || !ind) return;\n      ind.style.transform = `translateY(${el.offsetTop}px)`;\n      ind.style.height = `${el.offsetHeight}px`;\n      ind.dataset.ready = \"true\";\n    }\n    place();\n    const ro = new ResizeObserver(place);\n    ro.observe(list);\n    return () => ro.disconnect();\n  }, [active, items]);\n\n  React.useEffect(() => {\n    const rootEl = resolveRoot(root);\n    const scope: ParentNode = rootEl ?? document;\n    const targets = items\n      .map((it) => ({ id: it.id, el: scope.querySelector<HTMLElement>(`#${CSS.escape(it.id)}`) }))\n      .filter((t): t is { id: string; el: HTMLElement } => t.el !== null);\n    if (targets.length === 0) return;\n\n    function compute() {\n      const top = (rootEl ? rootEl.getBoundingClientRect().top : 0) + offset;\n      let current = targets[0].id;\n      for (const t of targets) {\n        if (t.el.getBoundingClientRect().top <= top + 1) current = t.id;\n        else break;\n      }\n      const scroller = rootEl ?? document.scrollingElement;\n      if (scroller && scroller.scrollTop > 0 && scroller.scrollTop + scroller.clientHeight >= scroller.scrollHeight - 2) {\n        current = targets[targets.length - 1].id;\n      }\n      if (current === last.current) return;\n      last.current = current;\n      setInner(current);\n      notify.current?.(current);\n    }\n\n    const io = new IntersectionObserver(compute, { root: rootEl, threshold: thresholds });\n    targets.forEach((t) => io.observe(t.el));\n    const target: HTMLElement | Window = rootEl ?? window;\n    target.addEventListener(\"scroll\", compute, { passive: true });\n    return () => {\n      io.disconnect();\n      target.removeEventListener(\"scroll\", compute);\n    };\n  }, [items, root, offset]);\n\n  function go(e: React.MouseEvent<HTMLAnchorElement>, id: string) {\n    const rootEl = resolveRoot(root);\n    const el = (rootEl ?? document).querySelector<HTMLElement>(`#${CSS.escape(id)}`);\n    if (!el) return;\n    e.preventDefault();\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const behavior: ScrollBehavior = reduce ? \"auto\" : \"smooth\";\n    if (rootEl) {\n      const delta = el.getBoundingClientRect().top - rootEl.getBoundingClientRect().top - offset;\n      rootEl.scrollTo({ top: rootEl.scrollTop + delta, behavior });\n    } else {\n      window.scrollTo({ top: window.scrollY + el.getBoundingClientRect().top - offset, behavior });\n    }\n    if (!controlled) setInner(id);\n    last.current = id;\n    notify.current?.(id);\n  }\n\n  return (\n    <>\n      <style href=\"hb-site-toc-scrollspy\" precedence=\"default\">{css}</style>\n      <nav\n        aria-label={title}\n        className={`hb-yf7 ${className}`}\n        data-sticky={sticky}\n        style={{ \"--hb-yf7-a\": accent, \"--hb-yf7-top\": `${stickyTop}px`, ...style } as React.CSSProperties}\n        {...rest}\n      >\n        <p className=\"hb-yf7-title\">\n          <svg viewBox=\"0 0 12 12\" fill=\"none\" aria-hidden=\"true\">\n            <path d=\"M1.5 3h9M1.5 6h6M1.5 9h8\" stroke=\"currentColor\" strokeWidth=\"1.3\" strokeLinecap=\"round\" />\n          </svg>\n          {title}\n        </p>\n        <div className=\"hb-yf7-list\" ref={listRef}>\n          <span className=\"hb-yf7-ind\" ref={indRef} aria-hidden=\"true\" />\n          <ul className=\"hb-yf7-ul\">\n          {items.map((it) => (\n            <li key={it.id}>\n              <a\n                href={`#${it.id}`}\n                className=\"hb-yf7-link\"\n                data-level={it.level ?? 1}\n                aria-current={it.id === active ? \"location\" : undefined}\n                ref={(node) => {\n                  if (node) linkRefs.current.set(it.id, node);\n                  else linkRefs.current.delete(it.id);\n                }}\n                onClick={(e) => go(e, it.id)}\n              >\n                {it.label}\n              </a>\n            </li>\n          ))}\n          </ul>\n        </div>\n      </nav>\n    </>\n  );\n}\n"
    }
  ]
}