primereact
Version:
PrimeReact is an open source UI library for React featuring a rich set of 90+ 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) • 16.4 kB
JavaScript
import*as e from"react";import{useEffect as t,useContext as n,useState as r,useRef as u}from"react";import{DomHandler as o,ObjectUtils as c,UniqueComponentId as i,mergeProps as a}from"primereact/utils";import l,{PrimeReactContext as f}from"primereact/api";function s(e){if(Array.isArray(e))return e}function d(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,u,o,c,i=[],a=!0,l=!1;try{if(o=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;a=!1}else for(;!(a=(r=o.call(n)).done)&&(i.push(r.value),i.length!==t);a=!0);}catch(e){l=!0,u=e}finally{try{if(!a&&null!=n.return&&(c=n.return(),Object(c)!==c))return}finally{if(l)throw u}}return i}}function v(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function y(e,t){if(e){if("string"==typeof e)return v(e,t);var n={}.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)?v(e,t):void 0}}function p(){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 m(e,t){return s(e)||d(e,t)||y(e,t)||p()}var g=function(t){var n=e.useRef(null);return e.useEffect((function(){return n.current=t,function(){n.current=null}}),[t]),n.current},h=function(t){return e.useEffect((function(){return t}),[])},w=function(t){var n=t.target,r=void 0===n?"document":n,u=t.type,i=t.listener,a=t.options,l=t.when,f=void 0===l||l,s=e.useRef(null),d=e.useRef(null),v=g(i),y=g(a),p=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.target;c.isNotEmpty(t)&&(m(),(e.when||f)&&(s.current=o.getTargetElement(t))),!d.current&&s.current&&(d.current=function(e){return i&&i(e)},s.current.addEventListener(u,d.current,a))},m=function(){d.current&&(s.current.removeEventListener(u,d.current,a),d.current=null)},w=function(){m(),v=null,y=null},b=e.useCallback((function(){f?s.current=o.getTargetElement(r):(m(),s.current=null)}),[r,f]);return e.useEffect((function(){b()}),[b]),e.useEffect((function(){var e="".concat(v)!=="".concat(i),t=d.current;t&&(e||y!==a)?(m(),f&&p()):t||w()}),[i,a,f]),h((function(){w()})),[p,m]},b=function(t,n){var r=function(e){t.current&&!t.current.contains(e.target)&&n(e)},u=m(w({type:"mousedown",listener:r}),2),o=u[0],c=u[1],i=m(w({type:"touchstart",listener:r}),2),a=i[0],l=i[1];return e.useEffect((function(){if(t.current)return o(),a(),function(){c(),l()}})),[t,n]},E=function(){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{step:1},n=m(e.useState(arguments.length>0&&void 0!==arguments[0]?arguments[0]:0),2),r=n[0],u=n[1];return{count:r,increment:function(){t.max&&r>=t.max||u(r+t.step)},decrement:function(){if(t.min||0===t.min&&r<=t.min)return null;u(r-t.step)},reset:function(){u(0)}}},O=function(t,n){var r=m(e.useState(t),2),u=r[0],o=r[1],c=m(e.useState(t),2),i=c[0],a=c[1],l=e.useRef(!1),f=e.useRef(null),s=function(){return window.clearTimeout(f.current)};return K((function(){l.current=!0})),h((function(){s()})),e.useEffect((function(){l.current&&(s(),f.current=window.setTimeout((function(){a(u)}),n))}),[u,n]),[u,i,o]},S={},j=function(t){var n=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],r=m(e.useState((function(){return i()})),1)[0],u=m(e.useState(0),2),o=u[0],a=u[1];return e.useEffect((function(){if(n){S[t]||(S[t]=[]);var e=S[t].push(r);return a(e),function(){delete S[t][e-1];var n=S[t].length-1,r=c.findLastIndex(S[t],(function(e){return void 0!==e}));r!==n&&S[t].splice(r+1),a(void 0)}}}),[t,r,n]),o},x={ico:"image/x-icon",png:"image/png",svg:"image/svg+xml",gif:"image/gif"},L=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"shortcut icon";e.useLayoutEffect((function(){if(t){document.querySelectorAll("link[rel*='icon']").forEach((function(e){document.head.removeChild(e)}));var e=document.createElement("link");e.setAttribute("type",x[t.split(".").pop()]),e.setAttribute("rel",n),e.setAttribute("href",t),document.head.appendChild(e)}}),[t,n])};function D(e){if(Array.isArray(e))return v(e)}function R(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function P(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function A(e){return D(e)||R(e)||y(e)||P()}var T={SIDEBAR:100,SLIDE_MENU:200,DIALOG:300,IMAGE:400,MENU:500,OVERLAY_PANEL:600,PASSWORD:700,CASCADE_SELECT:800,SPLIT_BUTTON:900,SPEED_DIAL:1e3,TOOLTIP:1200},I={escKeyListeners:new Map,onGlobalKeyDown:function(e){if("Escape"===e.code){var t=I.escKeyListeners,n=Math.max.apply(Math,A(t.keys())),r=t.get(n),u=Math.max.apply(Math,A(r.keys()));r.get(u)(e)}},refreshGlobalKeyDownListener:function(){var e=o.getTargetElement("document");this.escKeyListeners.size>0?e.addEventListener("keydown",this.onGlobalKeyDown):e.removeEventListener("keydown",this.onGlobalKeyDown)},addListener:function(e,t){var n=this,r=m(t,2),u=r[0],o=r[1],c=this.escKeyListeners;c.has(u)||c.set(u,new Map);var i=c.get(u);if(i.has(o))throw new Error("Unexpected: global esc key listener with priority [".concat(u,", ").concat(o,"] already exists."));return i.set(o,e),this.refreshGlobalKeyDownListener(),function(){i.delete(o),0===i.size&&c.delete(u),n.refreshGlobalKeyDownListener()}}},C=function(e){var n=e.callback,r=e.when,u=e.priority;t((function(){if(r)return I.addListener(n,u)}),[n,r,u])},k=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=m(e.useState(!1),2),u=r[0],o=r[1];return e.useEffect((function(){if(t.current){var e=new IntersectionObserver((function(e){var t=m(e,1);o(t[0].isIntersecting)}),n);return e.observe(t.current),function(){e.disconnect()}}}),[n,t]),u},M=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),o=e.useRef(null),c=e.useCallback((function(){return clearInterval(u.current)}),[u.current]);return e.useEffect((function(){o.current=t})),e.useEffect((function(){if(r)return u.current=setInterval((function(){o.current()}),n),c;c()}),[n,r]),h((function(){c()})),[c]},N=function(t){var n=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],r=m(e.useState(!1),2),u=r[0],o=r[1],c=e.useRef(null),i=function(e){return o(e.matches)},a=function(){return c.current&&c.current.removeEventListener("change",i)&&(c.current=null)};return e.useEffect((function(){return n&&(c.current=window.matchMedia(t),o(c.current.matches),c.current&&c.current.addEventListener("change",i)),a}),[t,n]),u},Y=function(){var e=n(f);return function(){for(var t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];return a(n,null==e?void 0:e.ptOptions)}},K=function(t){var n=e.useRef(!1);return e.useEffect((function(){if(!n.current)return n.current=!0,t&&t()}),[])};function X(e){return X="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},X(e)}function G(e,t){if("object"!=X(e)||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=X(r))return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}function z(e){var t=G(e,"string");return"symbol"==X(t)?t:t+""}function _(e,t,n){return(t=z(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function B(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function U(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?B(Object(n),!0).forEach((function(t){_(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):B(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}var V=function(){var t=m(e.useState({x:0,y:0}),2),n=t[0],r=t[1],u=e.useRef(null),o=e.useCallback((function(e){var t,n;if(u.current){var o=e.currentTarget.getBoundingClientRect();t=e.pageX-o.left-(window.pageXOffset||window.scrollX),n=e.pageY-o.top-(window.pageYOffset||window.scrollY)}else t=e.clientX,n=e.clientY;r({x:Math.max(0,Math.round(t)),y:Math.max(0,Math.round(n))})}),[]),c=m(w({target:u,type:"mousemove",listener:o}),2),i=c[0],a=c[1],l=m(w({type:"mousemove",listener:o}),2),f=l[0],s=l[1];return e.useEffect((function(){return i(),u.current||f(),function(){a(),u.current||s()}}),[f,i,s,a]),U(U({ref:u},n),{},{reset:function(){return r({x:0,y:0})}})};function J(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function q(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?J(Object(n),!0).forEach((function(t){_(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):J(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function F(t){var n=t.mode,r=void 0===n?"both":n,u=t.initialValue,o=void 0===u?{x:0,y:0}:u,c=m(e.useState(o),2),i=c[0],a=c[1],l=m(e.useState(!1),2),f=l[0],s=l[1],d=e.useRef(!1),v=e.useRef(!1),y=e.useRef(null),p=function(e){return B({x:e.clientX,y:e.clientY})},g=function(e){var t=e.clampedX,n=e.clampedY;"vertical"===r?a({y:1-n}):"horizontal"===r?a({x:t}):"both"===r&&a({x:t,y:n})},h=function(){v.current&&d.current&&(v.current=!1,s(!1),z())},b=function(e){e.cancelable&&e.preventDefault(),B({x:e.changedTouches[0].clientX,y:e.changedTouches[0].clientY})},E=m(w({type:"mousemove",listener:p}),2),O=E[0],S=E[1],j=m(w({type:"mouseup",listener:h}),2),x=j[0],L=j[1],D=m(w({type:"touchmove",listener:b}),2),R=D[0],P=D[1],A=m(w({type:"touchend",listener:h}),2),T=A[0],I=A[1],C=m(w({target:y,type:"mousedown",listener:function(e){_(),e.preventDefault(),p(e)}}),2),k=C[0],M=C[1],N=m(w({target:y,type:"touchstart",listener:function(e){e.cancelable&&e.preventDefault(),_(),b(e)},options:{passive:!1}}),2),Y=N[0],K=N[1],X=function(e,t,n){return Math.min(Math.max(e,t),n)},G=function(e){var t=e.y;return{clampedX:X(e.x,0,1),clampedY:X(t,0,1)}},z=function(){S(),L(),P(),I()};e.useEffect((function(){d.current=!0}),[]);var _=function(){!v.current&&d.current&&(v.current=!0,s(!0),O(),x(),R(),T())},B=function(e){var t=e.x,n=e.y;if(v.current){var r=y.current.getBoundingClientRect(),u=G({x:(t-r.left)/r.width,y:(n-r.top)/r.height});g({clampedX:u.clampedX,clampedY:u.clampedY})}};return e.useEffect((function(){return y.current&&(k(),Y()),function(){y.current&&(M(),K())}}),[k,Y,i,M,K]),q(q({ref:y},i),{},{active:f,reset:function(){a(o)}})}var W=function(t){var n=t.target,r=t.listener,u=t.options,i=t.when,a=void 0===i||i,s=e.useContext(f),d=e.useRef(null),v=e.useRef(null),y=e.useRef([]),p=g(r),m=g(u),w=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if(c.isNotEmpty(e.target)&&(b(),(e.when||a)&&(d.current=o.getTargetElement(e.target))),!v.current&&d.current){var t=s?s.hideOverlaysOnDocumentScrolling:l.hideOverlaysOnDocumentScrolling,n=y.current=o.getScrollableParents(d.current);n.some((function(e){return e===document.body||e===window}))||n.push(t?window:document.body),v.current=function(e){return r&&r(e)},n.forEach((function(e){return e.addEventListener("scroll",v.current,u)}))}},b=function(){v.current&&(y.current.forEach((function(e){return e.removeEventListener("scroll",v.current,u)})),v.current=null)},E=function(){b(),y.current=null,p=null,m=null},O=e.useCallback((function(){a?d.current=o.getTargetElement(n):(b(),d.current=null)}),[n,a]);return e.useEffect((function(){O()}),[O]),e.useEffect((function(){var e="".concat(p)!=="".concat(r),t=v.current;t&&(e||m!==u)?(b(),a&&w()):t||E()}),[r,u,a]),h((function(){E()})),[w,b]},$=function(e){var t=e.when;return w({target:"window",type:"resize",listener:e.listener,when:void 0===t||t})},H=function(t){var n=t.target,r=t.overlay,u=t.listener,c=t.when,i=void 0===c||c,a=t.type,l=void 0===a?"click":a,f=e.useRef(null),s=e.useRef(null),d=m(w({target:"window",type:l,listener:function(e){u&&u(e,{type:"outside",valid:3!==e.which&&D(e)})},when:i}),2),v=d[0],y=d[1],p=m($({listener:function(e){u&&u(e,{type:"resize",valid:!o.isTouchDevice()})},when:i}),2),g=p[0],b=p[1],E=m(w({target:"window",type:"orientationchange",listener:function(e){u&&u(e,{type:"orientationchange",valid:!0})},when:i}),2),O=E[0],S=E[1],j=m(W({target:n,listener:function(e){u&&u(e,{type:"scroll",valid:!0})},when:i}),2),x=j[0],L=j[1],D=function(e){return f.current&&!(f.current.isSameNode(e.target)||f.current.contains(e.target)||s.current&&s.current.contains(e.target))},R=function(){y(),b(),S(),L()};return e.useEffect((function(){i?(f.current=o.getTargetElement(n),s.current=o.getTargetElement(r)):(R(),f.current=s.current=null)}),[n,r,i]),h((function(){R()})),[function(){v(),g(),O(),x()},R]},Q=function(t,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"local",u="undefined"!=typeof window,o=m(w({target:"window",type:"storage",listener:function(e){var t="local"===r?window.localStorage:window.sessionStorage;if(e.storageArea===t&&e.key===n){var u=e.newValue?JSON.parse(e.newValue):void 0;f(u)}}}),2),c=o[0],i=o[1],a=m(e.useState(t),2),l=a[0],f=a[1];return e.useEffect((function(){u||f(t);try{var e="local"===r?window.localStorage.getItem(n):window.sessionStorage.getItem(n);f(e?JSON.parse(e):t)}catch(e){f(t)}return c(),function(){return i()}}),[]),[l,function(e){try{var t=e instanceof Function?e(l):e;if(f(t),u){var o=JSON.stringify(t);"local"===r?window.localStorage.setItem(n,o):window.sessionStorage.setItem(n,o)}}catch(e){throw new Error("PrimeReact useStorage: Failed to serialize the value at key: ".concat(n))}}]},Z=function(e,t){return Q(e,t,"local")},ee=function(e,t){return Q(e,t,"session")},te=0,ne=function(e){var c=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=m(r(!1),2),a=i[0],s=i[1],d=u(null),v=n(f),y=o.isClient()?window.document:void 0,p=c.document,g=void 0===p?y:p,h=c.manual,w=void 0!==h&&h,b=c.name,E=void 0===b?"style_".concat(++te):b,O=c.id,S=void 0===O?void 0:O,j=c.media,x=void 0===j?void 0:j,L=function(e){var t=e.querySelector('style[data-primereact-style-id="'.concat(E,'"]'));if(t)return t;if(void 0!==S){var n=g.getElementById(S);if(n)return n}return g.createElement("style")},D=function(){if(g&&!a){var t=(null==v?void 0:v.styleContainer)||g.head;d.current=L(t),d.current.isConnected||(d.current.type="text/css",S&&(d.current.id=S),x&&(d.current.media=x),o.addNonce(d.current,v&&v.nonce||l.nonce),t.appendChild(d.current),E&&d.current.setAttribute("data-primereact-style-id",E)),d.current.textContent=e,s(!0)}};return t((function(){w||D()}),[w]),{id:S,name:E,update:function(t){a&&e!==t&&(d.current.textContent=t)},unload:function(){g&&d.current&&(o.removeInlineStyle(d.current),s(!1))},load:D,isLoaded:a}},re=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),o=e.useRef(null),c=e.useCallback((function(){return clearTimeout(u.current)}),[u.current]);return e.useEffect((function(){o.current=t})),e.useEffect((function(){if(r)return u.current=setTimeout((function(){o.current()}),n),c;c()}),[n,r]),h((function(){c()})),[c]},ue=function(t,n){var r=e.useRef(!1);return e.useEffect((function(){if(r.current)return t&&t();r.current=!0}),n)};export{T as ESC_KEY_HANDLING_PRIORITIES,b as useClickOutside,E as useCounter,O as useDebounce,j as useDisplayOrder,w as useEventListener,L as useFavicon,C as useGlobalOnEscapeKey,k as useIntersectionObserver,M as useInterval,Z as useLocalStorage,N as useMatchMedia,Y as useMergeProps,K as useMountEffect,V as useMouse,F as useMove,H as useOverlayListener,W as useOverlayScrollListener,g as usePrevious,$ as useResizeListener,ee as useSessionStorage,Q as useStorage,ne as useStyle,re as useTimeout,h as useUnmountEffect,ue as useUpdateEffect};