{"version":3,"sources":["../src/mesh-gradient.tsx","../src/use-mesh-gradient.tsx"],"sourcesContent":["'use client';\n\nimport type { HTMLAttributes } from 'react';\nimport type { MeshGradientInitOptions, MeshGradientOptions, MeshGradientUpdateOptions } from '@mesh-gradient/core';\n\nimport { useEffect, useRef } from 'react';\nimport { MeshGradient as CoreMeshGradient } from '@mesh-gradient/core';\n\nimport { useMeshGradient } from './use-mesh-gradient';\n\nexport interface MeshGradientProps extends HTMLAttributes<HTMLCanvasElement> {\n  options?: MeshGradientOptions & MeshGradientInitOptions & MeshGradientUpdateOptions;\n  /**\n   * Pause the gradient.\n   */\n  isPaused?: boolean;\n  /**\n   * Callback when the gradient is initialized.\n   */\n  onInit?: (instance: CoreMeshGradient) => void;\n  /**\n   * Callback when the gradient is updated.\n   */\n  onUpdate?: (instance: CoreMeshGradient) => void;\n}\n\n/**\n * MeshGradient component that connects the core MeshGradient class with React.\n *\n * @param props - The component props.\n * @returns The MeshGradient component.\n */\nexport const MeshGradient = (props: MeshGradientProps) => {\n  const { options, isPaused, onInit, onUpdate, ...canvasProps } = props;\n\n  const { instance } = useMeshGradient();\n\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const prevOptionsIdRef = useRef<string | null>(null);\n\n  /** Always read latest merged props on init (ref is not a valid effect dependency). */\n  const initialOptionsRef = useRef(options);\n\n  initialOptionsRef.current = options;\n\n  useEffect(() => {\n    if (!instance) return;\n\n    const id = requestAnimationFrame(() => {\n      const canvas = canvasRef.current;\n\n      if (!canvas) return;\n\n      instance.init(canvas, initialOptionsRef.current);\n      onInit?.(instance);\n      prevOptionsIdRef.current = JSON.stringify(initialOptionsRef.current);\n    });\n\n    return () => cancelAnimationFrame(id);\n  }, [instance, onInit]);\n\n  useEffect(() => {\n    if (!instance || !instance.isInitialized || prevOptionsIdRef.current === JSON.stringify(options)) return;\n\n    instance.update(options);\n    onUpdate?.(instance);\n    prevOptionsIdRef.current = JSON.stringify(options);\n  }, [instance, options]);\n\n  useEffect(() => {\n    if (!instance) return;\n\n    if (isPaused) {\n      instance.pause();\n    } else {\n      if (!instance.isInitialized) return;\n\n      instance.play();\n    }\n  }, [instance, isPaused]);\n\n  return <canvas ref={canvasRef} {...canvasProps} />;\n};\n","'use client';\n\nimport { useEffect, useState } from 'react';\nimport { MeshGradient } from '@mesh-gradient/core';\n\n/**\n * Hook to create and manage a MeshGradient instance. Automatically cleans up the instance when the component unmounts.\n *\n * @returns The MeshGradient instance.\n */\nexport const useMeshGradient = () => {\n  const [instance, setInstance] = useState<MeshGradient | null>(null);\n\n  useEffect(() => {\n    const gradient = new MeshGradient();\n\n    setInstance(gradient);\n\n    return () => {\n      gradient.destroy();\n      setInstance(null);\n    };\n  }, []);\n\n  return { instance };\n};\n"],"mappings":";AAKA,OAAS,aAAAA,EAAW,UAAAC,MAAc,QCHlC,OAAS,aAAAC,EAAW,YAAAC,MAAgB,QACpC,OAAS,gBAAAC,MAAoB,sBAOtB,IAAMC,EAAkB,IAAM,CACnC,GAAM,CAACC,EAAUC,CAAW,EAAIJ,EAA8B,IAAI,EAElE,OAAAD,EAAU,IAAM,CACd,IAAMM,EAAW,IAAIJ,EAErB,OAAAG,EAAYC,CAAQ,EAEb,IAAM,CACXA,EAAS,QAAQ,EACjBD,EAAY,IAAI,CAClB,CACF,EAAG,CAAC,CAAC,EAEE,CAAE,SAAAD,CAAS,CACpB,EDwDS,cAAAG,MAAA,oBAjDF,IAAMC,EAAgBC,GAA6B,CACxD,GAAM,CAAE,QAAAC,EAAS,SAAAC,EAAU,OAAAC,EAAQ,SAAAC,EAAU,GAAGC,CAAY,EAAIL,EAE1D,CAAE,SAAAM,CAAS,EAAIC,EAAgB,EAE/BC,EAAYC,EAA0B,IAAI,EAC1CC,EAAmBD,EAAsB,IAAI,EAG7CE,EAAoBF,EAAOR,CAAO,EAExC,OAAAU,EAAkB,QAAUV,EAE5BW,EAAU,IAAM,CACd,GAAI,CAACN,EAAU,OAEf,IAAMO,EAAK,sBAAsB,IAAM,CACrC,IAAMC,EAASN,EAAU,QAEpBM,IAELR,EAAS,KAAKQ,EAAQH,EAAkB,OAAO,EAC/CR,IAASG,CAAQ,EACjBI,EAAiB,QAAU,KAAK,UAAUC,EAAkB,OAAO,EACrE,CAAC,EAED,MAAO,IAAM,qBAAqBE,CAAE,CACtC,EAAG,CAACP,EAAUH,CAAM,CAAC,EAErBS,EAAU,IAAM,CACV,CAACN,GAAY,CAACA,EAAS,eAAiBI,EAAiB,UAAY,KAAK,UAAUT,CAAO,IAE/FK,EAAS,OAAOL,CAAO,EACvBG,IAAWE,CAAQ,EACnBI,EAAiB,QAAU,KAAK,UAAUT,CAAO,EACnD,EAAG,CAACK,EAAUL,CAAO,CAAC,EAEtBW,EAAU,IAAM,CACd,GAAKN,EAEL,GAAIJ,EACFI,EAAS,MAAM,MACV,CACL,GAAI,CAACA,EAAS,cAAe,OAE7BA,EAAS,KAAK,CAChB,CACF,EAAG,CAACA,EAAUJ,CAAQ,CAAC,EAEhBJ,EAAC,UAAO,IAAKU,EAAY,GAAGH,EAAa,CAClD","names":["useEffect","useRef","useEffect","useState","MeshGradient","useMeshGradient","instance","setInstance","gradient","jsx","MeshGradient","props","options","isPaused","onInit","onUpdate","canvasProps","instance","useMeshGradient","canvasRef","useRef","prevOptionsIdRef","initialOptionsRef","useEffect","id","canvas"]}