{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-sticky-scroll",
  "title": "Sticky scroll",
  "description": "Steps that scroll past a pinned picture, which changes in place to match the step being read.",
  "dependencies": [],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "components/core/sticky-scroll.tsx",
      "content": "'use client';\nimport {\n  type ComponentProps,\n  type ReactNode,\n  type RefObject,\n  useEffect,\n  useRef,\n  useState,\n} from 'react';\nimport { cn } from '@/lib/utils';\n\n// A one percent band across the middle of the scrolling area: the step that crosses it is active.\nconst MIDDLE_BAND = '-50% 0px -49% 0px';\n\nexport type StickyScrollStep = {\n  id: string;\n  title: ReactNode;\n  description: ReactNode;\n  media: ReactNode;\n};\n\nexport type StickyScrollProps = {\n  steps: StickyScrollStep[];\n  containerRef?: RefObject<HTMLElement | null>;\n  mediaSide?: 'start' | 'end';\n  className?: string;\n  frameClassName?: string;\n  stepClassName?: string;\n};\n\nfunction stateOf(isActive: boolean) {\n  if (isActive) {\n    return 'active';\n  }\n  return 'inactive';\n}\n\nfunction panelOrderOf(mediaSide: 'start' | 'end') {\n  if (mediaSide === 'start') {\n    return '@xl:order-first';\n  }\n  return '';\n}\n\nfunction useActiveStep(\n  listRef: RefObject<HTMLOListElement | null>,\n  containerRef: RefObject<HTMLElement | null> | undefined,\n  stepCount: number\n) {\n  const [activeIndex, setActiveIndex] = useState(0);\n\n  useEffect(() => {\n    const list = listRef.current;\n    if (!list) {\n      return;\n    }\n\n    const root = containerRef?.current ?? null;\n    const activateCrossingStep = (entries: IntersectionObserverEntry[]) => {\n      for (const entry of entries) {\n        if (!entry.isIntersecting) {\n          continue;\n        }\n        const indexAttribute = entry.target.getAttribute('data-step-index');\n        const index = Number(indexAttribute);\n        setActiveIndex(index);\n      }\n    };\n    const observer = new IntersectionObserver(activateCrossingStep, {\n      root,\n      rootMargin: MIDDLE_BAND,\n    });\n\n    for (const step of Array.from(list.children)) {\n      observer.observe(step);\n    }\n    return () => observer.disconnect();\n  }, [listRef, containerRef, stepCount]);\n\n  return activeIndex;\n}\n\nexport function StickyScroll({\n  steps,\n  containerRef,\n  mediaSide = 'end',\n  className,\n  frameClassName,\n  stepClassName,\n}: StickyScrollProps) {\n  const listRef = useRef<HTMLOListElement>(null);\n  const activeIndex = useActiveStep(listRef, containerRef, steps.length);\n  const panelOrder = panelOrderOf(mediaSide);\n\n  return (\n    <div className={cn('@container', className)}>\n      <div className='grid gap-x-12 @xl:grid-cols-2'>\n        <ol ref={listRef} className='flex flex-col'>\n          {steps.map((step, index) => {\n            const isActive = index === activeIndex;\n\n            return (\n              <li\n                key={step.id}\n                data-step-index={index}\n                data-state={stateOf(isActive)}\n                className={cn(\n                  'group/step flex min-h-[70svh] flex-col justify-center gap-4 py-10',\n                  stepClassName\n                )}\n              >\n                <div className='@xl:hidden'>{step.media}</div>\n                <div className='flex flex-col gap-2'>\n                  <h3 className='text-muted-foreground group-data-[state=active]/step:text-foreground text-lg font-semibold text-balance transition-colors duration-200'>\n                    {step.title}\n                  </h3>\n                  <div className='text-muted-foreground text-sm'>\n                    {step.description}\n                  </div>\n                </div>\n              </li>\n            );\n          })}\n        </ol>\n        <div\n          aria-hidden='true'\n          className={cn(\n            'sticky top-0 hidden h-svh items-center self-start @xl:flex',\n            panelOrder,\n            frameClassName\n          )}\n        >\n          <div className='grid w-full'>\n            {steps.map((step, index) => {\n              const isActive = index === activeIndex;\n\n              // The outgoing media stays opaque under the incoming one until its fade ends,\n              // so the panel never shows the page through a half-faded pair.\n              return (\n                <div\n                  key={step.id}\n                  data-state={stateOf(isActive)}\n                  className='col-start-1 row-start-1 opacity-0 transition-opacity delay-200 duration-0 data-[state=active]:z-10 data-[state=active]:opacity-100 data-[state=active]:delay-0 data-[state=active]:duration-200 data-[state=active]:ease-(--ease-out-strong)'\n                >\n                  {step.media}\n                </div>\n              );\n            })}\n          </div>\n        </div>\n      </div>\n    </div>\n  );\n}\n\nexport function StickySections({ className, ...props }: ComponentProps<'div'>) {\n  return <div className={cn('relative', className)} {...props} />;\n}\n\nexport function StickySection({\n  className,\n  ...props\n}: ComponentProps<'section'>) {\n  return (\n    <section\n      className={cn('sticky top-0 flex h-svh flex-col', className)}\n      {...props}\n    />\n  );\n}\n",
      "type": "registry:component",
      "target": "components/core/sticky-scroll.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}