{
  "$schema": "https://shadcn-svelte.com/schema/registry-item.json",
  "name": "magnetic",
  "title": "Magnetic",
  "type": "registry:ui",
  "description": "Pulls an isolated child toward a fine pointer and settles with a spring.",
  "dependencies": [
    "motion@^13.1.0"
  ],
  "meta": {
    "kida": {
      "package": {
        "name": "@kida-ui/svelte",
        "exportName": "Magnetic",
        "styleImports": [
          "@kida-ui/styles/kida.css"
        ]
      },
      "framework": "svelte"
    }
  },
  "categories": [
    "interaction"
  ],
  "files": [
    {
      "content": "<script lang=\"ts\">\nimport './magnetic.css'\nimport { magnetic } from './magnetic.motion.js'\nimport { untrack } from 'svelte'\nimport type { MagneticProps } from './magnetic.types.js'\n\nlet {\n  strength,\n  maxDistance,\n  targetClass,\n  element = $bindable(null),\n  children,\n  ...rest\n}: MagneticProps = $props()\n\nlet target = $state<HTMLSpanElement | null>(null)\n\n// A parent's {...spread} invalidates every prop when any one changes. These deriveds only\n// notify on a real value change, so unrelated updates cannot restart the controller.\nconst root = $derived(element)\nconst optionsKey = $derived(`${strength}:${maxDistance}`)\n\n$effect(() => {\n  const outer = root\n  const inner = target\n  void optionsKey\n  if (!outer || !inner) return\n  return untrack(() => magnetic(outer, inner, { strength, maxDistance }))\n})\n</script>\n\n<span {...rest} bind:this={element} data-kida-magnetic=\"\"><span bind:this={target} class={targetClass} data-kida-magnetic-target=\"\">{@render children?.()}</span></span>\n",
      "type": "registry:ui",
      "target": "kida/magnetic.svelte"
    },
    {
      "content": "import type { MagneticOptions } from './magnetic.motion.js';\n\nimport type { HTMLAttributes } from 'svelte/elements';\n\nexport interface MagneticProps extends MagneticOptions, HTMLAttributes<HTMLSpanElement> {\n    /** Class applied to the moving inner span. */\n    targetClass?: string;\n    /** Bind to read the root DOM element; null after unmount. */\n    element?: HTMLSpanElement | null;\n}\n",
      "type": "registry:ui",
      "target": "kida/magnetic.types.ts"
    },
    {
      "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": "kida/magnetic.css"
    },
    {
      "content": "import { motionValue, springValue, styleEffect } from 'motion'\nimport { prefersReducedMotion } from './reduced-motion.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:ui",
      "target": "kida/magnetic.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:ui",
      "target": "kida/reduced-motion.motion.ts"
    }
  ]
}
