{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-magic-card",
  "title": "Magic card",
  "description": "A card whose edge lights up on the side the pointer comes from, before the pointer reaches it.",
  "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/magic-card.tsx",
      "content": "'use client';\n\nimport {\n  animate,\n  motion,\n  type MotionValue,\n  useMotionTemplate,\n  useMotionValue,\n  useSpring,\n} from 'motion/react';\nimport { useEffect, useRef } from 'react';\n\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\n\nexport type MagicCardTint = 'primary' | 'neutral';\n\nconst TINT_COLORS = {\n  primary: {\n    rim: 'color-mix(in oklab, var(--primary) 75%, transparent)',\n    wash: 'color-mix(in oklab, var(--primary) 6%, transparent)',\n  },\n  neutral: {\n    rim: 'color-mix(in oklab, var(--foreground) 40%, transparent)',\n    wash: 'color-mix(in oklab, var(--foreground) 3%, transparent)',\n  },\n} satisfies Record<MagicCardTint, { rim: string; wash: string }>;\n\nconst ANGLE_SPRING = { stiffness: 320, damping: 40 };\n\nconst STRENGTH_SPRING = { stiffness: 260, damping: 36 };\n\nconst WASH_RADIUS = 280;\n\nconst WASH_IN = { duration: 0.3, ease: [0.23, 1, 0.32, 1] as const };\n\nconst WASH_OUT = { duration: 0.15, ease: [0.23, 1, 0.32, 1] as const };\n\nconst RIM_SHAPE: React.CSSProperties = {\n  inset: 'calc(var(--hairline, 1px) * -1)',\n  padding: 1,\n  mask: 'linear-gradient(black, black) content-box exclude, linear-gradient(black, black)',\n  WebkitMask:\n    'linear-gradient(black, black) content-box xor, linear-gradient(black, black)',\n};\n\nconst CARD = [\n  'relative isolate rounded-xl bg-card text-card-foreground',\n  'hairline-ring ring-border shadow-[0_1px_2px_rgb(0_0_0/0.04)]',\n].join(' ');\n\nfunction clamp(value: number, min: number, max: number) {\n  const atLeastMin = Math.max(value, min);\n  return Math.min(atLeastMin, max);\n}\n\nfunction distanceOutside(box: DOMRect, x: number, y: number) {\n  const beforeLeft = box.left - x;\n  const pastRight = x - box.right;\n  const aboveTop = box.top - y;\n  const belowBottom = y - box.bottom;\n  const horizontal = Math.max(beforeLeft, 0, pastRight);\n  const vertical = Math.max(aboveTop, 0, belowBottom);\n  return Math.hypot(horizontal, vertical);\n}\n\n/** Conic gradients start at the top and turn clockwise, so the pointer angle is measured the same way. */\nfunction pointerAngleOf(box: DOMRect, x: number, y: number) {\n  const centreX = box.left + box.width / 2;\n  const centreY = box.top + box.height / 2;\n  const deltaX = x - centreX;\n  const deltaY = y - centreY;\n  const radians = Math.atan2(deltaY, deltaX);\n  const degrees = (radians * 180) / Math.PI;\n  return degrees + 90;\n}\n\nfunction continuousAngleOf(target: number, previous: number) {\n  const difference = target - previous;\n  const turns = Math.round(difference / 360);\n  const wrapped = difference - turns * 360;\n  return previous + wrapped;\n}\n\nfunction followedOf(\n  prefersReducedMotion: boolean | null,\n  raw: MotionValue<number>,\n  smooth: MotionValue<number>\n) {\n  if (prefersReducedMotion) {\n    return raw;\n  }\n  return smooth;\n}\n\nfunction washTransitionOf(isInside: boolean) {\n  if (isInside) {\n    return WASH_IN;\n  }\n  return WASH_OUT;\n}\n\nfunction washOpacityOf(isInside: boolean) {\n  if (isInside) {\n    return 1;\n  }\n  return 0;\n}\n\nexport type MagicCardProps = React.ComponentProps<'div'> & {\n  tint?: MagicCardTint;\n  proximity?: number;\n  spread?: number;\n};\n\nexport function MagicCard({\n  tint = 'primary',\n  proximity = 120,\n  spread = 70,\n  className,\n  children,\n  ...props\n}: MagicCardProps) {\n  const cardRef = useRef<HTMLDivElement>(null);\n  const prefersReducedMotion = usePrefersReducedMotion();\n  const rawAngle = useMotionValue(0);\n  const rawStrength = useMotionValue(0);\n  const smoothAngle = useSpring(rawAngle, ANGLE_SPRING);\n  const smoothStrength = useSpring(rawStrength, STRENGTH_SPRING);\n  const localX = useMotionValue(0);\n  const localY = useMotionValue(0);\n  const washOpacity = useMotionValue(0);\n  const colors = TINT_COLORS[tint];\n  const angle = followedOf(prefersReducedMotion, rawAngle, smoothAngle);\n  const strength = followedOf(\n    prefersReducedMotion,\n    rawStrength,\n    smoothStrength\n  );\n  const spreadEnd = spread * 2;\n  const rimLight = useMotionTemplate`conic-gradient(from ${angle}deg at 50% 50%, transparent 0deg, ${colors.rim} ${spread}deg, transparent ${spreadEnd}deg)`;\n  const washLight = useMotionTemplate`radial-gradient(${WASH_RADIUS}px circle at ${localX}px ${localY}px, ${colors.wash}, transparent)`;\n\n  useEffect(() => {\n    const card = cardRef.current;\n    if (!card) {\n      return;\n    }\n    let frame = 0;\n    let isNear = false;\n    let isInside = false;\n    let pointerX = 0;\n    let pointerY = 0;\n\n    const setInside = (nextInside: boolean) => {\n      if (nextInside === isInside) {\n        return;\n      }\n      isInside = nextInside;\n      const opacity = washOpacityOf(nextInside);\n      const transition = washTransitionOf(nextInside);\n      animate(washOpacity, opacity, transition);\n    };\n\n    const update = () => {\n      frame = 0;\n      const box = card.getBoundingClientRect();\n      const distance = distanceOutside(box, pointerX, pointerY);\n      const nearness = 1 - distance / proximity;\n      const pointerAngle = pointerAngleOf(box, pointerX, pointerY);\n      const startAngle = pointerAngle - spread;\n      const previousAngle = rawAngle.get();\n      const nextAngle = continuousAngleOf(startAngle, previousAngle);\n      const wasDark = rawStrength.get() === 0;\n      rawAngle.set(nextAngle);\n      if (wasDark) {\n        smoothAngle.jump(nextAngle);\n      }\n      const nextStrength = clamp(nearness, 0, 1);\n      const cardX = pointerX - box.left;\n      const cardY = pointerY - box.top;\n      const isPointerInside = distance === 0;\n      rawStrength.set(nextStrength);\n      localX.set(cardX);\n      localY.set(cardY);\n      setInside(isPointerInside);\n    };\n\n    const track = (event: PointerEvent) => {\n      if (event.pointerType !== 'mouse') {\n        return;\n      }\n      pointerX = event.clientX;\n      pointerY = event.clientY;\n      if (frame) {\n        return;\n      }\n      frame = requestAnimationFrame(update);\n    };\n\n    const fade = () => {\n      rawStrength.set(0);\n      setInside(false);\n    };\n\n    const startTracking = () => {\n      window.addEventListener('pointermove', track, { passive: true });\n      document.documentElement.addEventListener('pointerleave', fade);\n    };\n\n    const stopTracking = () => {\n      window.removeEventListener('pointermove', track);\n      document.documentElement.removeEventListener('pointerleave', fade);\n      cancelAnimationFrame(frame);\n      frame = 0;\n    };\n\n    const nearObserver = new IntersectionObserver(\n      ([entry]) => {\n        const wasNear = isNear;\n        isNear = entry.isIntersecting;\n        if (isNear === wasNear) {\n          return;\n        }\n        if (isNear) {\n          startTracking();\n          return;\n        }\n        stopTracking();\n        fade();\n      },\n      { rootMargin: `${proximity}px` }\n    );\n    nearObserver.observe(card);\n\n    return () => {\n      nearObserver.disconnect();\n      stopTracking();\n    };\n  }, [\n    proximity,\n    spread,\n    rawAngle,\n    rawStrength,\n    smoothAngle,\n    localX,\n    localY,\n    washOpacity,\n  ]);\n\n  return (\n    <div ref={cardRef} className={cn(CARD, className)} {...props}>\n      <motion.span\n        aria-hidden='true'\n        className='pointer-events-none absolute inset-0 rounded-[inherit]'\n        style={{ background: washLight, opacity: washOpacity }}\n      />\n      <motion.span\n        aria-hidden='true'\n        className='pointer-events-none absolute rounded-[inherit]'\n        style={{ ...RIM_SHAPE, background: rimLight, opacity: strength }}\n      />\n      <div className='relative'>{children}</div>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/core/magic-card.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}