{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scroll-story",
  "type": "registry:ui",
  "title": "Scroll Story",
  "description": "A chart that stays in view while the story beside it scrolls: each step of text the reader reaches changes the chart, one point at a time. The story is told in the steps; the chart shows it.",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "data",
    "pattern"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/cn.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/patterns/scroll-story.tsx",
      "target": "components/ui/patterns/scroll-story.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/scroll-story v1.1.1 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/scroll-story\n// What changed since: https://ds.interlace.tools/c/scroll-story#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — ScrollStory\n *\n * A chart that stays in view while the story beside it scrolls: each step of\n * text the reader reaches changes the chart, one point at a time. The story\n * is told in the steps; the chart shows it.\n *\n *   <ScrollStory graphic={(step) => <DotPlot series={SERIES[step]} rows={rows} />}>\n *     <ScrollStoryStep>In 2014, most districts were under 20%.</ScrollStoryStep>\n *     <ScrollStoryStep>Ten years on, every one of them rose.</ScrollStoryStep>\n *     <ScrollStoryStep>The biggest change came where it started lowest.</ScrollStoryStep>\n *   </ScrollStory>\n *\n * ## Steps, not scroll position\n *\n * The chart changes when a step crosses the middle of the screen, and only\n * then: it is never tied to how far the page has scrolled, so there is no\n * scroll-linked motion, nothing moves while the reader holds still, and\n * scrolling back up steps back. `graphic(step)` draws the chart for a step;\n * how it moves from one to the next is the chart's own business, so it\n * follows the chart's own reduced-motion rules.\n *\n * ## Content first\n *\n * Every step's text is in the server HTML, in reading order, and the chart\n * is drawn for the first step. Without scripts the reader gets the whole\n * story and the opening chart. Write each step so it says its point in\n * words: a screen reader reads the steps in order and does not watch the\n * chart change, so the text is the story for them.\n *\n * ## Layout by container\n *\n * When the story's own box is 48rem or wider, the steps and the chart sit\n * side by side and the chart stays put in its column (`side` picks which\n * column, start or end, and follows the page's direction). Narrower, the\n * chart stays at the top of the screen and the steps pass over it as cards.\n * Each step is a screen tall, so one step is read at a time, and its card\n * rests at the foot of the screen, so a tall chart above it stays in view\n * while the card is read instead of sitting under it. The first card sits\n * right under the chart, so the opening screen shows where to start. A page with\n * its own sticky header sets `--scroll-story-offset` to that header's\n * height, and the chart sits below it.\n *\n * ## MIN_VIEWPORT — 320\n *\n * ## Anatomy\n *\n *   <div data-slot=\"scroll-story\" data-step={n} data-side=\"start|end\">\n *     <div data-slot=\"scroll-story-graphic\">{graphic(step)}</div>      (sticky)\n *     <div data-slot=\"scroll-story-steps\">\n *       <div data-slot=\"scroll-story-step\" data-active?>               (a screen tall)\n *         <div data-slot=\"scroll-story-card\">{children}</div>\n *       </div> × steps\n *     </div>\n *   </div>\n *\n * | Rule | Concept                   | Where in this file                                     |\n * | ---- | ------------------------- | ------------------------------------------------------ |\n * | R4   | Extends native el         | `React.ComponentProps<'div'>`                          |\n * | R6   | data-slot per part        | scroll-story / -graphic / -steps / -step / -card       |\n * | R7   | cn + ...rest              | `cn(…, className)` + `{...props}`                      |\n * | R8   | Enums, not booleans       | `side`                                                 |\n * | R10  | Composition seam          | `graphic(step)` draws any chart; steps hold any content |\n * | R14  | Declares min viewport     | `data-min-viewport` + exported const                   |\n * | R18  | Tailwind only             | zero inline `style`                                    |\n * | R19  | Tokens only               | card, border, primary, background                      |\n * | R25  | Client component          | the step watcher (IntersectionObserver) runs in the browser |\n * | R26  | A11y                      | steps in reading order, active step not shown by colour alone (`data-active`, border weight) |\n */\n\nimport { cn } from '@/lib/utils';\n\nexport const MIN_VIEWPORT = 320 as const;\n\ntype StoryContext = { active: number };\n\nconst StoryContext = React.createContext<StoryContext>({ active: 0 });\nconst StepIndex = React.createContext(-1);\n\nexport interface ScrollStoryProps extends Omit<React.ComponentProps<'div'>, 'children'> {\n  /** Draws the chart for a step, counted from 0. */\n  graphic: (step: number) => React.ReactNode;\n  /** The steps, each a `ScrollStoryStep`, in reading order. */\n  children: React.ReactNode;\n  /** Which column the chart takes when there is room for two. @default 'end' */\n  side?: 'start' | 'end';\n  /** Called when a new step becomes the active one. */\n  onStepChange?: (step: number) => void;\n}\n\nexport const ScrollStory = React.forwardRef<HTMLDivElement, ScrollStoryProps>(function ScrollStory(\n  { graphic, children, side = 'end', onStepChange, className, ...props },\n  ref,\n) {\n  const [active, setActive] = React.useState(0);\n  const stepsRef = React.useRef<HTMLDivElement>(null);\n  const notify = React.useRef(onStepChange);\n  notify.current = onStepChange;\n\n  const steps = React.Children.toArray(children);\n  const count = steps.length;\n\n  // A step is active while it covers the middle line of the screen. The steps\n  // are a screen tall and stacked, so one of them always does once the story\n  // is under way; before the first one arrives, the first step stays active.\n  React.useEffect(() => {\n    const root = stepsRef.current;\n    if (!root || typeof IntersectionObserver === 'undefined') return;\n    const observer = new IntersectionObserver(\n      (entries) => {\n        for (const entry of entries) {\n          if (!entry.isIntersecting) continue;\n          const at = Number((entry.target as HTMLElement).dataset.index);\n          if (Number.isInteger(at)) setActive(at);\n        }\n      },\n      { rootMargin: '-50% 0px -50% 0px', threshold: 0 },\n    );\n    root.querySelectorAll<HTMLElement>('[data-slot=\"scroll-story-step\"]').forEach((el) => observer.observe(el));\n    return () => observer.disconnect();\n  }, [count]);\n\n  const step = Math.min(active, Math.max(0, count - 1));\n  const context = React.useMemo(() => ({ active: step }), [step]);\n  const first = React.useRef(true);\n  React.useEffect(() => {\n    if (first.current) {\n      first.current = false;\n      return;\n    }\n    notify.current?.(step);\n  }, [step]);\n\n  return (\n    <div\n      ref={ref}\n      data-slot=\"scroll-story\"\n      data-step={step}\n      data-side={side}\n      data-min-viewport={String(MIN_VIEWPORT)}\n      className={cn('@container/story isolate', className)}\n      {...props}\n    >\n      <div\n        className={cn(\n          'grid grid-cols-1 @3xl/story:gap-10',\n          side === 'end'\n            ? '@3xl/story:grid-cols-[minmax(0,2fr)_minmax(0,3fr)]'\n            : '@3xl/story:grid-cols-[minmax(0,3fr)_minmax(0,2fr)]',\n        )}\n      >\n        <div\n          data-slot=\"scroll-story-graphic\"\n          className={cn(\n            'bg-background sticky top-[var(--scroll-story-offset,0px)] z-base py-4 @3xl/story:row-start-1 @3xl/story:flex @3xl/story:min-h-[calc(100svh-var(--scroll-story-offset,0px))] @3xl/story:flex-col @3xl/story:justify-center @3xl/story:self-start',\n            side === 'end' ? '@3xl/story:col-start-2' : '@3xl/story:col-start-1',\n          )}\n        >\n          {graphic(step)}\n        </div>\n\n        <div\n          ref={stepsRef}\n          data-slot=\"scroll-story-steps\"\n          className={cn(\n            'relative z-raised flex flex-col @3xl/story:row-start-1',\n            side === 'end' ? '@3xl/story:col-start-1' : '@3xl/story:col-start-2',\n          )}\n        >\n          <StoryContext.Provider value={context}>\n            {steps.map((child, i) => (\n              <StepIndex.Provider key={React.isValidElement(child) && child.key != null ? child.key : i} value={i}>\n                {child}\n              </StepIndex.Provider>\n            ))}\n          </StoryContext.Provider>\n        </div>\n      </div>\n    </div>\n  );\n});\nScrollStory.displayName = 'ScrollStory';\n\nexport interface ScrollStoryStepProps extends React.ComponentProps<'div'> {}\n\n/** One step of a `ScrollStory`: the text that goes with one state of the chart. */\nexport const ScrollStoryStep = React.forwardRef<HTMLDivElement, ScrollStoryStepProps>(function ScrollStoryStep(\n  { className, children, ...props },\n  ref,\n) {\n  const index = React.useContext(StepIndex);\n  const { active } = React.useContext(StoryContext);\n  const isActive = index === active;\n  return (\n    <div\n      ref={ref}\n      data-slot=\"scroll-story-step\"\n      data-index={index}\n      data-active={isActive ? '' : undefined}\n      // Narrow: the card rests at the foot of the screen, clear of the chart pinned at the top;\n      // the first card sits right under the chart, so the opening screen shows where to read.\n      // Wide: the chart has its own column, so every card sits in the middle.\n      className=\"flex min-h-[calc(100svh-var(--scroll-story-offset,0px))] flex-col justify-end pt-10 pb-6 first:justify-start first:pt-4 @3xl/story:justify-center @3xl/story:py-10 @3xl/story:first:justify-center @3xl/story:first:py-10\"\n    >\n      <div\n        data-slot=\"scroll-story-card\"\n        className={cn(\n          'bg-card text-card-foreground rounded-lg border p-6 shadow-sm',\n          'border-s-4 motion-safe:transition-colors motion-safe:duration-(--duration-base) motion-safe:ease-enter',\n          isActive ? 'border-s-primary' : 'border-s-border',\n          className,\n        )}\n        {...props}\n      >\n        {children}\n      </div>\n    </div>\n  );\n});\nScrollStoryStep.displayName = 'ScrollStoryStep';\n"
    }
  ],
  "meta": {
    "tier": "pattern",
    "client": true,
    "minViewport": 320,
    "loading": false,
    "version": "1.1.1",
    "since": "2.0.0"
  },
  "docs": "## @interlace/scroll-story\n\nInstalled to `components/ui/patterns/scroll-story.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/patterns/scroll-story';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/scroll-story\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
