overlayscrollbars-react 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,12 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.5.1
4
+
5
+ - The `package.json` has now an appropriate `exports` field to optimize builds in modern bundlers.
6
+
3
7
  ## 0.5.0
4
8
 
5
9
  ### Features
6
10
 
7
11
  Added the possibility 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)
8
12
  - `OverlayScrollbarsComponent` accepts now the `defer` property
9
- - `useOverlayScrollbars` params accept now the `defer` key
13
+ - `useOverlayScrollbars` params accept now the `defer` property
10
14
  - `useOverlayScrollbars` will now always try to destroy the instance if the component unmounts.
11
15
 
12
16
  ### Breaking Changes
package/README.md CHANGED
@@ -65,7 +65,7 @@ import { OverlayScrollbarsComponent } from "overlayscrollbars-react";
65
65
  ### Properties
66
66
 
67
67
  The component accepts all properties of intrinsic JSX elements such as `div` and `span`.
68
- Additionally it has three optional properties: `element`, `options` and `events`.
68
+ Additionally it has custom optional properties:
69
69
 
70
70
  - `element`: accepts a `string` which represents the tag of the root element.
71
71
  - `options`: accepts an `object` which represents the OverlayScrollbars options.
@@ -121,7 +121,7 @@ The hook will destroy the instance automatically if the component unmounts.
121
121
  ### Parameters
122
122
 
123
123
  Parameters are optional and similar to the `OverlayScrollbarsComponent`.
124
- Its an `object` with two optional properties:
124
+ Its an `object` with optional properties:
125
125
 
126
126
  - `options`: accepts an `object` which represents the OverlayScrollbars options.
127
127
  - `events`: accepts an `object` which represents the OverlayScrollbars events.
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const t=require("react"),b=require("overlayscrollbars"),O=n=>n&&typeof n=="object"&&"default"in n?n:{default:n},R=O(t),E=()=>{if(typeof window>"u"){const c=()=>{};return[c,c]}let n,s;const r=window,l=typeof r.requestIdleCallback=="function",u=r.requestAnimationFrame,f=r.cancelAnimationFrame,o=l?r.requestIdleCallback:u,i=l?r.cancelIdleCallback:f,a=()=>{i(n),f(s)};return[(c,e)=>{a(),n=o(l?()=>{a(),s=u(c)}:c,typeof e=="object"?e:{timeout:2233})},a]},S=n=>{const{options:s,events:r,defer:l}=n||{},[u,f]=t.useMemo(E,[]),o=t.useRef(null),i=t.useRef(l),a=t.useRef(s),c=t.useRef(r);return t.useEffect(()=>{i.current=l},[l]),t.useEffect(()=>{const{current:e}=o;a.current=s,b.OverlayScrollbars.valid(e)&&e.options(s||{},!0)},[s]),t.useEffect(()=>{const{current:e}=o;c.current=r,b.OverlayScrollbars.valid(e)&&e.on(r||{},!0)},[r]),t.useEffect(()=>()=>{var e;f(),(e=o.current)==null||e.destroy()},[]),t.useMemo(()=>[e=>{const y=o.current;if(b.OverlayScrollbars.valid(y))return;const d=i.current,p=a.current||{},v=c.current||{},m=()=>o.current=b.OverlayScrollbars(e,p,v);d?u(m,d):m()},()=>o.current],[])},I=(n,s)=>{const{element:r="div",options:l,events:u,defer:f,children:o,...i}=n,a=r,c=t.useRef(null),e=t.useRef(null),[y,d]=S({options:l,events:u,defer:f});return t.useEffect(()=>{const{current:p}=c,{current:v}=e;return p&&v&&y({target:p,elements:{viewport:v,content:v}}),()=>{var m;return(m=d())==null?void 0:m.destroy()}},[y,r]),t.useImperativeHandle(s,()=>({osInstance:d,getElement:()=>c.current}),[]),R.default.createElement(a,{"data-overlayscrollbars-initialize":"",ref:c,...i},R.default.createElement("div",{ref:e},o))},w=t.forwardRef(I);exports.OverlayScrollbarsComponent=w;exports.useOverlayScrollbars=S;
2
+ //# sourceMappingURL=overlayscrollbars-react.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"overlayscrollbars-react.cjs.js","sources":["../src/useOverlayScrollbars.ts","../src/OverlayScrollbarsComponent.tsx"],"sourcesContent":["import { 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","import { forwardRef, useEffect, useRef, useImperativeHandle } from 'react';\r\nimport type { OverlayScrollbars } from 'overlayscrollbars';\r\nimport type { PartialOptions, EventListeners } from 'overlayscrollbars';\r\nimport type { ComponentPropsWithoutRef, ElementRef, ForwardedRef } from 'react';\r\nimport { useOverlayScrollbars } from './useOverlayScrollbars';\r\n\r\nexport type OverlayScrollbarsComponentProps<T extends keyof JSX.IntrinsicElements = '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 interface OverlayScrollbarsComponentRef<T extends keyof JSX.IntrinsicElements = '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 keyof JSX.IntrinsicElements>(\r\n props: OverlayScrollbarsComponentProps<T>,\r\n ref: ForwardedRef<OverlayScrollbarsComponentRef<T>>\r\n) => {\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: childrenElm } = childrenRef;\r\n if (elm && childrenElm) {\r\n initialize({\r\n target: elm as any,\r\n elements: {\r\n viewport: childrenElm,\r\n content: childrenElm,\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 <div ref={childrenRef}>{children}</div>\r\n </Tag>\r\n );\r\n};\r\n\r\nconst OverlayScrollbarsComponentForwardedRef = forwardRef(OverlayScrollbarsComponent) as <\r\n T extends keyof JSX.IntrinsicElements\r\n>(\r\n props: OverlayScrollbarsComponentProps<T> & {\r\n ref?: ForwardedRef<OverlayScrollbarsComponentRef<T>>;\r\n }\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","childrenElm","useImperativeHandle","React","OverlayScrollbarsComponentForwardedRef","forwardRef"],"mappings":"mOA4BMA,EAAc,IAAa,CAE3B,GAAA,OAAO,OAAW,IAAa,CAGjC,MAAMC,EAAO,IAAM,CAAA,EACZ,MAAA,CAACA,EAAMA,CAAI,CACpB,CAGI,IAAAC,EACAC,EACJ,MAAMC,EAAM,OACNC,EAAgB,OAAOD,EAAI,qBAAwB,WACnDE,EAAMF,EAAI,sBACVG,EAAMH,EAAI,qBACVI,EAAQH,EAAgBD,EAAI,oBAAsBE,EAClDG,EAAQJ,EAAgBD,EAAI,mBAAqBG,EACjDG,EAAQ,IAAM,CAClBD,EAAMP,CAAM,EACZK,EAAIJ,CAAK,CAAA,EAGJ,M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AAW,GAClCU,EAAO,IACVd,EAAY,QAAUO,EAAkB,kBAAAE,EAAQG,EAAaC,CAAU,EAEtEF,EACFd,EAAaiB,EAAMH,CAAS,EAEvBG,GAET,EACA,IAAMd,EAAY,OACpB,EACA,CAAC,CAAA,CAEL,ECtHMe,EAA6B,CACjCC,EACAC,IACG,CACG,KAAA,CAAE,QAAAC,EAAU,MAAO,QAAA1B,EAAS,OAAAG,EAAQ,MAAAC,EAAO,SAAAuB,KAAaC,CAAU,EAAAJ,EAClEK,EAAMH,EACNI,EAAarB,SAAsB,IAAI,EACvCsB,EAActB,SAAuB,IAAI,EACzC,CAACuB,EAAYC,CAAU,EAAIhC,EAAqB,CAAE,QAAAD,EAAS,OAAAG,EAAQ,MAAAC,CAAA,CAAO,EAEhFS,OAAAA,EAAAA,UAAU,IAAM,CACR,KAAA,CAAE,QAASqB,CAAQ,EAAAJ,EACnB,CAAE,QAASK,CAAgB,EAAAJ,EACjC,OAAIG,GAAOC,GACEH,EAAA,CACT,OAAQE,EACR,SAAU,CACR,SAAUC,EACV,QAASA,CACX,CAAA,CACD,EAEI,IAAM,OAAA,OAAAnB,EAAAiB,MAAA,YAAAjB,EAAc,UAAQ,EAClC,CAACgB,EAAYN,CAAO,CAAC,EAExBU,EAAA,oBACEX,EACA,KACS,CACL,WAAAQ,EACA,WAAY,IAAMH,EAAW,OAAA,GAGjC,CAAC,CAAA,EAKAO,EAAA,QAAA,cAAAR,EAAA,CAAI,oCAAkC,GAAG,IAAKC,EAAa,GAAGF,CAAA,EAC5DS,EAAA,QAAA,cAAA,MAAA,CAAI,IAAKN,CAAA,EAAcJ,CAAS,CACnC,CAEJ,EAEMW,EAAyCC,aAAWhB,CAA0B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "overlayscrollbars-react",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "OverlayScrollbars for React.",
5
5
  "author": "Rene Haas | KingSora",
6
6
  "license": "MIT",
@@ -25,6 +25,13 @@
25
25
  "main": "overlayscrollbars-react.umd.js",
26
26
  "module": "overlayscrollbars-react.es.js",
27
27
  "types": "types/overlayscrollbars-react.d.ts",
28
+ "exports": {
29
+ ".": {
30
+ "require": "./overlayscrollbars-react.cjs.js",
31
+ "import": "./overlayscrollbars-react.es.js",
32
+ "types": "./types/overlayscrollbars-react.d.ts"
33
+ }
34
+ },
28
35
  "peerDependencies": {
29
36
  "react": ">=16.8.0",
30
37
  "overlayscrollbars": "^2.0.0"