react-perfect-scrollbar-z
Version:
React-Wrap PerfectScrollbar (Hooks)
2 lines (1 loc) • 9.58 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@delpi/common"),require("react"),require("perfect-scrollbar")):"function"==typeof define&&define.amd?define(["exports","@delpi/common","react","perfect-scrollbar"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactPerfectScrollbar={},e.common,e.React,e.PerfectScrollbar)}(this,(function(e,t,r,n){"use strict";function l(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=l(r),c=l(n),i={"ps-scroll-y":"onScrollY","ps-scroll-x":"onScrollX","ps-scroll-up":"onScrollUp","ps-scroll-down":"onScrollDown","ps-scroll-left":"onScrollLeft","ps-scroll-right":"onScrollRight","ps-y-reach-start":"onYReachStart","ps-y-reach-end":"onYReachEnd","ps-x-reach-start":"onXReachStart","ps-x-reach-end":"onXReachEnd"},s=function(e){var t=e.height,r=e.maxHeight,n=e.width,l=e.maxWidth,o={};return(t||r)&&(o.height=t||void 0,o.maxHeight=r||void 0),n&&(o.width=n),l&&(o.maxWidth=l),o},u=function(e,t){if(t){var r={};return Object.keys(i).forEach((function(n){var l=e[i[n]];l&&(r[n]=function(e){return l(e)},t.addEventListener(n,r[n],!1))})),r}},a=function(e,t){Object.keys(e).forEach((function(r){return t.removeEventListener(r,e[r],!1)}))};function f(e){return void 0===e&&(e=[]),e.reduce((function(e,t){return e+t.clientHeight||0}),0)}var d=function(e,t,r){if(e.current&&r){var n=e.current.querySelector("table"),l=n?n.querySelector("tbody"):null;if(l)return Object.keys(t).forEach((function(e){l.style[e]=t[e]})),{table:n,el:null==n?void 0:n.querySelector("tbody")}}return{el:e.current}},p=function(e,r,n){var l=(n||t.HtmlUtils.findFirstLevel(e,r)).filter((function(e){return t.HtmlUtils.isVisible(e)}));return l.length>0?l[l.length-1]:{}},v=function(e,t){return e.length<=0?[]:Array.from(e).filter((function(e){return!t.includes(e)}))},h=function(){return h=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var l in t=arguments[r])Object.prototype.hasOwnProperty.call(t,l)&&(e[l]=t[l]);return e},h.apply(this,arguments)};"function"==typeof SuppressedError&&SuppressedError;var g=":scope>.ps__rail-x",m=":scope>.ps__rail-y",y="".concat(g,", ").concat(g,">.ps__thumb-x, ").concat(m,", ").concat(m,">.ps__thumb-y"),S=function(e){var t=r.useState(!1),n=t[0],l=t[1],o=function(e){var t=r.useRef(null),n=r.useState(!1),l=n[0],o=n[1];return r.useEffect((function(){try{"IntersectionObserver"in window&&(t.current=new IntersectionObserver((function(e){var t=e[0];return o(t.isIntersecting)})))}catch(e){o(!0)}}),[]),r.useEffect((function(){if(t.current&&e.current)return t.current.observe(e.current),function(){var e;null===(e=t.current)||void 0===e||e.disconnect()}}),[e]),l}(e);return r.useEffect((function(){o&&l(!0)}),[o]),n},b=function(e,t,n){void 0===t&&(t=!1);var l=r.useState({}),o=l[0],c=l[1],i=r.useState(!1),s=i[0],u=i[1],f=r.useRef(),d=r.useRef([]);r.useEffect((function(){p()}),[o]);var p=function(){e&&(d.current=Array.from(e.querySelectorAll(y)),t&&d.current.forEach((function(e){e.addEventListener("click",(function(e){e.stopPropagation(),e.preventDefault()}))})))};return{psElement:d.current,scrollbar:f,setPsEvent:c,triggerScrollbar:p,handleFocus:function(){if(!s&&f.current){u(!0);var e=f.current.element.classList;if(e.contains("ps--active-y")||e.contains("ps--active-x"))return;f.current.update()}},handleClose:function(){f.current&&(a(o,f.current.element),c({}),f.current.destroy(),n&&(n.current=null))}}},w=function(e){var n,l=r.useRef(null),i=r.useRef(null),a=b(e.injectTable?l.current:i.current,e.triggerScrollClick,e.refScroll),g=a.psElement,m=a.scrollbar,y=a.triggerScrollbar,w=a.setPsEvent,E=a.handleFocus,_=a.handleClose,T=t.Hooks.useWindowSize(),H=S(i),R=s(e),C=(null===(n=e.options)||void 0===n?void 0:n.wheelPropagation)||e.wheelStop;r.useEffect((function(){if(H&&i.current){var t=d(i,R,e.injectTable),r=t.table,n=t.el,o=h({},e.options||{});return o.wheelPropagation=C,r&&(l.current=n,o.suppressScrollX=!0,r.classList.add("list-dk-scroll-view"),r.classList.add("perfect-table"),null==n||n.classList.add(e.always?"scroll-always":""),null==n||n.classList.add(e.wheelStop?"scroll-wheel-stop":"")),m.current=new c.default(n,o),w(u(e,n)),e.refScroll&&(e.refScroll.current=m.current),y(),setTimeout((function(){return m.current.update()}),320),function(){_()}}}),[H,i.current]),r.useEffect((function(){L()}),[e.effectData,T]);var j=function(e){void 0===e&&(e=!0),m.current&&(e&&(m.current.element.scrollTop=0),m.current.update())},L=function(){if(m.current){var r=m.current.element,n=r,l=n.children,o=n.scrollTop,c=n.scrollHeight;if(n.clientHeight>=c||!l.length)j(!0);else{var i=v(l,g);if(i.length){var s=p(r,":not(.ps__rail-x):not(.ps__rail-y)",i);if(s){var u=3*(r.querySelectorAll("div.ps__rail-y").length-1);e.injectTable?setTimeout((function(){var e=p(r,"div.ps__rail-y").clientHeight,n=s.getBoundingClientRect().top-r.getBoundingClientRect().top;if(void 0!==e&&n+s.clientHeight<e){var l=(e<c?c:e)-f(i);if(l>0)return t.HtmlUtils.scrollTo(r,o-l,250),void setTimeout((function(){return j(!1)}),320)}else setTimeout((function(){return j(!1)}))}),u):setTimeout((function(){return j(!1)}),u)}}else j(!0)}}},P=h(h({},e.style),e.injectTable?{}:R),x=e.injectTable?void 0:t.HtmlUtils.joinClass("list-dk-scroll-view","perfect-list",e.className),k=e.injectTable?"scroll-content":t.HtmlUtils.joinClass("scroll-content",{"scroll-always":e.always},{"scroll-wheel-stop":C});return o.default.createElement("div",{id:e.id,className:x,style:P,onMouseEnter:E},o.default.createElement("div",{ref:i,className:k},e.children))};w.defaultProps={wheelStop:!0,style:{}};var E=function(e){var n,l=r.useState("".concat(e.tagName).toLowerCase())[0],i=r.useRef(null),a=b(i.current,e.triggerScrollClick,e.refScroll),d=a.psElement,g=a.scrollbar,m=a.triggerScrollbar,y=a.setPsEvent,w=a.handleFocus,E=a.handleClose,_=t.Hooks.useWindowSize(),T=S(i),H=s(e),R=(null===(n=e.options)||void 0===n?void 0:n.wheelPropagation)||e.wheelStop;r.useEffect((function(){if(T&&i.current){var t=h({},e.options||{});return t.wheelPropagation=R,g.current=new c.default(i.current,t),y(u(e,i.current)),e.refScroll&&(e.refScroll.current=g.current),m(),setTimeout((function(){return g.current.update()}),320),function(){E()}}}),[T,i.current]),r.useEffect((function(){j()}),[e.effectData,_]);var C=function(e){void 0===e&&(e=!0),g.current&&(e&&(g.current.element.scrollTop=0),g.current.update())},j=function(){if(g.current){var e=g.current.element,r=e,n=r.children,l=r.scrollTop,o=r.scrollHeight;if(r.clientHeight>=o||!n.length)C(!0);else{var c=v(n,d);if(c.length){var i=p(e,":not(.ps__rail-x):not(.ps__rail-y)",c);if(i){var s=3*(e.querySelectorAll("div.ps__rail-y").length-1);setTimeout((function(){var r=p(e,"div.ps__rail-y").clientHeight,n=i.getBoundingClientRect().top-e.getBoundingClientRect().top;if(void 0!==r&&n+i.clientHeight<r){var s=(r<o?o:r)-f(c);if(s>0)return t.HtmlUtils.scrollTo(e,l-s,250),void setTimeout((function(){return C(!1)}),320)}else setTimeout((function(){return C(!1)}))}),s)}}else C(!0)}}};return o.default.createElement(l,{ref:i,id:e.id,style:h(h({},e.style),H),className:t.HtmlUtils.joinClass("list-dk-scroll-view","perfect-list",{"scroll-always":e.always},{"scroll-wheel-stop":R}),onMouseEnter:w},e.children)};E.defaultProps={tagName:"ul",wheelStop:!0,style:{}};var _=function(e){var n,l=r.useRef(null),i=b(l.current,e.triggerScrollClick,e.refScroll),a=i.psElement,d=i.scrollbar,g=i.triggerScrollbar,m=i.setPsEvent,y=i.handleFocus,w=i.handleClose,E=t.Hooks.useWindowSize(),_=S(l),T=s(e),H=(null===(n=e.options)||void 0===n?void 0:n.wheelPropagation)||e.wheelStop;r.useEffect((function(){if(_&&l.current){var r=l.current.parentElement;r.classList.add(t.HtmlUtils.joinClass(["list-dk-scroll-view",e.className])),r.classList.add("perfect-table");var n=h({},e.options||{});return n.suppressScrollX=!0,n.wheelPropagation=H,d.current=new c.default(l.current,n),m(u(e,l.current)),e.refScroll&&(e.refScroll.current=d.current),g(),setTimeout((function(){return d.current.update()}),320),function(){w()}}}),[_,l.current]),r.useEffect((function(){C()}),[e.effectData,E]);var R=function(e){void 0===e&&(e=!0),d.current&&(e&&(d.current.element.scrollTop=0),d.current.update())},C=function(){if(d.current){var e=d.current.element,r=e,n=r.children,l=r.scrollTop,o=r.scrollHeight;if(r.clientHeight>=o||!n.length)R(!0);else{var c=v(n,a);if(c.length){var i=p(e,":not(.ps__rail-x):not(.ps__rail-y)",c);if(i){var s=3*(e.querySelectorAll("div.ps__rail-y").length-1);setTimeout((function(){var r=p(e,"div.ps__rail-y").clientHeight,n=i.getBoundingClientRect().top-e.getBoundingClientRect().top;if(void 0!==r&&n+i.clientHeight<r){var s=(r<o?o:r)-f(c);if(s>0)return t.HtmlUtils.scrollTo(e,l-s,250),void setTimeout((function(){return R(!1)}),320)}else setTimeout((function(){return R(!1)}))}),s)}}else R(!0)}}};return o.default.createElement("tbody",{ref:l,id:e.id,style:h(h({},e.style),T),className:t.HtmlUtils.joinClass({"scroll-always":e.always},{"scroll-wheel-stop":H}),onMouseEnter:y},e.children)};_.defaultProps={wheelStop:!0,style:{}};var T=function(e){var t="".concat(e.tagName).toLowerCase();return"tbody"===t?o.default.createElement(_,h({},e)):["ul","dl","ol"].includes(t)?o.default.createElement(E,h({},e,{tagName:t})):o.default.createElement(w,h({},e))};T.defaultProps={tagName:"div",wheelStop:!0,style:{}},e.Scroll=T,e.Scrollbar=w,e.ScrollbarDataList=E,e.ScrollbarTBody=_,e.appendDimension=s,e.appendEventListener=u,e.calculatorRealSize=f,e.clearEventListener=a,e.default=T,e.find1stLevel=p,e.findTargetTable=d,e.psHtmlListSize=v,Object.defineProperty(e,"__esModule",{value:!0})}));