{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-stacking-cards",
  "title": "Stacking cards",
  "description": "Cards that pin and pile up as the page scrolls, each one behind shrinking and fading back a little.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "components/core/stacking-cards.tsx",
      "content": "'use client';\nimport {\n  motion,\n  type MotionValue,\n  useScroll,\n  useTransform,\n} from 'motion/react';\nimport {\n  Children,\n  createContext,\n  type CSSProperties,\n  type ReactNode,\n  type RefObject,\n  useContext,\n  useRef,\n} from 'react';\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\n\nconst DEEPEST_SCALE = 0.9;\nconst MAX_SCALE_STEP = 0.04;\nconst DIM_STEP = 0.12;\nconst MAX_DIM = 0.48;\n\ntype StackingCardsContextValue = {\n  progress: MotionValue<number>;\n  count: number;\n  peek: number;\n  frameClassName?: string;\n};\n\nconst StackingCardsContext = createContext<StackingCardsContextValue | null>(\n  null\n);\nconst StackingCardIndexContext = createContext(0);\n\nfunction useStackingCards() {\n  const context = useContext(StackingCardsContext);\n  if (!context) {\n    throw new Error('StackingCard must be used inside StackingCards');\n  }\n  return context;\n}\n\nfunction scaleStepOf(count: number) {\n  if (count < 2) {\n    return 0;\n  }\n  const scaleRange = 1 - DEEPEST_SCALE;\n  const gaps = count - 1;\n  const evenStep = scaleRange / gaps;\n  return Math.min(MAX_SCALE_STEP, evenStep);\n}\n\nfunction rangeStartOf(index: number, lastIndex: number) {\n  if (lastIndex === 0) {\n    return 0;\n  }\n  return index / lastIndex;\n}\n\nfunction scaleOf(\n  prefersReducedMotion: boolean | null,\n  scale: MotionValue<number>\n) {\n  if (prefersReducedMotion) {\n    return 1;\n  }\n  return scale;\n}\n\nfunction dimOf(prefersReducedMotion: boolean | null, dim: MotionValue<number>) {\n  if (prefersReducedMotion) {\n    return 0;\n  }\n  return dim;\n}\n\nexport type StackingCardsProps = {\n  children: ReactNode;\n  className?: string;\n  frameClassName?: string;\n  containerRef?: RefObject<HTMLElement | null>;\n  peek?: number;\n};\n\nexport function StackingCards({\n  children,\n  className,\n  frameClassName,\n  containerRef,\n  peek = 16,\n}: StackingCardsProps) {\n  const stackRef = useRef<HTMLDivElement>(null);\n  const cards = Children.toArray(children);\n  const count = cards.length;\n\n  const { scrollYProgress } = useScroll({\n    target: stackRef,\n    container: containerRef,\n    offset: ['start start', 'end end'],\n  });\n\n  const value = { progress: scrollYProgress, count, peek, frameClassName };\n\n  return (\n    <StackingCardsContext value={value}>\n      <div ref={stackRef} className={cn('relative', className)}>\n        {cards.map((card, index) => (\n          <StackingCardIndexContext key={index} value={index}>\n            {card}\n          </StackingCardIndexContext>\n        ))}\n      </div>\n    </StackingCardsContext>\n  );\n}\n\nexport type StackingCardProps = {\n  children: ReactNode;\n  className?: string;\n};\n\nexport function StackingCard({ children, className }: StackingCardProps) {\n  const { progress, count, peek, frameClassName } = useStackingCards();\n  const index = useContext(StackingCardIndexContext);\n  const prefersReducedMotion = usePrefersReducedMotion();\n\n  const lastIndex = count - 1;\n  const depth = lastIndex - index;\n  const scaleStep = scaleStepOf(count);\n  const targetScale = 1 - depth * scaleStep;\n  const depthDim = depth * DIM_STEP;\n  const targetDim = Math.min(MAX_DIM, depthDim);\n  const rangeStart = rangeStartOf(index, lastIndex);\n  const stackOffset = index * peek;\n\n  const scale = useTransform(progress, [rangeStart, 1], [1, targetScale]);\n  const dim = useTransform(progress, [rangeStart, 1], [0, targetDim]);\n  const cardScale = scaleOf(prefersReducedMotion, scale);\n  const cardDim = dimOf(prefersReducedMotion, dim);\n  const offsetStyle = {\n    '--stack-offset': `${stackOffset}px`,\n  } as CSSProperties;\n\n  return (\n    <div\n      style={offsetStyle}\n      className={cn(\n        'sticky top-0 flex h-svh items-center justify-center',\n        'motion-reduce:static motion-reduce:h-auto motion-reduce:py-2',\n        frameClassName\n      )}\n    >\n      <motion.div\n        style={{ scale: cardScale }}\n        className={cn(\n          'relative top-(--stack-offset) w-full origin-top overflow-clip motion-reduce:top-0',\n          className\n        )}\n      >\n        {children}\n        <motion.div\n          aria-hidden='true'\n          style={{ opacity: cardDim }}\n          className='bg-background pointer-events-none absolute inset-0'\n        />\n      </motion.div>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/core/stacking-cards.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}