{"version":3,"file":"index.cjs","sources":["../src/utils/element-intersection.ts","../src/utils/window-dimension.ts","../src/_root/Provider.tsx"],"sourcesContent":["import { useCallback, useRef } from \"react\";\n\nexport type IntersectionObserverCallback = (\n  entry: IntersectionObserverEntry\n) => void;\nexport type IntersectionObserverSubscriber = (\n  target: Element | null,\n  callback: IntersectionObserverCallback\n) => () => void;\n\nexport default function useElementIntersection(): IntersectionObserverSubscriber {\n  const targetCallbackMapRef = useRef(\n    new Map<Element, IntersectionObserverCallback>()\n  );\n\n  const handleIntersection = useCallback((entry: IntersectionObserverEntry) => {\n    targetCallbackMapRef.current.get(entry.target)?.(entry);\n  }, []);\n\n  const intersectionObserverRef = useRef<IntersectionObserver | null>(\n    typeof window !== \"undefined\"\n      ? new IntersectionObserver((entries) =>\n          entries.forEach(handleIntersection)\n        )\n      : null\n  );\n\n  return useCallback((target, callback) => {\n    const callbackMap = targetCallbackMapRef.current;\n    const observer = intersectionObserverRef.current;\n\n    if (!target || !observer) return () => {};\n\n    callbackMap.set(target, callback);\n    observer.observe(target);\n\n    return () => {\n      callbackMap.delete(target);\n      observer.unobserve(target);\n    };\n  }, []);\n}\n","import { useSyncExternalStore } from \"react\";\n\nimport createSubscriber from \"./create-subscriber.js\";\nimport { roundToDecimal } from \"./math.js\";\n\nconst FALLBACK_HEIGHT = 500;\nconst FALLBACK_WIDTH = 800;\n\nexport interface WindowDimensionOptions {\n  disabled?: boolean;\n  defaultHeight?: number;\n  defaultWidth?: number;\n  precision?: number;\n}\n\nexport interface WindowDimension {\n  readonly windowHeight: number;\n  readonly windowWidth: number;\n}\n\nexport default function useWindowDimension(\n  options: WindowDimensionOptions = {}\n): WindowDimension {\n  const {\n    disabled,\n    defaultHeight = FALLBACK_HEIGHT,\n    defaultWidth = FALLBACK_WIDTH,\n    precision,\n  } = options;\n  const defaultDimension: WindowDimension = {\n    windowHeight: defaultHeight,\n    windowWidth: defaultWidth,\n  };\n\n  return useSyncExternalStore(\n    createSubscriber(\"resize\", disabled),\n    createWindowDimensionSnapshotGetter(\n      defaultHeight,\n      defaultWidth,\n      precision ?? -1\n    ),\n    () => defaultDimension\n  );\n}\n\n// Snapshot getters\n\nfunction createWindowDimensionSnapshotGetter(\n  defaultHeight: number,\n  defaultWidth: number,\n  decimalPlaces: number\n): () => WindowDimension {\n  let dimensions: WindowDimension = {\n    windowHeight: defaultHeight,\n    windowWidth: defaultWidth,\n  };\n\n  return () => {\n    const windowHeight = Math.max(\n      1,\n      roundToDecimal(window.innerHeight, decimalPlaces)\n    );\n    const windowWidth = Math.max(\n      1,\n      roundToDecimal(window.innerWidth, decimalPlaces)\n    );\n\n    if (\n      windowHeight !== dimensions.windowHeight ||\n      windowWidth !== dimensions.windowWidth\n    ) {\n      dimensions = { windowHeight, windowWidth };\n    }\n\n    return dimensions;\n  };\n}\n","import { ScrollyRootContext } from \"./root-context.js\";\nimport useElementIntersection from \"../utils/element-intersection.js\";\nimport useWindowDimension from \"../utils/window-dimension.js\";\n\nexport interface ScrollyProviderProps {\n  /**\n   * Content of the application.\n   * @default null\n   */\n  children: React.ReactNode;\n\n  /**\n   * Default values for the window dimension. Used in server-side rendering.\n   */\n  defaultWindowDimension?: {\n    windowHeight: number;\n    windowWidth: number;\n  };\n}\n\n/**\n * React context provider that provides the window height and width to the scrolly elements.\n */\nexport function ScrollyProvider({\n  children,\n  defaultWindowDimension,\n}: ScrollyProviderProps): JSX.Element {\n  const windowDimension = useWindowDimension({\n    defaultHeight: defaultWindowDimension?.windowHeight,\n    defaultWidth: defaultWindowDimension?.windowWidth,\n  });\n  const observeElementIntersection = useElementIntersection();\n\n  return (\n    <ScrollyRootContext.Provider\n      value={{ ...windowDimension, observeElementIntersection }}\n    >\n      {children}\n    </ScrollyRootContext.Provider>\n  );\n}\n"],"names":["useElementIntersection","targetCallbackMapRef","useRef","handleIntersection","useCallback","entry","_a","intersectionObserverRef","entries","target","callback","callbackMap","observer","FALLBACK_HEIGHT","FALLBACK_WIDTH","useWindowDimension","options","disabled","defaultHeight","defaultWidth","precision","defaultDimension","useSyncExternalStore","createSubscriber","createWindowDimensionSnapshotGetter","decimalPlaces","dimensions","windowHeight","roundToDecimal","windowWidth","ScrollyProvider","children","defaultWindowDimension","windowDimension","observeElementIntersection","jsx","ScrollyRootContext","__spreadProps","__spreadValues"],"mappings":"wlBAUA,SAAwBA,GAAyD,CAC/E,MAAMC,EAAuBC,EAAA,WACvB,GAA2C,EAG3CC,EAAqBC,cAAaC,GAAqC,QAC3EC,EAAAL,EAAqB,QAAQ,IAAII,EAAM,MAAM,IAA7C,MAAAC,EAAiDD,EACnD,EAAG,CAAE,CAAA,EAECE,EAA0BL,EAAA,OAC9B,OAAO,QAAW,YACd,IAAI,qBAAsBM,GACxBA,EAAQ,QAAQL,CAAkB,CAAA,EAEpC,IAAA,EAGC,OAAAC,EAAA,YAAY,CAACK,EAAQC,IAAa,CACvC,MAAMC,EAAcV,EAAqB,QACnCW,EAAWL,EAAwB,QAErC,MAAA,CAACE,GAAU,CAACG,EAAiB,IAAM,CAAA,GAE3BD,EAAA,IAAIF,EAAQC,CAAQ,EAChCE,EAAS,QAAQH,CAAM,EAEhB,IAAM,CACXE,EAAY,OAAOF,CAAM,EACzBG,EAAS,UAAUH,CAAM,CAAA,EAE7B,EAAG,CAAE,CAAA,CACP,CCpCA,MAAMI,EAAkB,IAClBC,EAAiB,IAcC,SAAAC,EACtBC,EAAkC,GACjB,CACX,KAAA,CACJ,SAAAC,EACA,cAAAC,EAAgBL,EAChB,aAAAM,EAAeL,EACf,UAAAM,CACE,EAAAJ,EACEK,EAAoC,CACxC,aAAcH,EACd,YAAaC,CAAA,EAGR,OAAAG,EAAA,qBACLC,EAAA,iBAAiB,SAAUN,CAAQ,EACnCO,EACEN,EACAC,EACAC,GAAA,KAAAA,EAAa,EACf,EACA,IAAMC,CAAA,CAEV,CAIA,SAASG,EACPN,EACAC,EACAM,EACuB,CACvB,IAAIC,EAA8B,CAChC,aAAcR,EACd,YAAaC,CAAA,EAGf,MA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