UNPKG

primereact

Version:

PrimeReact is an open source UI library for React featuring a rich set of 80+ components, a theme designer, various theme alternatives such as Material, Bootstrap, Tailwind, premium templates and professional support. In addition, it integrates with Prime

2 lines (1 loc) 5.83 kB
import*as e from"react";import{DomHandler as t,ObjectUtils as n}from"primereact/utils";var r=function(t){var n=e.useRef(void 0);return e.useEffect((function(){n.current=t})),n.current},u=function(t){return e.useEffect(t,[])},c=function(t,n){var r=e.useRef(!1);return e.useEffect((function(){if(r.current)return t&&t();r.current=!0}),n)},o=function(t){return e.useEffect((function(){return t}),[])},i=function(u){var c=u.target,i=void 0===c?"document":c,f=u.type,l=u.listener,a=u.options,s=u.when,v=void 0===s||s,g=e.useRef(null),d=e.useRef(null),m=r(a),y=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};n.isNotEmpty(e.target)&&(w(),(e.when||v)&&(g.current=t.getTargetElement(e.target))),!d.current&&g.current&&(d.current=function(e){return l&&l(e)},g.current.addEventListener(f,d.current,a))},w=function(){d.current&&(g.current.removeEventListener(f,d.current,a),d.current=null)};return e.useEffect((function(){v?g.current=t.getTargetElement(i):(w(),g.current=null)}),[i,v]),e.useEffect((function(){!d.current||d.current===l&&m===a||(w(),v&&y())}),[l,a]),o((function(){w()})),[y,w]};function f(e){if(Array.isArray(e))return e}function l(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,u,c=[],o=!0,i=!1;try{for(n=n.call(e);!(o=(r=n.next()).done)&&(c.push(r.value),!t||c.length!==t);o=!0);}catch(e){i=!0,u=e}finally{try{o||null==n.return||n.return()}finally{if(i)throw u}}return c}}function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function s(e,t){if(e){if("string"==typeof e)return a(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?a(e,t):void 0}}function v(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function g(e,t){return f(e)||l(e,t)||s(e,t)||v()}var d=function(u){var c=u.target,i=u.listener,f=u.options,l=u.when,a=void 0===l||l,s=e.useRef(null),v=e.useRef(null),g=e.useRef([]),d=r(f),m=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if(n.isNotEmpty(e.target)&&(y(),(e.when||a)&&(s.current=t.getTargetElement(e.target))),!v.current&&s.current){var r=g.current=t.getScrollableParents(s.current);v.current=function(e){return i&&i(e)},r.forEach((function(e){return e.addEventListener("scroll",v.current,f)}))}},y=function(){v.current&&(g.current.forEach((function(e){return e.removeEventListener("scroll",v.current,f)})),v.current=null)};return e.useEffect((function(){a?s.current=t.getTargetElement(c):(y(),s.current=null)}),[c,a]),e.useEffect((function(){!v.current||v.current===i&&d===f||(y(),a&&m())}),[i,f]),o((function(){y()})),[m,y]},m=function(e){return i({target:"window",type:"resize",listener:e.listener})},y=function(n){var r=n.target,u=n.overlay,c=n.listener,f=n.when,l=void 0===f||f,a=e.useRef(null),s=e.useRef(null),v=g(i({type:"click",listener:function(e){c&&c(e,{type:"outside",valid:3!==e.which&&T(e)})}}),2),y=v[0],w=v[1],E=g(m({listener:function(e){c&&c(e,{type:"resize",valid:!t.isTouchDevice()})}}),2),h=E[0],p=E[1],S=g(d({target:a,listener:function(e){c&&c(e,{type:"scroll",valid:!0})}}),2),R=S[0],b=S[1],T=function(e){return a.current&&!(a.current.isSameNode(e.target)||a.current.contains(e.target)||s.current&&s.current.contains(e.target))},I=function(){w(),p(),b()};return e.useEffect((function(){l?(a.current=t.getTargetElement(r),s.current=t.getTargetElement(u)):(I(),a.current=s.current=null)}),[r,u,l]),e.useEffect((function(){I()}),[l]),o((function(){I()})),[function(){y(),h(),R()},I]},w=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=!(arguments.length>2&&void 0!==arguments[2])||arguments[2],u=e.useRef(null),c=e.useRef(null),i=e.useCallback((function(){return clearInterval(u.current)}),[u.current]);return e.useEffect((function(){c.current=t})),e.useEffect((function(){if(r)return u.current=setInterval((function(){c.current()}),n),i;i()}),[n,r]),o((function(){i()})),[i]},E=function(t,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"local",u="undefined"!=typeof window,c=i({target:"window",type:"storage",listener:function(e){var t="local"===r?window.localStorage:window.sessionStorage;e.storageArea===t&&e.key===n&&d(e.newValue||void 0)}}),o=g(c,2),f=o[0],l=o[1],a=e.useState((function(){if(!u)return t;try{var e="local"===r?window.localStorage.getItem(n):window.sessionStorage.getItem(n);return e?JSON.parse(e):t}catch(e){return t}})),s=g(a,2),v=s[0],d=s[1],m=function(e){try{var t=e instanceof Function?e(v):e;if(d(t),u){var c=JSON.stringify(t);"local"===r?window.localStorage.setItem(n,c):window.sessionStorage.setItem(n,c)}}catch(e){throw new Error("PrimeReact useStorage: Failed to serialize the value at key: ".concat(n))}};return e.useEffect((function(){return f(),function(){return l()}}),[]),[v,m]},h=function(e,t){return E(e,t,"local")},p=function(e,t){return E(e,t,"session")},S=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,r=!(arguments.length>2&&void 0!==arguments[2])||arguments[2],u=e.useRef(null),c=e.useRef(null),i=e.useCallback((function(){return clearTimeout(u.current)}),[u.current]);return e.useEffect((function(){c.current=t})),e.useEffect((function(){if(r)return u.current=setTimeout((function(){c.current()}),n),i;i()}),[n,r]),o((function(){i()})),[i]};export{i as useEventListener,w as useInterval,h as useLocalStorage,u as useMountEffect,y as useOverlayListener,d as useOverlayScrollListener,r as usePrevious,m as useResizeListener,p as useSessionStorage,E as useStorage,S as useTimeout,o as useUnmountEffect,c as useUpdateEffect};