{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "magnetic-cards",
  "title": "Magnetic Cards",
  "description": "A fanned stack of cards that fling away from the cursor on release, with GSAP InertiaPlugin easing each one back to rest.",
  "dependencies": ["gsap", "@gsap/react"],
  "registryDependencies": ["@soralabs/hooks-use-prefers-reduced-motion"],
  "files": [
    {
      "path": "registry/primitives/effects/magnetic-cards/index.tsx",
      "content": "\"use client\";\n\nimport { useGSAP } from \"@gsap/react\";\nimport { cn } from \"@/lib/utils\";\nimport { gsap } from \"gsap\";\nimport { InertiaPlugin } from \"gsap/InertiaPlugin\";\nimport Image from \"next/image\";\nimport { useMemo, useRef } from \"react\";\nimport type { ComponentPropsWithoutRef } from \"react\";\n\nimport { usePrefersReducedMotion } from \"@/hooks/use-prefers-reduced-motion\";\n\ngsap.registerPlugin(InertiaPlugin);\n\nexport interface MagneticCardsItem {\n  alt?: string;\n  src: string;\n}\n\nexport interface MagneticCardsLayout {\n  rotation: number;\n  x: number;\n  y: number;\n}\n\nexport interface MagneticCardsProps extends Omit<\n  ComponentPropsWithoutRef<\"div\">,\n  \"children\"\n> {\n  /** Extra classes applied to each card wrapper. */\n  cardClassName?: string;\n  /** Cards to render, in left-to-right stacking order. */\n  items: MagneticCardsItem[];\n  /** Rest position/rotation per card. Defaults to an auto-generated symmetric fan matching `items.length`. */\n  layout?: MagneticCardsLayout[];\n  /** How much a card spins on release, scaled from horizontal cursor speed while hovering it. @default 1.5 */\n  rotationVelocityMultiplier?: number;\n  /** How far a card flies on release, scaled from cursor speed while hovering it. @default 20 */\n  velocityMultiplier?: number;\n}\n\nconst REMOTE_IMAGE_SRC = /^https?:\\/\\//;\n\nfunction isRemoteImageSrc(src: string): boolean {\n  return REMOTE_IMAGE_SRC.test(src);\n}\n\nconst DEFAULTS = {\n  rotationVelocityMultiplier: 1.5,\n  velocityMultiplier: 20,\n} as const;\n\nconst LAYOUT_SPACING = 130;\nconst LAYOUT_ROTATION_STEP = 5;\n\nfunction createDefaultLayout(count: number): MagneticCardsLayout[] {\n  const mid = (count - 1) / 2;\n  return Array.from({ length: count }, (_, index) => {\n    const offset = index - mid;\n    const sign = index % 2 === 0 ? 1 : -1;\n    return {\n      rotation: offset * LAYOUT_ROTATION_STEP + sign * 2.5,\n      x: offset * LAYOUT_SPACING,\n      y: sign * 10,\n    };\n  });\n}\n\nfunction staticCardStyle(rest: MagneticCardsLayout, index: number) {\n  return {\n    transform: `translate(-50%, -50%) translate(${rest.x}px, ${rest.y}px) rotate(${rest.rotation}deg)`,\n    zIndex: index,\n  };\n}\n\n/** Tracks cursor speed while over a single card and flings it back to rest with GSAP's InertiaPlugin on release — same per-card model as Truus.co's MotionCards. */\nfunction attachCardInertia(\n  el: HTMLDivElement,\n  rest: MagneticCardsLayout,\n  velocityMultiplier: number,\n  rotationVelocityMultiplier: number\n): () => void {\n  let lastX = 0;\n  let lastY = 0;\n  let speedX = 0;\n  let speedY = 0;\n\n  const onMove = (event: MouseEvent) => {\n    speedX = event.clientX - lastX;\n    speedY = event.clientY - lastY;\n    lastX = event.clientX;\n    lastY = event.clientY;\n  };\n\n  const onEnter = (event: MouseEvent) => {\n    speedX = 0;\n    speedY = 0;\n    lastX = event.clientX;\n    lastY = event.clientY;\n  };\n\n  const onLeave = () => {\n    gsap.to(el, {\n      inertia: {\n        rotation: {\n          end: rest.rotation,\n          velocity: speedX * rotationVelocityMultiplier,\n        },\n        x: { end: rest.x, velocity: speedX * velocityMultiplier },\n        y: { end: rest.y, velocity: speedY * velocityMultiplier },\n      },\n    });\n  };\n\n  el.addEventListener(\"mousemove\", onMove);\n  el.addEventListener(\"mouseenter\", onEnter);\n  el.addEventListener(\"mouseleave\", onLeave);\n\n  return () => {\n    el.removeEventListener(\"mousemove\", onMove);\n    el.removeEventListener(\"mouseenter\", onEnter);\n    el.removeEventListener(\"mouseleave\", onLeave);\n  };\n}\n\nfunction MagneticCards({\n  cardClassName,\n  className,\n  items,\n  layout,\n  rotationVelocityMultiplier = DEFAULTS.rotationVelocityMultiplier,\n  velocityMultiplier = DEFAULTS.velocityMultiplier,\n  ...props\n}: MagneticCardsProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const containerRef = useRef<HTMLDivElement>(null);\n  const cardRefs = useRef<(HTMLDivElement | null)[]>([]);\n  const prefersReducedMotion = usePrefersReducedMotion();\n\n  const resolvedLayout = useMemo(\n    () => layout ?? createDefaultLayout(items.length),\n    [layout, items.length]\n  );\n\n  useGSAP(\n    () => {\n      const root = rootRef.current;\n      if (!root || prefersReducedMotion) {\n        return;\n      }\n\n      const cleanups = resolvedLayout.flatMap((rest, index) => {\n        const el = cardRefs.current[index];\n        if (!el) {\n          return [];\n        }\n\n        gsap.set(el, {\n          rotation: rest.rotation,\n          x: rest.x,\n          xPercent: -50,\n          y: rest.y,\n          yPercent: -50,\n          zIndex: index,\n        });\n\n        return [\n          attachCardInertia(\n            el,\n            rest,\n            velocityMultiplier,\n            rotationVelocityMultiplier\n          ),\n        ];\n      });\n\n      return () => {\n        for (const cleanup of cleanups) {\n          cleanup();\n        }\n      };\n    },\n    {\n      dependencies: [\n        items,\n        resolvedLayout,\n        prefersReducedMotion,\n        velocityMultiplier,\n        rotationVelocityMultiplier,\n      ],\n      scope: rootRef,\n    }\n  );\n\n  return (\n    <div className={cn(\"relative isolate\", className)} ref={rootRef} {...props}>\n      <div\n        className=\"relative flex h-full w-full items-center justify-center\"\n        ref={containerRef}\n      >\n        {items.map((item, index) => {\n          const rest = resolvedLayout[index] ?? { rotation: 0, x: 0, y: 0 };\n          return (\n            <div\n              className={cn(\n                \"absolute top-1/2 left-1/2 aspect-[3/4] w-40 overflow-hidden rounded-xl shadow-xl\",\n                !prefersReducedMotion && \"will-change-transform\",\n                cardClassName\n              )}\n              key={item.src}\n              ref={(el) => {\n                cardRefs.current[index] = el;\n              }}\n              style={\n                prefersReducedMotion ? staticCardStyle(rest, index) : undefined\n              }\n            >\n              <Image\n                alt={item.alt ?? \"\"}\n                className=\"pointer-events-none object-cover\"\n                draggable={false}\n                fill\n                sizes=\"160px\"\n                src={item.src}\n                unoptimized={isRemoteImageSrc(item.src)}\n              />\n            </div>\n          );\n        })}\n      </div>\n    </div>\n  );\n}\n\nexport { MagneticCards };\n",
      "type": "registry:ui",
      "target": "components/sora-ui/effects/magnetic-cards.tsx"
    }
  ],
  "meta": {
    "keywords": ["magnetic", "cards", "cursor", "inertia", "gsap"],
    "inspiration": {
      "type": "reimplemented",
      "label": "Truus.co",
      "url": "https://www.truus.co/",
      "stack": "GSAP InertiaPlugin and React"
    }
  },
  "type": "registry:ui"
}
