{"version":3,"file":"overlayscrollbars-react.mjs","sources":["../src/useOverlayScrollbars.ts","../src/OverlayScrollbarsComponent.tsx"],"sourcesContent":["// eslint-disable-next-line @typescript-eslint/no-unused-vars\r\nimport React, { useEffect, useMemo, useRef } from 'react';\r\nimport { OverlayScrollbars } from 'overlayscrollbars';\r\nimport type { InitializationTarget } from 'overlayscrollbars';\r\nimport type {\r\n  OverlayScrollbarsComponentProps,\r\n  OverlayScrollbarsComponentRef,\r\n} from './OverlayScrollbarsComponent';\r\n\r\ntype Defer = [\r\n  requestDefer: (callback: () => any, options?: OverlayScrollbarsComponentProps['defer']) => void,\r\n  cancelDefer: () => void\r\n];\r\n\r\nexport interface UseOverlayScrollbarsParams {\r\n  /** OverlayScrollbars options. */\r\n  options?: OverlayScrollbarsComponentProps['options'];\r\n  /** OverlayScrollbars events. */\r\n  events?: OverlayScrollbarsComponentProps['events'];\r\n  /** Whether to defer the initialization to a point in time when the browser is idle. (or to the next frame if `window.requestIdleCallback` is not supported) */\r\n  defer?: OverlayScrollbarsComponentProps['defer'];\r\n}\r\n\r\nexport type UseOverlayScrollbarsInitialization = (target: InitializationTarget) => void;\r\n\r\nexport type UseOverlayScrollbarsInstance = () => ReturnType<\r\n  OverlayScrollbarsComponentRef['osInstance']\r\n>;\r\n\r\nconst createDefer = (): Defer => {\r\n  /* c8 ignore start */\r\n  if (typeof window === 'undefined') {\r\n    // mock ssr calls with \"noop\"\r\n    // eslint-disable-next-line @typescript-eslint/no-empty-function\r\n    const noop = () => {};\r\n    return [noop, noop];\r\n  }\r\n  /* c8 ignore end */\r\n\r\n  let idleId: number;\r\n  let rafId: number;\r\n  const wnd = window;\r\n  const idleSupported = typeof wnd.requestIdleCallback === 'function';\r\n  const rAF = wnd.requestAnimationFrame;\r\n  const cAF = wnd.cancelAnimationFrame;\r\n  const rIdle = idleSupported ? wnd.requestIdleCallback : rAF;\r\n  const cIdle = idleSupported ? wnd.cancelIdleCallback : cAF;\r\n  const clear = () => {\r\n    cIdle(idleId);\r\n    cAF(rafId);\r\n  };\r\n\r\n  return [\r\n    (callback, options) => {\r\n      clear();\r\n      idleId = rIdle(\r\n        idleSupported\r\n          ? () => {\r\n              clear();\r\n              // inside idle its best practice to use rAF to change DOM for best performance\r\n              rafId = rAF(callback);\r\n            }\r\n          : callback,\r\n        typeof options === 'object' ? options : { timeout: 2233 }\r\n      );\r\n    },\r\n    clear,\r\n  ];\r\n};\r\n\r\n/**\r\n * Hook for advanced usage of OverlayScrollbars. (When the OverlayScrollbarsComponent is not enough)\r\n * @param params Parameters for customization.\r\n * @returns A tuple with two values:\r\n * The first value is the initialization function, it takes one argument which is the `InitializationTarget`.\r\n * The second value is a function which returns the current OverlayScrollbars instance or `null` if not initialized.\r\n */\r\nexport const useOverlayScrollbars = (\r\n  params?: UseOverlayScrollbarsParams\r\n): [UseOverlayScrollbarsInitialization, UseOverlayScrollbarsInstance] => {\r\n  const { options, events, defer } = params || {};\r\n  const [requestDefer, cancelDefer] = useMemo<Defer>(createDefer, []);\r\n  const instanceRef = useRef<ReturnType<UseOverlayScrollbarsInstance>>(null);\r\n  const deferRef = useRef(defer);\r\n  const optionsRef = useRef(options);\r\n  const eventsRef = useRef(events);\r\n\r\n  useEffect(() => {\r\n    deferRef.current = defer;\r\n  }, [defer]);\r\n\r\n  useEffect(() => {\r\n    const { current: instance } = instanceRef;\r\n\r\n    optionsRef.current = options;\r\n\r\n    if (OverlayScrollbars.valid(instance)) {\r\n      instance.options(options || {}, true);\r\n    }\r\n  }, [options]);\r\n\r\n  useEffect(() => {\r\n    const { current: instance } = instanceRef;\r\n\r\n    eventsRef.current = events;\r\n\r\n    if (OverlayScrollbars.valid(instance)) {\r\n      instance.on(events || {}, true);\r\n    }\r\n  }, [events]);\r\n\r\n  useEffect(\r\n    () => () => {\r\n      cancelDefer();\r\n      instanceRef.current?.destroy();\r\n    },\r\n    []\r\n  );\r\n\r\n  return useMemo<[UseOverlayScrollbarsInitialization, UseOverlayScrollbarsInstance]>(\r\n    () => [\r\n      (target) => {\r\n        // if already initialized do nothing\r\n        const presentInstance = instanceRef.current;\r\n        if (OverlayScrollbars.valid(presentInstance)) {\r\n          return;\r\n        }\r\n\r\n        const currDefer = deferRef.current;\r\n        const currOptions = optionsRef.current || {};\r\n        const currEvents = eventsRef.current || {};\r\n        const init = () =>\r\n          (instanceRef.current = OverlayScrollbars(target, currOptions, currEvents));\r\n\r\n        if (currDefer) {\r\n          requestDefer(init, currDefer);\r\n        } else {\r\n          init();\r\n        }\r\n      },\r\n      () => instanceRef.current,\r\n    ],\r\n    []\r\n  );\r\n};\r\n","// eslint-disable-next-line @typescript-eslint/no-unused-vars\r\nimport React, { forwardRef, useEffect, useRef, useImperativeHandle } from 'react';\r\nimport type { OverlayScrollbars } from 'overlayscrollbars';\r\nimport type { PartialOptions, EventListeners } from 'overlayscrollbars';\r\nimport type {\r\n  ComponentPropsWithoutRef,\r\n  ElementRef,\r\n  ElementType,\r\n  ForwardedRef,\r\n  ReactElement,\r\n} from 'react';\r\nimport { useOverlayScrollbars } from './useOverlayScrollbars';\r\n\r\ntype OverlayScrollbarsComponentBaseProps<T extends ElementType = 'div'> =\r\n  ComponentPropsWithoutRef<T> & {\r\n    /** Tag of the root element. */\r\n    element?: T;\r\n    /** OverlayScrollbars options. */\r\n    options?: PartialOptions | false | null;\r\n    /** OverlayScrollbars events. */\r\n    events?: EventListeners | false | null;\r\n    /** Whether to defer the initialization to a point in time when the browser is idle. (or to the next frame if `window.requestIdleCallback` is not supported) */\r\n    defer?: boolean | IdleRequestOptions;\r\n  };\r\n\r\nexport type OverlayScrollbarsComponentProps<T extends ElementType = 'div'> =\r\n  OverlayScrollbarsComponentBaseProps<T> & {\r\n    ref?: ForwardedRef<OverlayScrollbarsComponentRef<T>>;\r\n  };\r\n\r\nexport interface OverlayScrollbarsComponentRef<T extends ElementType = 'div'> {\r\n  /** Returns the OverlayScrollbars instance or null if not initialized. */\r\n  osInstance(): OverlayScrollbars | null;\r\n  /** Returns the root element. */\r\n  getElement(): ElementRef<T> | null;\r\n}\r\n\r\nconst OverlayScrollbarsComponent = <T extends ElementType = 'div'>(\r\n  props: OverlayScrollbarsComponentBaseProps<T>,\r\n  ref: ForwardedRef<OverlayScrollbarsComponentRef<T>>\r\n): ReactElement | null => {\r\n  const { element = 'div', options, events, defer, children, ...other } = props;\r\n  const Tag = element;\r\n  const elementRef = useRef<ElementRef<T>>(null);\r\n  const childrenRef = useRef<HTMLDivElement>(null);\r\n  const [initialize, osInstance] = useOverlayScrollbars({ options, events, defer });\r\n\r\n  useEffect(() => {\r\n    const { current: elm } = elementRef;\r\n    const { current: contentsElm } = childrenRef;\r\n\r\n    /* c8 ignore start */\r\n    if (!elm) {\r\n      return;\r\n    }\r\n    /* c8 ignore end */\r\n\r\n    const target = elm as unknown as HTMLElement;\r\n\r\n    initialize(\r\n      element === 'body'\r\n        ? {\r\n            target,\r\n            cancel: {\r\n              body: null,\r\n            },\r\n          }\r\n        : {\r\n            target,\r\n            elements: {\r\n              viewport: contentsElm,\r\n              content: contentsElm,\r\n            },\r\n          }\r\n    );\r\n\r\n    return () => osInstance()?.destroy();\r\n  }, [initialize, element]);\r\n\r\n  useImperativeHandle(\r\n    ref,\r\n    () => {\r\n      return {\r\n        osInstance,\r\n        getElement: () => elementRef.current,\r\n      };\r\n    },\r\n    []\r\n  );\r\n\r\n  return (\r\n    // @ts-ignore\r\n    <Tag data-overlayscrollbars-initialize=\"\" ref={elementRef} {...other}>\r\n      {element === 'body' ? (\r\n        children\r\n      ) : (\r\n        <div data-overlayscrollbars-contents=\"\" ref={childrenRef}>\r\n          {children}\r\n        </div>\r\n      )}\r\n    </Tag>\r\n  );\r\n};\r\n\r\nconst OverlayScrollbarsComponentForwardedRef = forwardRef(OverlayScrollbarsComponent) as <\r\n  T extends ElementType = 'div'\r\n>(\r\n  props: OverlayScrollbarsComponentProps<T>\r\n) => ReturnType<typeof OverlayScrollbarsComponent>;\r\n\r\nexport { OverlayScrollbarsComponentForwardedRef as OverlayScrollbarsComponent };\r\n"],"names":["createDefer","noop","idleId","rafId","wnd","idleSupported","rAF","cAF","rIdle","cIdle","clear","callback","options","useOverlayScrollbars","params","events","defer","requestDefer","cancelDefer","useMemo","instanceRef","useRef","deferRef","optionsRef","eventsRef","useEffect","instance","OverlayScrollbars","_a","target","presentInstance","currDefer","currOptions","currEvents","init","OverlayScrollbarsComponent","props","ref","element","children","other","Tag","elementRef","childrenRef","initialize","osInstance","elm","contentsElm","useImperativeHandle","OverlayScrollbarsComponentForwardedRef","forwardRef"],"mappings":";;AA6BA,MAAMA,IAAc,MAAa;AAE3B,MAAA,OAAO,SAAW,KAAa;AAGjC,UAAMC,IAAO,MAAM;AAAA,IAAA;AACZ,WAAA,CAACA,GAAMA,CAAI;AAAA,EACpB;AAGI,MAAAC,GACAC;AACJ,QAAMC,IAAM,QACNC,IAAgB,OAAOD,EAAI,uBAAwB,YACnDE,IAAMF,EAAI,uBACVG,IAAMH,EAAI,sBACVI,IAAQH,IAAgBD,EAAI,sBAAsBE,GAClDG,IAAQJ,IAAgBD,EAAI,qBAAqBG,GACjDG,IAAQ,MAAM;AAClB,IAAAD,EAAMP,CAAM,GACZK,EAAIJ,CAAK;AAAA,EAAA;AAGJ,SAAA;AAAA,IACL,CAACQ,GAAUC,MAAY;AACf,MAAAF,KACGR,IAAAM;AAAA,QACPH,IACI,MAAM;AACE,UAAAK,KAENP,IAAQG,EAAIK,CAAQ;AAAA,QAAA,IAEtBA;AAAA,QACJ,OAAOC,KAAY,WAAWA,IAAU,EAAE,SAAS,KAAK;AAAA,MAAA;AAAA,IAE5D;AAAA,IACAF;AAAA,EAAA;AAEJ,GASaG,IAAuB,CAClCC,MACuE;AACvE,QAAM,EAAE,SAAAF,GAAS,QAAAG,GAAQ,OAAAC,EAAM,IAAIF,KAAU,CAAA,GACvC,CAACG,GAAcC,CAAW,IAAIC,EAAenB,GAAa,CAAA,CAAE,GAC5DoB,IAAcC,EAAiD,IAAI,GACnEC,IAAWD,EAAOL,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