{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "reading-progress",
  "type": "registry:block",
  "title": "Reading Progress",
  "description": "A thin bar at the top of the screen that fills as the reader moves through an article.",
  "dependencies": [
    "svg-scroll-draw"
  ],
  "files": [
    {
      "path": "registry/blocks/reading-progress.tsx",
      "content": "'use client';\n\n/**\n * reading-progress — an article with a thin bar fixed to the top of the\n * viewport that fills as the reader moves through it.\n *\n * Built on svg-scroll-draw (MIT): https://svg-scroll-draw.vercel.app/blocks\n * Needs: react, svg-scroll-draw 2.19 or later (`scrollProgress` re-measures\n * when the layout changes, such as images loading in the article; earlier\n * versions re-measure only on a window resize, so the bar would fill before\n * the end). Styling: Tailwind CSS.\n *\n * Uses `scrollProgress` from svg-scroll-draw/progress. It writes the reader's\n * position as a CSS variable, --reading-progress (0 to 1), and the bar's fill is\n * plain CSS, scaleX(var(--reading-progress)), so React does not render per frame.\n * The bar is empty when the article's top reaches the top of the viewport and\n * full when the article's bottom reaches the bottom of the viewport.\n *\n * Pass your article as `children`. The default is a placeholder essay, long on\n * purpose: the bar appears only for an article taller than the viewport, and\n * only while the article is on screen. `kicker`, `title` and `byline` fill the\n * header, `as` sets the title's heading level and `accent` colours the bar.\n *\n * The bar is decorative and aria-hidden: the scroll position already tells\n * assistive technology where the reader is, and a progressbar whose value\n * changed on every frame would only add noise. The library has no\n * reduced-motion path for scrollProgress, so the block sets its own: the fill\n * moves only as far as the reader scrolls, like a scrollbar, so it stays; the\n * fade in and out is removed.\n *\n * `position: fixed` places the bar against the viewport unless an ancestor has a\n * transform, filter, backdrop-filter, perspective or contain (page-transition\n * wrappers often add one), so keep the block out of one. The bar sits on the top\n * edge at z-50; to place it under a fixed site header instead, change `top-0` to\n * the header's height (`top-16` for a 64px header).\n */\n\nimport { useEffect, useRef, useState, type ReactNode } from 'react';\nimport { scrollProgress } from 'svg-scroll-draw/progress';\n\nexport interface ReadingProgressProps {\n  /** The article body. Defaults to a placeholder essay. */\n  children?: ReactNode;\n  kicker?: string;\n  title?: string;\n  /** Line under the title, such as the author and the reading time. */\n  byline?: string;\n  /** Heading level for the title. Use 'h1' when the article is the page's main content. */\n  as?: 'h1' | 'h2';\n  /** Colour of the bar's fill. Any CSS colour. */\n  accent?: string;\n  className?: string;\n}\n\nexport function ReadingProgress({\n  children,\n  kicker = 'Field notes',\n  title = 'We close on Wednesdays now, and here is what changed',\n  byline = 'The bakery on Kessel Street · 2 min read',\n  as: Heading = 'h2',\n  accent = '#ff90e8',\n  className = '',\n}: ReadingProgressProps) {\n  const frameRef = useRef<HTMLDivElement>(null);\n  const [shown, setShown] = useState(false);\n\n  useEffect(() => {\n    const frame = frameRef.current;\n    if (!frame) return;\n\n    // scrollProgress writes its variable on the element it measures. Set on the\n    // article, every element inside would inherit the new value and be restyled\n    // with it on every scroll frame. `frame` is an empty layer over the same box\n    // with only the bar inside, so the bar is all that is restyled.\n    const progress = scrollProgress(frame, {\n      variable: '--reading-progress',\n      // Only the linear value is used: skip writing the eased one.\n      easedVariable: null,\n      // The end is a point every page can reach: however the page ends, the\n      // article's bottom can always scroll up to the bottom of the viewport.\n      trigger: { start: 'top top', end: 'bottom bottom' },\n    });\n\n    // scrollProgress updates only while the element it measures is on screen.\n    // That is invisible for most uses, but this bar is fixed and stays in view:\n    // a jump over the whole article in one step, or the browser restoring a\n    // scroll position past the article after load, would leave the bar showing a\n    // value that is no longer true. So the bar is shown only while the article\n    // is on screen, where the library keeps it exact on every frame.\n    //\n    // It also stays hidden for an article no taller than the viewport. There is\n    // nothing to scroll through, and the window runs backwards (its end comes\n    // before its start), so the bar would start full and empty as you scroll.\n    let onScreen = false;\n    const sync = () => setShown(onScreen && frame.getBoundingClientRect().height > window.innerHeight);\n\n    const io = new IntersectionObserver((entries) => {\n      // Entries arrive in order, so the last one is the current state.\n      for (const entry of entries) onScreen = entry.isIntersecting;\n      sync();\n    });\n    io.observe(frame);\n\n    // The article's height changes as images and fonts load; the viewport's\n    // changes on a resize, including a mobile toolbar showing or hiding. The\n    // library re-measures its own window on both; these only decide `shown`.\n    // ResizeObserver is missing in jsdom, where component tests often stub\n    // IntersectionObserver alone.\n    const ro = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(sync);\n    ro?.observe(frame);\n    window.addEventListener('resize', sync);\n\n    // StrictMode mounts effects twice in development: destroy() removes the\n    // variable, so the second run starts clean.\n    return () => {\n      progress.destroy();\n      io.disconnect();\n      ro?.disconnect();\n      window.removeEventListener('resize', sync);\n    };\n  }, []);\n\n  return (\n    <article className={`relative mx-auto w-full max-w-2xl ${className}`}>\n      {/* The layer scrollProgress measures and writes to: it covers the article\n          exactly and takes no clicks, so text under it can still be selected. */}\n      <div ref={frameRef} aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0\">\n        <div\n          className={`fixed inset-x-0 top-0 z-50 h-[3px] bg-neutral-900/10 print:hidden motion-safe:transition-opacity motion-safe:duration-300 dark:bg-white/15 ${\n            shown ? 'opacity-100' : 'opacity-0'\n          }`}\n        >\n          {/* Transform, not width, so a scroll frame never triggers layout. Until\n              the effect runs the variable is unset and the fallback keeps the\n              fill empty. It fills from the reading start: the right on an RTL page. */}\n          <div\n            className=\"h-full origin-left will-change-transform [transform:scaleX(var(--reading-progress,0))] rtl:origin-right\"\n            style={{ backgroundColor: accent }}\n          />\n        </div>\n      </div>\n\n      <header>\n        <p className=\"font-mono text-xs text-neutral-600 dark:text-neutral-400\">\n          {kicker}\n        </p>\n        <Heading className=\"mt-3 text-4xl font-medium tracking-tight text-neutral-950 text-balance sm:text-5xl dark:text-neutral-50\">\n          {title}\n        </Heading>\n        <p className=\"mt-4 text-sm text-neutral-600 dark:text-neutral-400\">{byline}</p>\n      </header>\n\n      <div className=\"mt-10 space-y-6 text-lg leading-relaxed text-neutral-700 dark:text-neutral-300\">\n        {children ?? <PlaceholderEssay heading={Heading === 'h1' ? 'h2' : 'h3'} />}\n      </div>\n    </article>\n  );\n}\n\n/** Placeholder copy. Replace it by passing your own article as children. */\nfunction PlaceholderEssay({ heading: Subheading }: { heading: 'h2' | 'h3' }) {\n  const subheading = 'pt-4 text-2xl font-semibold tracking-tight text-neutral-950 dark:text-neutral-50';\n\n  return (\n    <>\n      <p>\n        For eleven years the bakery on Kessel Street opened six days a week. The ovens were lit at four,\n        the first rye came out at half past five, and by Friday everyone behind the counter was running on\n        coffee and habit. Last March we tried something smaller: open four days, Thursday to Sunday, with\n        Wednesday kept for the work that never fitted between customers.\n      </p>\n\n      <Subheading className={subheading}>Why we stopped</Subheading>\n      <p>\n        It was not about rest at first. It was about waste. On Tuesdays and Wednesdays we baked for the\n        crowd we hoped for and sold to the crowd we had, and about one loaf in five went to the food bank or\n        the compost. Those days only looked like they paid their way because the weekend was paying for them.\n      </p>\n      <p>\n        Closing on the slow days let us bake later on the busy ones. The second bake moved to eleven, so the\n        sourdough that used to sit on the shelf from dawn came out in time for lunch. A regular asked whether\n        we had changed the flour. We had changed the clock.\n      </p>\n\n      <Subheading className={subheading}>What the numbers did</Subheading>\n      <p>\n        Six months in, we sell about as many loaves a week as before, in four days instead of six. Waste\n        fell from a fifth of each bake to roughly one loaf in twenty. Takings dipped in the first month,\n        while people learned the new days, and were back to normal by the third.\n      </p>\n      <p>\n        Wednesday changed the most. With the door locked, the team tests recipes, fixes what broke during\n        the week and trains the apprentice on the shaping bench without a queue at the counter. The proofing\n        cabinet that leaked all winter was mended on the second Wednesday. Two of the loaves on today&apos;s\n        board started as Wednesday experiments.\n      </p>\n\n      <Subheading className={subheading}>What we would tell another small shop</Subheading>\n      <p>\n        Count what you throw away before you count what you sell; the quiet days are often the expensive\n        ones. Tell customers early and in plain words, on the door, on the receipts and at the till. We lost\n        fewer regulars to the new hours than to the six weeks the road outside was dug up.\n      </p>\n      <p>\n        We are not going back. The ovens still light at four, only now four times a week, and the people who\n        light them have Monday and Tuesday to themselves.\n      </p>\n    </>\n  );\n}\n\nexport default ReadingProgress;\n",
      "type": "registry:component",
      "target": "components/scroll/reading-progress.tsx"
    }
  ],
  "categories": [
    "scroll",
    "progress"
  ]
}
