{"version":3,"file":"useHasIntersected.mjs","sources":["../../../../src/common/hooks/useHasIntersected/useHasIntersected.ts"],"sourcesContent":["import { useEffect, useState } from 'react';\n\nconst ObserverParams: IntersectionObserverInit = {\n  threshold: 0.1,\n};\n\n/**\n * useHasIntersected.\n * Use this custom hook to detect when an element has became visible inside the viewport. This hook checks only if the intersection happend.\n * Once the intersection has happened the hook will not return false even if the element gets out of the viewport.\n *\n * @param {object} params\n * @param {object} [params.elRef] - node object that contains a react reference to the element that needs to be observed.\n * @param {string} [params.loading = 'eager'] - string that contains the type of loading.\n * @param elRef.loading\n * @usage `const [hasIntersected] = useHasIntersected({imageRef,loading});`\n */\nexport const useHasIntersected = ({\n  elRef,\n  loading = 'eager',\n}: {\n  elRef: React.RefObject<HTMLElement>;\n  loading?: 'eager' | 'lazy';\n}) => {\n  const [hasIntersected, setHasIntersected] = useState(false);\n\n  const { current } = elRef || {};\n\n  const isValidReference = () => {\n    return elRef && current;\n  };\n\n  const handleOnIntersect: IntersectionObserverCallback = (entries, observer) => {\n    entries.forEach((entry) => {\n      if (entry.isIntersecting) {\n        setHasIntersected(true);\n\n        if (current) {\n          observer.unobserve(current);\n        }\n      }\n    });\n  };\n\n  useEffect(() => {\n    let observer: IntersectionObserver | undefined;\n    let didCancel = false;\n\n    // Check if window is defined for SSR and Old browsers fallback\n    if (typeof window === 'undefined' || !window.IntersectionObserver || !isValidReference()) {\n      setHasIntersected(true);\n    } else if (current && !didCancel) {\n      observer = new IntersectionObserver(handleOnIntersect, ObserverParams);\n      observer.observe(current);\n    }\n    return () => {\n      didCancel = true;\n      if (observer && current) {\n        observer.unobserve(current);\n      }\n    };\n  }, [elRef]);\n\n  if (loading === 'eager') {\n    return [false];\n  }\n\n  return [hasIntersected];\n};\n"],"names":["ObserverParams","threshold","useHasIntersected","elRef","loading","hasIntersected","setHasIntersected","useState","current","isValidReference","handleOnIntersect","entries","observer","forEach","entry","isIntersecting","unobserve","useEffect","didCancel","window","IntersectionObserver","observe"],"mapp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