{
  "mappings": "AA4BA,OAAO,cAAM,uBAAwB",
  "names": [],
  "sources": [
    "src/useDisableScroll.ts"
  ],
  "version": 3,
  "sourcesContent": [
    "import { useEffect, useRef } from 'react'\n\nconst canUseDOM = () =>\n  typeof window !== 'undefined' && !!window.document && !!window.document.createElement\n\nlet refCount = 0\nlet savedScrollY = 0\nlet previousStyles: {\n  htmlOverflow: string\n  htmlScrollbarGutter: string\n  bodyPosition: string\n  bodyTop: string\n  bodyWidth: string\n  bodyOverflow: string\n  bodyOverscrollBehavior: string\n} | null = null\n\n// detect iOS Safari which needs position:fixed workaround\nfunction isIOSSafari() {\n  if (typeof navigator === 'undefined') return false\n  const ua = navigator.userAgent\n  const isIOS =\n    /iPad|iPhone|iPod/.test(ua) ||\n    (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)\n  const isSafari = /^((?!chrome|android).)*safari/i.test(ua)\n  return isIOS && isSafari\n}\n\nexport const useDisableBodyScroll = (enabled: boolean): void => {\n  const wasEnabled = useRef(false)\n\n  useEffect(() => {\n    if (!canUseDOM()) {\n      return\n    }\n\n    if (enabled && !wasEnabled.current) {\n      wasEnabled.current = true\n\n      if (++refCount === 1) {\n        const html = document.documentElement\n        const body = document.body\n        const isIOS = isIOSSafari()\n\n        // save current scroll position before locking\n        savedScrollY = window.scrollY\n\n        // save previous styles\n        previousStyles = {\n          htmlOverflow: html.style.overflow,\n          htmlScrollbarGutter: html.style.scrollbarGutter,\n          bodyPosition: body.style.position,\n          bodyTop: body.style.top,\n          bodyWidth: body.style.width,\n          bodyOverflow: body.style.overflow,\n          bodyOverscrollBehavior: body.style.overscrollBehavior,\n        }\n\n        // keep the gutter only where a scrollbar was: reserving one on a page\n        // that had none shifts the content by the scrollbar width on every open\n        // iOS uses overlay scrollbars, so locking never removes a gutter.\n        // measuring clientWidth here would flush the opening modal's layout.\n        if (!isIOS && window.innerWidth > html.clientWidth) {\n          html.style.scrollbarGutter = 'stable'\n        }\n        html.style.overflow = 'hidden'\n\n        // prevent scroll chaining on all browsers\n        body.style.overscrollBehavior = 'none'\n\n        // for iOS Safari, use position:fixed workaround\n        // this also helps with any edge cases on other mobile browsers\n        if (isIOS) {\n          body.style.position = 'fixed'\n          body.style.top = `-${savedScrollY}px`\n          body.style.width = '100%'\n          body.style.overflow = 'hidden'\n        }\n      }\n    } else if (!enabled && wasEnabled.current) {\n      wasEnabled.current = false\n\n      if (--refCount === 0 && previousStyles) {\n        const html = document.documentElement\n        const body = document.body\n\n        // restore previous styles\n        html.style.overflow = previousStyles.htmlOverflow\n        html.style.scrollbarGutter = previousStyles.htmlScrollbarGutter\n        body.style.position = previousStyles.bodyPosition\n        body.style.top = previousStyles.bodyTop\n        body.style.width = previousStyles.bodyWidth\n        body.style.overflow = previousStyles.bodyOverflow\n        body.style.overscrollBehavior = previousStyles.bodyOverscrollBehavior\n\n        // restore scroll position (browsers reset to 0 when overflow:hidden is removed)\n        if (savedScrollY > 0) {\n          window.scrollTo(0, savedScrollY)\n        }\n\n        previousStyles = null\n        savedScrollY = 0\n      }\n    }\n  }, [enabled])\n\n  // cleanup on unmount\n  useEffect(() => {\n    return () => {\n      if (wasEnabled.current) {\n        wasEnabled.current = false\n        if (--refCount === 0 && previousStyles) {\n          const html = document.documentElement\n          const body = document.body\n\n          html.style.overflow = previousStyles.htmlOverflow\n          html.style.scrollbarGutter = previousStyles.htmlScrollbarGutter\n          body.style.position = previousStyles.bodyPosition\n          body.style.top = previousStyles.bodyTop\n          body.style.width = previousStyles.bodyWidth\n          body.style.overflow = previousStyles.bodyOverflow\n          body.style.overscrollBehavior = previousStyles.bodyOverscrollBehavior\n\n          if (savedScrollY > 0) {\n            window.scrollTo(0, savedScrollY)\n          }\n\n          previousStyles = null\n          savedScrollY = 0\n        }\n      }\n    }\n  }, [])\n}\n"
  ]
}