morphing-scroll 2.2.20 → 2.2.21

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/README.md CHANGED
@@ -51,7 +51,10 @@ import Morph from "morphing-scroll";
51
51
  Start using the `MorphScroll` component by defining the required `size` prop. For better precision and control, it's recommended to begin by understanding the `objectsSize` and `progressTrigger` props, which are explained below.
52
52
 
53
53
  > **✦ Note:**
54
- > `MorphScroll` supports only **ESM** (`import`) build
54
+ > `MorphScroll` supports both **ESM** and **CommonJS** builds:
55
+ >
56
+ > - **Modern bundlers** (e.g. _Vite_, _Webpack 5_, _esbuild_) use the **ESM** (`import`) build
57
+ > - **Node.js** (and older environments) use the **CommonJS** (`require`) build
55
58
 
56
59
  <h2></h2>
57
60
 
package/cjs/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react");const t=(e,t=!1,r)=>{let n=[];if(n=Array.isArray(e)?2===e.length?[e[1],e[0],e[1],e[0]]:[...e]:[e,e,e,e],t&&(n=[n[1],n[0],n[3],n[2]]),r){const e=[];for(let t=0;r>t;t++)e.push(n[t%4]);n=e}return n},r=({className:r,children:n,style:o,root:l,threshold:s,rootMargin:i,visibleContent:c=!1,onIntersection:a})=>{const[u,d]=e.useState(!1),m=e.useRef(null),p=e.useMemo((()=>{if(!i)return"";const e=t(i);return`${e[0]}px ${e[1]}px ${e[2]}px ${e[3]}px`}),[i]),f=e.useCallback((([e])=>{d(e.isIntersecting),a&&a({time:e.time,rootBounds:e.rootBounds,boundingClientRect:e.boundingClientRect,intersectionRect:e.intersectionRect,isIntersecting:e.isIntersecting,intersectionRatio:e.intersectionRatio,target:e.target})}),[a]);return e.useEffect((()=>{const e=new IntersectionObserver(f,{root:l,threshold:s,rootMargin:p});return m.current&&e.observe(m.current),()=>{e.disconnect()}}),[f,l,s,p]),e.createElement("div",{"intersection-tracker":"",className:r,ref:m,style:o},c||u?n:null)};r.displayName="IntersectionTracker";const n=({className:t,children:r,style:n,measure:o="inner",onResize:l})=>{const s=e.useRef(null);e.useEffect((()=>{const e=s.current;if(!e)return;const t=new ResizeObserver((e=>{for(const t of e)l&&l(t.contentRect)}));return t.observe(e),()=>{t.unobserve(e),t.disconnect()}}),[o,l]);const i={width:"max-content",height:"max-content"},c={inner:Object.assign({},i),outer:Object.assign({},{width:"100%",height:"100%"}),all:Object.assign({minWidth:"100%",minHeight:"100%"},i)};return e.createElement("div",{"resize-tracker":"",className:t,ref:s,style:Object.assign(Object.assign({willChange:"width, height"},c[o]),n)},r)};function o(t,r){const n=e.useMemo((()=>function(e,t){let r=null,n=null;const o=(...o)=>{"requestFrame"===t?(null!==n&&cancelAnimationFrame(n),n=requestAnimationFrame((()=>{e(...o),n=null}))):(null!==r&&clearTimeout(r),r=setTimeout((()=>{e(...o),r=null}),t))};return o.cancel=()=>{null!==r&&(clearTimeout(r),r=null),null!==n&&(cancelAnimationFrame(n),n=null)},o}(t,r)),[r,t]);return e.useEffect((()=>()=>{n.cancel()}),[n]),n}n.displayName="ResizeTracker";let l=1;const s=({type:t,direction:r,progressReverse:n,size:o,progressTrigger:l,scrollBarOnHover:s,scrollBarEvent:i,thumbSize:c,thumbSpace:a,objLengthPerSize:u,sliderCheckLocal:d,duration:m})=>{const p="sliderMenu"!==t?{onMouseDown:i,onTouchStart:i}:{},f=e.useMemo((()=>{if("scroll"===t||!r)return;const n=["hybrid","y"].includes(r)?"y":"x",s="x"===n?o[0]:o[1];return Array.from({length:u},((r,o)=>e.createElement("div",{key:o,className:"ms-slider-element",style:Object.assign({},"sliderMenu"===t&&{cursor:"pointer"}),onClick:"sliderMenu"===t?()=>{i(s*o,n,m,d)}:void 0},Array.isArray(null==l?void 0:l.progressElement)?l.progressElement[o]:null==l?void 0:l.progressElement)))}),[u,r,o,t,i,null==l?void 0:l.progressElement]);return e.createElement(e.Fragment,null,"scroll"===t?e.createElement("div",{className:"ms-bar",style:Object.assign(Object.assign(Object.assign({position:"absolute",width:"fit-content"},"x"===r?Object.assign({transformOrigin:"left top",height:o[0]+"px"},n?{top:0,transform:"rotate(-90deg) translateX(-100%)"}:{transform:"rotate(-90deg)"}):Object.assign({top:0,height:"100%"},n?{left:0}:{right:0})),0!=!(null==l?void 0:l.progressElement)&&{pointerEvents:"none"}),s&&{opacity:0,transition:"opacity 0.1s ease-in-out"})},e.createElement("div",Object.assign({className:"ms-thumb","data-direction":["hybrid","y"].includes(r)?"y":r},p,{style:Object.assign(Object.assign({height:c+"px",willChange:"transform, height",transform:`translateY(${a}px)`},(null==l?void 0:l.progressElement)&&{cursor:"grab"}),{transition:"height 0.05s ease-in-out"})}),null==l?void 0:l.progressElement)):u>1&&(null==l?void 0:l.progressElement)&&e.createElement("div",Object.assign({className:"ms-slider","data-direction":["hybrid","y"].includes(r)?"y":r},p,{style:Object.assign(Object.assign(Object.assign({position:"absolute",display:"flex"},"slider"===t&&{cursor:"grab"}),s&&{opacity:0,transition:"opacity 0.1s ease-in-out"}),"x"===r?Object.assign({transformOrigin:"left top",left:"50%",transform:"translateX(-50%)"},n?{top:0}:{bottom:0}):Object.assign({flexDirection:"column",top:"50%",transform:"translateY(-50%)"},n?{left:0}:{right:0}))}),f))};s.displayName="ScrollBar";var i=e.memo(s);const c=({edgeGradient:t,visibility:r,edgeType:n})=>{if(!t)return null;const o=Object.assign(Object.assign(Object.assign(Object.assign({position:"absolute",pointerEvents:"none",transition:"opacity 0.1s ease-in-out"},t.color&&{background:t.color&&`linear-gradient(${"right"===n||"left"===n?"90deg, ":""}${t.color}, transparent)`}),"right"===n||"left"===n?{height:"100%",width:t.size+"px",top:0}:{width:"100%",height:t.size+"px",left:0}),n?{[n]:0}:{}),"right"===n?{transform:"scaleX(-1)"}:"bottom"===n?{transform:"scaleY(-1)"}:{});return e.createElement("div",{className:"ms-edge"+(n?" "+n:""),style:Object.assign(Object.assign({},o),{opacity:r?1:0})})};c.displayName="Edge";var a=e.memo(c);const u=({activity:t,arrows:r,arrowType:n,handleArrow:o,size:l})=>{const s=Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({position:"absolute",display:"flex",justifyContent:"center",alignItems:"center",cursor:"pointer",width:r.size+"px"},n?{[n]:0}:{}),"top"===n&&{transform:"rotate(-90deg) translateX(-100%)",transformOrigin:"left top"}),"bottom"===n&&{transform:"rotate(90deg) translateX(-100%)",transformOrigin:"left bottom"}),"left"===n&&{transform:"scaleX(-1)"}),["top","bottom"].includes(n)?{height:l+"px"}:{height:"100%",top:0});return e.createElement("div",{className:`ms-arrow-box ${n}${t?" active":""}`,style:Object.assign({},s),onClick:()=>o(n)},r.element)};u.displayName="Arrow";var d=e.memo(u);function m(e,t){return e>t?Math.floor(e/t):1}function p(e,t=0,r=1/0){return Math.max(t,Math.min(Math.round(e),r))}const f=(e,t,r)=>{if(!t)return;const n=t.querySelectorAll("."+("scroll"===e?"ms-thumb":"ms-slider"));n.length>0&&(r.current=n)},h=e=>e?"start"===e?"flex-start":"center"===e?"center":"flex-end":void 0;function g(e,t,r=0,n=0){return o=>{var l,s,i,c;const a={width:(null!==(l=o.width)&&void 0!==l?l:0)-r,height:(null!==(s=o.height)&&void 0!==s?s:0)-n};(null===(i=e.current)||void 0===i?void 0:i.width)===a.width&&(null===(c=e.current)||void 0===c?void 0:c.height)===a.height||(e.current=a,t())}}function v(t){const r=[],n=t=>{if(null===t||"number"==typeof t||"boolean"==typeof t||"bigint"==typeof t)r.push(t+"");else if("string"==typeof t)r.push(t);else if("function"==typeof t)r.push("<function>");else if(e.isValidElement(t))r.push("<react-node>");else if(Array.isArray(t))t.forEach(n);else if("object"==typeof t){const e=Object.entries(t).sort((([e],[t])=>e.localeCompare(t)));for(const[,t]of e)n(t)}else void 0===t&&r.push("<undefined>")};return n(t),r.join("/")}function y(e){var t,r,n;const o={x:"touches"in e.mouseEvent?e.mouseEvent.touches[0].clientX:e.mouseEvent.clientX,y:"touches"in e.mouseEvent?e.mouseEvent.touches[0].clientY:e.mouseEvent.clientY,leftover:null!==(r=null===(t=e.prevCoordsRef.current)||void 0===t?void 0:t.leftover)&&void 0!==r?r:0},l=null!==(n=e.prevCoordsRef.current)&&void 0!==n?n:o,s={x:o.x-l.x,y:o.y-l.y};e.prevCoordsRef.current=o;const i=t=>{if(!e.scrollElementRef||!e.objectsWrapperRef)return;let r=0;const n="x"===t?"movementX"in e.mouseEvent?e.mouseEvent.movementX:s.x:"movementY"in e.mouseEvent?e.mouseEvent.movementY:s.y,o=e.scrollElementRef;if("thumb"===e.clicked){const s=e.objectsWrapperRef,i=getComputedStyle(s),c="x"===t?o.clientWidth:o.clientHeight,a="x"===t?s.clientWidth+parseFloat(i.marginLeft)+parseFloat(i.marginRight):s.clientHeight+parseFloat(i.marginTop)+parseFloat(i.marginBottom),u=c-e.thumbSize,d=a-c;if(0>=u||0>=d)return;const m=n*(d/u)+l.leftover,p=Math.trunc(m),f=m-p;e.prevCoordsRef.current.leftover=f,r=p}else r=-n;"x"===t?(o.scrollLeft+=r,e.scrollStateRef.targetScrollX=o.scrollLeft):(o.scrollTop+=r,e.scrollStateRef.targetScrollY=o.scrollTop)},c=t=>{const r=e.objectsWrapperRef;if(!r)return;const n="slider"===e.type&&"wrapp"===e.clicked?-1:1,o=s[t]*n,l="x"===t?e.sizeLocal[0]:e.sizeLocal[1],i="x"===t?r.offsetWidth:r.offsetHeight,c="x"===t?e.scrollElementRef.scrollLeft:e.scrollElementRef.scrollTop;e.numForSliderRef.current+=Math.abs(o),e.numForSliderRef.current>6&&!e.isScrollingRef.current&&(o>0&&i>c+l?e.smoothScroll(c+l,t,e.duration):0>o&&c>0&&e.smoothScroll(c-l,t,e.duration),e.numForSliderRef.current=0)},a=t=>{"thumb"===e.clicked?i(t):"wrapp"===e.clicked?"slider"===e.type?c(t):i(t):"slider"===e.clicked&&c(t)};"hybrid"===e.direction?"wrapp"===e.clicked?(a("x"),a("y")):e.axisFromAtr&&a(e.axisFromAtr):a(e.direction||"y")}function b(e){if(e.controller.abort(),document.body.style.removeProperty("cursor"),["thumb","slider"].includes(e.clicked)&&e.mouseOnEl(e.scrollBar),"wrapp"===e.clicked&&e.mouseOnEl(e.objectsWrapperRef),e.clickedObject.current="none",e.scrollBarOnHover){let t=e.mouseEvent.target,r=!1;for(;t&&t!==document.body;){if(t===e.scrollContentRef){r=!0;break}t=t.parentNode}r||e.mouseOnRefHandle(e.mouseEvent)}e.prevCoordsRef.current=null,e.triggerUpdate()}const x=e=>{e&&("grab"===e.style.cursor?(e.style.cursor="grabbing",e.classList.add("active")):"grabbing"===e.style.cursor&&(e.style.cursor="grab",e.classList.remove("active")))},E="wrap-id",j=new Map,O=(e,t,r)=>{w(e);const n=setTimeout((()=>{t(),j.delete(e)}),r);j.set(e,n)},w=e=>{const t=j.get(e);t&&(clearTimeout(t),j.delete(e))},R=({type:r="scroll",className:s,size:c,objectsSize:u,direction:w="y",gap:R,wrapperMargin:S,wrapperMinSize:M,progressReverse:k=!1,progressTrigger:T={wheel:!0},scrollBarOnHover:z=!1,suspending:A=!1,fallback:C,scrollPosition:L,edgeGradient:F,children:N,onScrollValue:I,elementsAlign:$,elementsDirection:W="row",wrapperAlign:X,isScrolling:B,render:H,emptyElements:Y,crossCount:q})=>{var P,D,V,_,K,G,U;const J=o((()=>{Mt()}),40),Q=o((()=>{gt()}),34),Z=function(){const t=e.useRef(null);return null===t.current&&(t.current=""+l++),t.current}();if(!c)throw Error(`Prop "size" is not provided\nMorphScroll〈♦${Z}〉`);Object.keys(T).length;const[ee,te]=e.useState(0),re=()=>{te((e=>"number"==typeof e&&1e3>e?e+1:0))},ne=e.useRef(null),oe=e.useRef(null),le=e.useRef(null),se=e.useRef(null),ie=e.useRef([]),ce=e.useRef(null),ae=e.useRef("none"),ue=e.useRef({loaded:[],empty:[]}),de=e.useRef({targetScrollY:0,targetScrollX:0,animating:!1,animationFrameId:0}),me=e.useRef(!1),pe=e.useRef(0),fe=e.useRef(null);function he(){return e.useRef({width:0,height:0})}const ge=he(),ve=he(),ye=he(),[be,xe,Ee,je,Oe,we,Re,Se]=function(...e){return e.map(v)}(null==L?void 0:L.value,H,c,u,Y,M,X,R),Me=e.useMemo((()=>{var e,t;return{value:"number"==typeof(null==L?void 0:L.value)||"string"==typeof(null==L?void 0:L.value)?[L.value,L.value]:null!==(e=null==L?void 0:L.value)&&void 0!==e?e:[null],duration:null!==(t=null==L?void 0:L.duration)&&void 0!==t?t:200}}),[be,null==L?void 0:L.duration]),ke={color:null,size:40},Te=e.useMemo((()=>"object"==typeof F?Object.assign(Object.assign({},ke),F):ke),[F]),ze=e.useMemo((()=>Object.assign(Object.assign({},ke),"object"==typeof T.arrows?T.arrows:{})),[T.arrows]),Ae=e.useCallback((t=>{if(null==t)return[];if(e.isValidElement(t)){const r=t;return r.type===e.Fragment?e.Children.toArray(r.props.children).flatMap(Ae):[r]}return[t]}),[]),Ce=e.useMemo((()=>e.Children.toArray(N).flatMap(Ae).map((t=>e.isValidElement(t)&&t.key?t.key+"":null)).filter((e=>null!==e)).filter((e=>{var t;return"clear"!==(null==Y?void 0:Y.mode)||!(null===(t=ue.current.empty)||void 0===t?void 0:t.includes(e))}))),[N,Oe,null===(P=ue.current.empty)||void 0===P?void 0:P.join()]),[Le,Fe,Ne,Ie]=S?t(S):[0,0,0,0],$e=Le+Ne,We=Ie+Fe,[Xe,Be]=e.useMemo((()=>{var e,t;return"number"==typeof R?[R,R]:Array.isArray(R)?[null!==(e=R[1])&&void 0!==e?e:0,null!==(t=R[0])&&void 0!==t?t:0]:[0,0]}),[R]),He=e.useMemo((()=>t((null==H?void 0:H.rootMargin)||0,"x"===w)),[xe,w]),[Ye,qe]=He?[He[2],He[0]]:[0,0],Pe=e.useMemo((()=>{const[e,t]=Array.isArray(c)?c:"number"==typeof c?[c,c]:[ge.current.width,ge.current.height];if(!T.arrows||!ze.size)return[e,t,e,t];const r=2*ze.size;let n=e,o=t;return"x"===w?n=e-r:"y"===w?o=t-r:"hybrid"===w&&(n=e-r,o=t-r),[n,o,e,t]}),[Ee,ze.size,ge.current]),De="x"===w?Pe[0]:Pe[1],Ve=e.useMemo((()=>u?Array.isArray(u)?u:t(u,!0,2):[null,null]),[je]),_e=e.useMemo((()=>{const e=(e,t)=>"number"==typeof e?e:"firstChild"===e?t:0;return[e(Ve[0]&&"none"!==Ve[0]&&"size"!==Ve[0]?Ve[0]:"y"===w&&"none"!==Ve[0]||"size"===Ve[0]?Pe[0]:0,ye.current.width),e(Ve[1]&&"none"!==Ve[1]&&"size"!==Ve[1]?Ve[1]:"x"===w&&"none"!==Ve[1]||"size"===Ve[1]?Pe[1]:0,ye.current.height)]}),[je,Ee,ye.current,Pe.join()]),Ke=e.useMemo((()=>{const e="x"===w,t="row"===W,r="column"===W,n=e?Pe[1]:Pe[0],o=e?_e[1]+Be:_e[0]+Xe,l="hybrid"===w?o*(Ce.length+1)-o:n,s=Math.floor(l/o)||1,i=q&&s>=q?"hybrid"===w?Math.ceil(s/q):q:s,c=i>1&&i<Ce.length?Math.ceil(Ce.length/i):i>Ce.length?1:Ce.length,a=q&&q<Ce.length,u=e=>Number.isFinite(e)&&e>0?e:1;if("hybrid"===w){const e=a?t?q:i:t?Ce.length:1,n=a?r?q:i:r?Ce.length:1;return[u(e),u(n)]}return[u(i),u(c)]}),[Ce.length,w,_e.join(),Pe.join(),Xe,We,$e,q]),Ge=e.useMemo((()=>{const e=Ke[0]?Ke[0]*Be-Be:0,t="x"===w?Ke[1]:Ke[0];return _e[0]?(_e[0]+Be)*t-Be:(null==H?void 0:H.type)?ye.current.width+e:ve.current.width}),[_e[0],Ke.join(),Be,ve.current.width,ye.current,xe]),Ue=e.useMemo((()=>{const e=1>Ke[1]?1:Ke[0]*Xe-Xe;return _e[1]?"x"===w?(_e[1]+Xe)*Ke[0]-Xe:(_e[1]+Xe)*Ke[1]-Xe:(null==H?void 0:H.type)?ye.current.height+e:ve.current.height}),[_e[1],Ke.join(),Xe,ve.current.height,ye.current,xe]),Je=e.useMemo((()=>Ue+$e),[Ue,$e]),Qe=e.useMemo((()=>Ge+We),[Ge,We]),Ze="x"===w?Qe:Je,et="x"===w?(null===(D=le.current)||void 0===D?void 0:D.scrollLeft)||0:(null===(V=le.current)||void 0===V?void 0:V.scrollTop)||0,tt=et>1&&!0,rt=Ze>Math.round(et+De);let nt=!1,ot=!1;"hybrid"===w&&(nt=((null===(_=le.current)||void 0===_?void 0:_.scrollLeft)||0)>1&&!0,ot=Math.round(((null===(K=le.current)||void 0===K?void 0:K.scrollLeft)||0)+Pe[0])<Qe);const lt=e.useMemo((()=>{if(!T.progressElement||!Ze)return 0;const e=Math.round(De/Ze*De);return!Number.isFinite(e)||0>e?0:e}),[De,Ze,T.progressElement]),st=e.useMemo((()=>{if(!T.progressElement)return 0;const e=Math.round(Pe[0]/Qe*Pe[0]);return!Number.isFinite(e)||0>e?0:e}),[Pe[0],Qe,T.progressElement]),it=e.useMemo((()=>De?Ze-De:Ze),[Ze,De]),ct=e.useMemo((()=>Pe[0]?Qe-Pe[0]:Qe),[Qe,Pe[0]]),at=e.useCallback((()=>{if(!(null==H?void 0:H.type)||1>=Ke[0])return[];const e=Array.from({length:Ce.length},((e,t)=>t)),t=Array.from({length:Ke[0]},(()=>[])),r="x"===w&&"column"===W||"x"!==w&&"row"===W;return e.forEach((e=>{const n=r?e%Ke[0]:Math.floor(e/Ke[1]);t[n]&&t[n].push(e)})),t}),[Ce.length,Ke.join(),xe,W,w]),ut=e.useMemo((()=>{if(!(null==H?void 0:H.type))return[{elementTop:0,elementBottom:0,left:0,right:0}];let e=[],t=0;if($){const r=Array.from({length:Ce.length},((e,t)=>t)),n=Math.abs(Math.floor(Ce.length/Ke[0])*Ke[0]-Ce.length);e=n?r.slice(-n):[],"center"===$?t=(_e[0]+Be)*(Ke[0]-n)/2:"end"===$&&(t=(_e[0]+Be)*(Ke[0]-n))}return Ce.map(((r,n)=>{const o=function(e,t){for(let r=0;r<t.length;r++){const n=t[r].indexOf(e);if(-1!==n)return["x"===w?n:r,["hybrid","y"].includes(w)?n:r]}return[0,0]}(n,at()),l=e.length>0&&e.includes(n)?t:0,s=function(e){const t="x"===w?l:0;return e>0?t+(_e[1]+Xe)*e:t}(Ke[0]>1||["hybrid","x"].includes(w)?o[1]:n),i=_e[1]?s+_e[1]:s,c=function(e){const t="x"===w?0:l;return e>0?t+(_e[0]+Be)*e:t}(1===Ke[0]&&"x"===w?n:o[0]);return{elementTop:s,elementBottom:i,left:c,right:c+_e[0]}}))}),[N,_e.join(),R,xe,Ke[0],W]),dt=e.useMemo((()=>(null==Pe?void 0:Pe.length)&&X?function(e,t,r,n){const[o,l="start"]="string"==typeof e?[e,e]:e,s={display:"flex"};return t[0]>r&&(s.justifyContent=h(o)),t[1]>n&&(s.alignItems=h(l)),s}(X,Pe,Qe,Je):{}),[X,Pe.join(),Je,Qe]),mt=e.useMemo((()=>[m(Qe,Pe[0]),m(Je,Pe[1])]),[Qe,Je,Pe.join()]),pt=e.useMemo((()=>"x"===w?mt[0]:mt[1]),[w,mt[0],mt[1]]),ft=e.useCallback((e=>{if(!z)return;const t=()=>((e,t,r,n)=>{e&&e.querySelectorAll(".ms-bar").forEach(((e,t)=>{const o=e;["mouseleave","mouseup","touchend"].includes(r.type)?(o.style.opacity="0",o.classList.remove("hover"),o.classList.add("leave"),n(`mouseOnRef${t}-anim`,(()=>o.classList.remove("leave")),200)):(o.style.opacity="1",o.classList.add("hover"))}))})(oe.current,0,e,O);"mouseleave"===e.type?!["thumb","slider","wrapp"].includes(ae.current)&&t():t()}),[z,r,ae.current,oe.current]),ht=e.useCallback((e=>{le.current&&(({arrowType:e,scrollElement:t,wrapSize:r,scrollSize:n,smoothScroll:o,duration:l})=>{const s=r[0],i=r[1],c=t.scrollTop,a=t.scrollLeft,u=["top","bottom"].includes(e)?i:s,d=(e,t)=>o(p(e,0,u),t,l);"top"===e&&c>0?d(c-n[1],"y"):"left"===e&&a>0&&d(a-n[0],"x"),"bottom"===e&&c+n[1]!==i?d(c+n[1],"y"):"right"===e&&a+n[0]!==s&&d(a+n[0],"x")})({arrowType:e,scrollElement:le.current,wrapSize:[Qe,Je],scrollSize:Pe,smoothScroll:Ot,duration:Me.duration})}),[le.current,Pe.join(),Qe,Je,Me.duration]),gt=e.useCallback((()=>{f(r,ne.current,ie);const e=le.current;oe.current&&e&&0!==ie.current.length&&((e,t,r,n)=>{!function(){var o,l,s,i;const c=null!==(l=null===(o=t[0])||void 0===o?void 0:o.querySelectorAll(".ms-slider-element"))&&void 0!==l?l:[],a=null!==(i=null===(s=t[1])||void 0===s?void 0:s.querySelectorAll(".ms-slider-element"))&&void 0!==i?i:[];function u(e,t,r,n){const o="x"===n?r.scrollLeft:r.scrollTop;e.forEach(((e,r)=>{const l="x"===n?t[0]:t[1];o>=l*r&&l*(r+1)>o?e.classList.add("active"):e.classList.remove("active")}))}c.length>0&&u(c,r,e,n),a.length>0&&u(a,r,e,"x")}()})(e,ie.current,Pe,w)}),[Pe.join(),w,le,oe,ie,r]),vt=e.useCallback((()=>{const e=le.current;e&&(null==I||I(e.scrollLeft,e.scrollTop),me.current=!0,null==B||B(!0),O("handleScroll-anim",(()=>{me.current=!1,null==B||B(!1),(null==H?void 0:H.type)?re():J()}),200),"slider"===r&&Q(),(null==H?void 0:H.type)||J(),re())}),[w,I,B,xe,r,Q]),yt=p(et/it*(De-lt),0,De-lt),bt=p(((null===(G=le.current)||void 0===G?void 0:G.scrollLeft)||0)/ct*(Pe[0]-Pe[0]/Qe*Pe[0]),0,Pe[0]-st),xt=o(g(ge,re),40),Et=o(g(ve,re,We,$e),40),jt=o(g(ye,re),40),Ot=e.useCallback(((e,t,r,n)=>{const o=le.current;return o?function(e,t,r,n,o){let l;if(!t&&!n)return null;const s=performance.now(),i=t.scrollTop,c=t.scrollLeft,a=u=>{const d=Math.min((u-s)/r,1);"y"===e?t.scrollTop=i+(n-i)*d:"x"===e&&(t.scrollLeft=c+(n-c)*d),1>d?l=requestAnimationFrame(a):null==o||o()};return l=requestAnimationFrame(a),()=>cancelAnimationFrame(l)}(t,o,r,e,n):null}),[le]),wt=e.useCallback(((e,t="mousedown",n)=>{const o=e||("scroll"===r?"thumb":"slider");if("wrapp"===e&&!T.content||["thumb","slider"].includes(o)&&!T.progressElement)return;f(r,ne.current,ie);let l=null;n&&(l=n.getAttribute("data-direction")),function(e){const t=new AbortController,{signal:r}=t;e.clickedObject.current=e.clicked,e.triggerUpdate(),"mousedown"===e.eventType?(["thumb","slider"].includes(e.clicked)&&e.mouseOnEl(e.scrollBar),"wrapp"===e.clicked&&e.mouseOnEl(e.objectsWrapperRef),document.addEventListener("mousemove",(t=>y(Object.assign(Object.assign({},e),{mouseEvent:t}))),{signal:r}),document.addEventListener("mouseup",(r=>b(Object.assign(Object.assign({},e),{mouseEvent:r,controller:t}))),{signal:r})):"touchstart"===e.eventType&&(document.addEventListener("touchmove",(t=>{t.preventDefault(),y(Object.assign(Object.assign({},e),{mouseEvent:t}))}),{signal:r,passive:!1}),document.addEventListener("touchend",(r=>b(Object.assign(Object.assign({},e),{mouseEvent:r,controller:t}))),{signal:r})),document.body.style.cursor="grabbing"}({eventType:t,scrollElementRef:le.current,objectsWrapperRef:se.current,scrollBar:n||null,clickedObject:ae,scrollContentRef:oe.current,scrollStateRef:de.current,type:r,scrollBarOnHover:z,mouseOnEl:x,mouseOnRefHandle:ft,triggerUpdate:re,direction:w,smoothScroll:Ot,sizeLocal:[Pe[0],Pe[1]],clicked:o,numForSliderRef:pe,isScrollingRef:me,prevCoordsRef:fe,thumbSize:"x"===l?st:lt,axisFromAtr:l,duration:Me.duration})}),[r,T.content,T.progressElement,Pe.join(),lt,st,Me.duration]),Rt=e.useCallback((e=>{wt(null,e.type,e.target)}),[wt]),St=e.useCallback((()=>{wt("wrapp")}),[wt]),Mt=e.useCallback((()=>{ne.current&&((e,t,r,n)=>{const{allIds:o,emptyKeysRaw:l}=(()=>{const t=[],r=[],n=document.createTreeWalker(e,NodeFilter.SHOW_ELEMENT,{acceptNode:e=>e instanceof Element&&e.hasAttribute(E)?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_SKIP});let o=n.nextNode();for(;o;){const e=o,l=e.getAttribute(E);l&&(t.push(l),0===e.children.length&&r.push(l)),o=n.nextNode()}return{allIds:t,emptyKeysRaw:r}})(),s=new Set(t.current.loaded);o.forEach((e=>s.add(e)));let i=null;if(t.current.empty){const e=new Set(t.current.empty);l.forEach((t=>e.add(t))),i=Array.from(e)}t.current={loaded:"lazy"===n?Array.from(s):o.map((e=>e)),empty:i},r()})(ne.current,ue,re,null==H?void 0:H.type)}),[xe]),kt=e.useCallback((e=>{(null==Y?void 0:Y.clickTrigger)&&((e,t,r,n)=>{e.target.closest(r.selector)&&t("emptyKeys-anim",(()=>{n()}),r.delay||0)})(e,O,Y.clickTrigger,Mt)}),[Oe]),Tt=e.useCallback(((e,t,r)=>{if(ce.current){if(ce.current!==Ce[0])return void(ce.current=Ce[0])}else ce.current=Ce[0];const n=Ot(t,e,Me.duration);n&&r.push(n)}),[Ce[0],Me.duration]),zt=e.useMemo((()=>{const e=Object.assign(Object.assign(Object.assign(Object.assign({margin:S?`${Le}px ${Fe}px ${Ne}px ${Ie}px`:"",height:Ve[1]&&"none"!==Ve[1]?Ue+"px":"fit-content",width:Ve[0]&&"none"!==Ve[0]?Ge+"px":"fit-content"},T.content&&{cursor:"grab"}),R&&!(null==H?void 0:H.type)&&{gap:`${Xe}px ${Be}px`}),M&&function(e,t,r,n,o){const l=(e,t)=>"full"===e?("x"===t?r[0]:r[1])-("x"===t?n:o):e;if("hybrid"!==t&&!Array.isArray(e)){const r=l(e,t)+"px";return"x"===t?{minWidth:r}:{minHeight:r}}let s,i;return Array.isArray(e)?[s,i]=e:s=i=e,{minWidth:l(s,"x")+"px",minHeight:l(i,"y")+"px"}}(M,w,Pe,We,$e)),X&&{flexShrink:0});if(null==H?void 0:H.type)return Object.assign(Object.assign({},e),{position:"relative"});const t=Ve[1]?"wrap":void 0,r=1===Ke[0]?"y"===w?"column":"row":W;return Object.assign(Object.assign({},e),{display:"flex",flexDirection:r,flexWrap:t,justifyContent:h($)})}),[S,[Le,Fe,Ne,Ie,We,$e].join(),Ve[1],Ue,Ge,T.content,Se,[Xe,Be].join(),null==H?void 0:H.type,we,w,Pe.join(),Re,Ke[0],W,$]);e.useEffect((()=>{(Y||(null==H?void 0:H.type))&&(Y||null===ue.current.empty||(ue.current.empty=null),J())}),[Oe,null==H?void 0:H.type,xe,me.current,Ce.length]),e.useEffect((()=>(((null==H?void 0:H.type)||B)&&(B&&B(!1),re()),gt(),()=>{de.current.animationFrameId&&cancelAnimationFrame(de.current.animationFrameId),(()=>{for(const e of j.values())clearTimeout(e);j.clear()})()})),[]),e.useEffect((()=>{const e=le.current;if(!e)return;const t=T.wheel?t=>function(e,t,r,n){e.preventDefault(),r.animating||(r.targetScrollX=t.scrollLeft,r.targetScrollY=t.scrollTop),"x"===n?r.targetScrollX=p(r.targetScrollX+e.deltaY,0,t.scrollWidth-t.clientWidth+2):r.targetScrollY=p(r.targetScrollY+e.deltaY,0,t.scrollHeight-t.clientHeight+2),r.animating||(r.animating=!0,r.animationFrameId=requestAnimationFrame((function e(){let o=0;"x"===n?(t.scrollLeft+=.4*(r.targetScrollX-t.scrollLeft),o=Math.abs(t.scrollLeft-r.targetScrollX)):(t.scrollTop+=.4*(r.targetScrollY-t.scrollTop),o=Math.abs(t.scrollTop-r.targetScrollY)),o>2.5?r.animationFrameId=requestAnimationFrame(e):(r.animating=!1,null!==r.animationFrameId&&(cancelAnimationFrame(r.animationFrameId),r.animationFrameId=null))})))}(t,e,de.current,w):e=>e.preventDefault();return e.addEventListener("wheel",t,{passive:!1}),()=>{e.removeEventListener("wheel",t)}}),[w,T.wheel]),e.useEffect((()=>{if(!Me.value)return;const e=[];return("hybrid"===w?["x","y"]:[w]).forEach((t=>{const r="x"===t?0:1;"end"===Me.value[r]&&Tt(t,"x"===t?ct:it,e)})),()=>{e.forEach((e=>e()))}}),[Me.value.join(),be,it,ct,Qe,Je]),e.useEffect((()=>{if(!Me.value)return;const e=[];return("hybrid"===w?["x","y"]:[w]).forEach((t=>{const r="x"===t?0:1;"number"==typeof Me.value[r]&&Tt(t,Me.value[r],e)})),()=>{e.forEach((e=>e()))}}),[be,null==L?void 0:L.updater]);const At=e.useCallback(((t,r,n,o)=>{var l;const s=Object.assign({width:_e[0]?_e[0]+"px":void 0,height:_e[1]?_e[1]+"px":void 0},(null==H?void 0:H.type)&&Object.assign({position:"absolute",top:r+"px",left:n+"px"},!_e[0]&&1===Ke[0]&&{transform:"translateX(-50%)"})),i=A?e.createElement(e.Suspense,{fallback:C},o):o,c=(null===(l=null==Y?void 0:Y.clickTrigger)||void 0===l?void 0:l.selector)&&kt;return e.createElement("div",Object.assign({key:t},H||Y?{[E]:""+t}:{},{className:"ms-object-box",style:s,onClick:c}),i)}),[A,!!C,_e.join(),le.current,xe,Oe,Ke[0]]),Ct=e.createElement("div",{className:"ms-objects-wrapper",ref:se,onMouseDown:St,style:zt},Ce.map(((t,r)=>{var o,l,s;const i=e.Children.toArray(N).find((r=>e.isValidElement(r)&&r.key===t)),c=(null==H?void 0:H.stopLoadOnScroll)&&me.current&&!ue.current.loaded.includes(""+t)?C:(null===(o=ue.current.empty)||void 0===o?void 0:o.includes(t))?"object"==typeof(null==Y?void 0:Y.mode)?Y.mode.fallback:C:i,a="number"==typeof Ve[0]&&"number"==typeof Ve[1]||"firstChild"!==Ve[0]&&"firstChild"!==Ve[1]||0!==r?c:e.createElement(n,{onResize:jt},c);if(!(null==H?void 0:H.type))return At(t,0,0,a);{const{elementTop:e,elementBottom:n,left:o,right:i}=ut[r],c="x"===w?Ye:qe,u=De+c>("x"===w?o:e)-et&&("x"===w?i:n)-et>0-c,d="hybrid"!==w||Pe[0]+Ye>o-((null===(l=le.current)||void 0===l?void 0:l.scrollLeft)||0)&&i-((null===(s=le.current)||void 0===s?void 0:s.scrollLeft)||0)>0-Ye;if(u&&d)return At(t,e,o,a)}}))),Lt=[{positionType:"x"===w?"left":"top",visibility:tt},{positionType:"x"===w?"right":"bottom",visibility:rt},..."hybrid"===w?[{positionType:"left",visibility:nt},{positionType:"right",visibility:ot}]:[]],Ft=e.createElement("div",{"morph-scroll":`〈♦${Z}〉`,className:s,ref:ne,style:{width:Pe[2]+"px",height:Pe[3]+"px"}},e.createElement("div",{className:"ms-content",ref:oe,onMouseEnter:ft,onMouseLeave:ft,onTouchStart:ft,onTouchEnd:ft,style:Object.assign({position:"relative",width:Pe[0]+"px",height:Pe[1]+"px"},T.arrows&&ze.size&&("x"===w?{left:ze.size+"px"}:"y"===w?{top:ze.size+"px"}:{top:ze.size+"px",left:ze.size+"px"}))},e.createElement("div",{className:"ms-element",ref:le,onScroll:vt,style:Object.assign(Object.assign(Object.assign({width:"100%",height:"100%"},dt),{overflow:null!==(U={x:Qe>Pe[0]?"scroll hidden":"hidden",y:Je>Pe[1]?"hidden scroll":"hidden",hybrid:`${Qe>Pe[0]?"scroll":"hidden"} ${Je>Pe[1]?"scroll":"hidden"}`,hide:"hidden"}[T.wheel||T.content?w:"hide"])&&void 0!==U?U:"hidden"}),"scroll"!==r||"boolean"!=typeof T.progressElement||!1===T.progressElement?{scrollbarWidth:"none"}:{})},_e[0]&&_e[1]?Ct:e.createElement(n,{onResize:Et,style:Object.assign({},dt)},Ct)),F&&Lt.map((({positionType:t,visibility:r})=>e.createElement(a,{key:"edge-"+t,edgeGradient:Te,visibility:r,edgeType:t}))),T.progressElement&&!0!==T.progressElement&&[{shouldRender:Ze>lt,direction:w,thumbSize:lt,thumbSpace:yt,objLengthPerSize:pt,progressReverseIndex:0},{shouldRender:"hybrid"===w&&Qe>st,direction:"x",thumbSize:st,thumbSpace:bt,objLengthPerSize:mt[0],progressReverseIndex:1}].filter((({shouldRender:e})=>e)).map((t=>e.createElement(i,{key:t.direction,type:r,direction:t.direction,progressReverse:"boolean"==typeof k?k:k[t.progressReverseIndex],size:Pe,progressTrigger:T,scrollBarOnHover:z,scrollBarEvent:"sliderMenu"===r?Ot:Rt,thumbSize:t.thumbSize,thumbSpace:t.thumbSpace,objLengthPerSize:t.objLengthPerSize,sliderCheckLocal:gt,duration:Me.duration})))),T.arrows&&Lt.map((({positionType:t,visibility:r})=>e.createElement(d,{key:"arrow-"+t,activity:r,arrows:ze,arrowType:t,handleArrow:ht,size:"hybrid"===w?Pe[0]+2*ze.size:Pe[0]}))));return"auto"===c?e.createElement(n,{measure:"outer",onResize:xt},Ft):Ft};R.displayName="MorphScroll";const S={MorphScroll:R,ResizeTracker:n,IntersectionTracker:r};exports.IntersectionTracker=r,exports.MorphScroll=R,exports.ResizeTracker=n,exports.default=S;
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "morphing-scroll",
3
- "version": "2.2.20",
3
+ "version": "2.2.21",
4
4
  "description": "React library for custom object scrolling and scrollbar styling",
5
5
  "author": "Georg Schilin",
6
6
  "license": "MIT",
7
- "module": "./index.js",
7
+ "main": "cjs/index.js",
8
+ "module": "esm/index.js",
8
9
  "exports": {
9
10
  ".": {
10
- "import": "./index.js",
11
- "types": "./types/index.d.ts"
12
- },
13
- "./package.json": "./package.json"
11
+ "import": "./esm/index.js",
12
+ "require": "./cjs/index.js"
13
+ }
14
14
  },
15
15
  "types": "./types/index.d.ts",
16
16
  "keywords": [
File without changes