{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "magnetic",
  "title": "Magnetic",
  "description": "Pulls an isolated child toward a fine pointer and settles with a spring.",
  "dependencies": [
    "motion@^13.1.0"
  ],
  "files": [
    {
      "path": "packages/react/src/magnetic.tsx",
      "content": "'use client'\n\nimport './magnetic.css'\nimport { type MagneticOptions, magnetic } from './motion/magnetic.js'\nimport type { HTMLAttributes, ReactNode } from 'react'\nimport { forwardRef, useRef } from 'react'\nimport { composeRefs } from './compose-refs.js'\nimport { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect.js'\n\nexport interface MagneticProps extends MagneticOptions, HTMLAttributes<HTMLSpanElement> {\n  children?: ReactNode\n  targetClassName?: string\n}\n\nexport const Magnetic = forwardRef<HTMLSpanElement, MagneticProps>(function Magnetic(\n  { strength, maxDistance, children, targetClassName, ...props },\n  forwardedRef,\n) {\n  const rootRef = useRef<HTMLSpanElement>(null)\n  const targetRef = useRef<HTMLSpanElement>(null)\n\n  useIsomorphicLayoutEffect(() => {\n    const root = rootRef.current\n    const target = targetRef.current\n    if (!root || !target) return\n    return magnetic(root, target, { strength, maxDistance })\n  }, [maxDistance, strength])\n\n  return (\n    <span {...props} ref={composeRefs(rootRef, forwardedRef)} data-kida-magnetic=\"\">\n      <span ref={targetRef} className={targetClassName} data-kida-magnetic-target=\"\">\n        {children}\n      </span>\n    </span>\n  )\n})\n",
      "type": "registry:ui",
      "target": "@ui/kida/magnetic.tsx"
    },
    {
      "path": "packages/react/src/compose-refs.ts",
      "content": "import type { Ref } from 'react'\n\n/** Feed one node to several refs — ours for measuring, Zag's for animation detection. */\nexport function composeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n  return (node: T | null) => {\n    for (const ref of refs) {\n      if (typeof ref === 'function') ref(node)\n      else if (ref) (ref as { current: T | null }).current = node\n    }\n  }\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/compose-refs.ts"
    },
    {
      "path": "packages/react/src/use-isomorphic-layout-effect.ts",
      "content": "'use client'\n\nimport { useEffect, useLayoutEffect } from 'react'\n\n/**\n * `useLayoutEffect` in the browser, `useEffect` on the server.\n *\n * Motion has to be applied before paint or the user sees an unstyled frame, but React\n * warns when `useLayoutEffect` runs during SSR — where it does nothing anyway.\n */\nexport const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect\n",
      "type": "registry:hook",
      "target": "@ui/kida/use-isomorphic-layout-effect.ts"
    },
    {
      "path": "packages/styles/components/magnetic.css",
      "content": "[data-kida-magnetic] {\n  --kida-magnetic-activation-distance: 2rem;\n\n  display: inline-flex;\n  position: relative;\n  isolation: isolate;\n}\n\n/* Keep hover detection around the stationary root. Without this stable area, translating\n   the target can move it out from under the pointer and immediately cancel the effect. */\n[data-kida-magnetic]::before {\n  position: absolute;\n  z-index: -1;\n  inset: calc(-1 * var(--kida-magnetic-activation-distance));\n  content: \"\";\n}\n\n[data-kida-magnetic-target] {\n  display: inline-flex;\n}\n\n@media (prefers-reduced-motion: reduce) {\n  [data-kida-magnetic-target] {\n    /* biome-ignore lint/complexity/noImportantStyles: stops an in-flight inline transform */\n    transform: none !important;\n  }\n}\n",
      "type": "registry:ui",
      "target": "@ui/kida/magnetic.css"
    },
    {
      "path": "packages/motion/src/magnetic.ts",
      "content": "import { motionValue, springValue, styleEffect } from 'motion'\nimport { prefersReducedMotion } from './reduced-motion.js'\n\nexport interface MagneticOptions {\n  /** Portion of pointer distance followed by the target. @default 0.22 */\n  strength?: number\n  /** Maximum travel in CSS pixels. @default 18 */\n  maxDistance?: number\n}\n\nconst DEFAULTS = { strength: 0.22, maxDistance: 18 } as const\n\nexport function magnetic(\n  root: HTMLElement,\n  target: HTMLElement,\n  options: MagneticOptions = {},\n): () => void {\n  if (prefersReducedMotion()) return () => {}\n\n  const { strength = DEFAULTS.strength, maxDistance = DEFAULTS.maxDistance } = options\n  let bounds = root.getBoundingClientRect()\n  let frame = 0\n  let x = 0\n  let y = 0\n  let inside = false\n\n  // Retarget one persistent spring instead of canceling and recreating a DOM animation\n  // for every pointer frame. Recreating it continually throws away most of the spring's\n  // velocity, which makes a moving pointer look as though it barely affects the target.\n  const sourceX = motionValue(0)\n  const sourceY = motionValue(0)\n  const spring = { stiffness: 360, damping: 24, mass: 0.65 }\n  const springX = springValue(sourceX, spring)\n  const springY = springValue(sourceY, spring)\n  const stopStyleEffect = styleEffect(target, { x: springX, y: springY })\n\n  let xSettled = true\n  let ySettled = true\n  const settle = () => {\n    if (inside || !xSettled || !ySettled || sourceX.get() !== 0 || sourceY.get() !== 0) return\n    target.style.transform = 'none'\n    target.style.willChange = 'auto'\n  }\n  const stopXStart = springX.on('animationStart', () => {\n    xSettled = false\n  })\n  const stopYStart = springY.on('animationStart', () => {\n    ySettled = false\n  })\n  const stopXComplete = springX.on('animationComplete', () => {\n    xSettled = true\n    settle()\n  })\n  const stopYComplete = springY.on('animationComplete', () => {\n    ySettled = true\n    settle()\n  })\n\n  const move = () => {\n    frame = 0\n    sourceX.set(x)\n    sourceY.set(y)\n  }\n\n  const onEnter = () => {\n    inside = true\n    bounds = root.getBoundingClientRect()\n    target.style.willChange = 'transform'\n  }\n  const onMove = (event: PointerEvent) => {\n    if (event.pointerType === 'touch') return\n    const centerX = bounds.left + bounds.width / 2\n    const centerY = bounds.top + bounds.height / 2\n    x = Math.max(-maxDistance, Math.min(maxDistance, (event.clientX - centerX) * strength))\n    y = Math.max(-maxDistance, Math.min(maxDistance, (event.clientY - centerY) * strength))\n    if (!frame) frame = requestAnimationFrame(move)\n  }\n  const onLeave = () => {\n    inside = false\n    if (frame) cancelAnimationFrame(frame)\n    frame = 0\n    x = 0\n    y = 0\n    sourceX.set(0)\n    sourceY.set(0)\n    settle()\n  }\n\n  root.addEventListener('pointerenter', onEnter)\n  root.addEventListener('pointermove', onMove)\n  root.addEventListener('pointerleave', onLeave)\n  const observer = new ResizeObserver(() => {\n    bounds = root.getBoundingClientRect()\n  })\n  observer.observe(root)\n\n  return () => {\n    root.removeEventListener('pointerenter', onEnter)\n    root.removeEventListener('pointermove', onMove)\n    root.removeEventListener('pointerleave', onLeave)\n    observer.disconnect()\n    if (frame) cancelAnimationFrame(frame)\n    stopXStart()\n    stopYStart()\n    stopXComplete()\n    stopYComplete()\n    stopStyleEffect()\n    springX.destroy()\n    springY.destroy()\n    sourceX.destroy()\n    sourceY.destroy()\n    target.style.transform = 'none'\n    target.style.willChange = 'auto'\n  }\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/motion/magnetic.ts"
    },
    {
      "path": "packages/motion/src/reduced-motion.ts",
      "content": "const QUERY = '(prefers-reduced-motion: reduce)'\n\n/**\n * Whether the user has asked for reduced motion.\n *\n * SSR-safe: returns `false` when there is no `window`, matching the CSS default\n * (the `prefers-reduced-motion` media query in `@kida-ui/styles` is the real guard;\n * this is for the JS-driven paths that CSS cannot express).\n */\nexport function prefersReducedMotion(): boolean {\n  if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return false\n  return window.matchMedia(QUERY).matches\n}\n\n/**\n * Subscribe to changes of the reduced-motion preference.\n * Returns an unsubscribe function. No-op (and immediately unsubscribable) on the server.\n */\nexport function watchReducedMotion(onChange: (reduced: boolean) => void): () => void {\n  if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return () => {}\n\n  const list = window.matchMedia(QUERY)\n  const handler = (event: MediaQueryListEvent) => onChange(event.matches)\n  list.addEventListener('change', handler)\n  return () => list.removeEventListener('change', handler)\n}\n",
      "type": "registry:lib",
      "target": "@ui/kida/motion/reduced-motion.ts"
    }
  ],
  "meta": {
    "kida": {
      "package": {
        "name": "@kida-ui/react",
        "exportName": "Magnetic",
        "styleImports": [
          "@kida-ui/styles/kida.css"
        ]
      }
    }
  },
  "categories": [
    "interaction"
  ],
  "type": "registry:ui"
}
