UNPKG

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) 12.6 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("primereact/api"),n=require("primereact/csstransition"),r=require("primereact/hooks"),a=require("primereact/portal"),o=require("primereact/ripple"),l=require("primereact/utils");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function u(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:function(){return e[n]}})}})),t.default=e,Object.freeze(t)}var c=u(e),s=i(t);function d(){return d=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},d.apply(this,arguments)}function m(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 p(e){if(Array.isArray(e))return m(e)}function f(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function g(e,t){if(e){if("string"==typeof e)return m(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)?m(e,t):void 0}}function b(){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 y(e){if(Array.isArray(e))return e}function h(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,a,o,l,i=[],u=!0,c=!1;try{if(o=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;u=!1}else for(;!(u=(r=o.call(n)).done)&&(i.push(r.value),i.length!==t);u=!0);}catch(e){c=!0,a=e}finally{try{if(!u&&null!=n.return&&(l=n.return(),Object(l)!==l))return}finally{if(c)throw a}}return i}}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 E(e,t){return y(e)||h(e,t)||g(e,t)||v()}var D={defaultProps:{__TYPE:"Dialog",appendTo:null,ariaCloseIconLabel:null,baseZIndex:0,blockScroll:!1,breakpoints:null,className:null,closable:!0,closeOnEscape:!0,contentClassName:null,contentStyle:null,dismissableMask:!1,draggable:!0,focusOnShow:!0,footer:null,header:null,headerClassName:null,headerStyle:null,icons:null,id:null,keepInViewport:!0,maskClassName:null,maskStyle:null,maximizable:!1,maximized:!1,minX:0,minY:0,modal:!0,onClick:null,onDrag:null,onDragEnd:null,onDragStart:null,onHide:null,onMaskClick:null,onMaximize:null,onResize:null,onResizeEnd:null,onResizeStart:null,onShow:null,position:"center",resizable:!0,rtl:!1,showHeader:!0,style:null,transitionOptions:null,visible:!1,children:void 0},getProps:function(e){return l.ObjectUtils.getMergedProps(e,D.defaultProps)},getOtherProps:function(e){return l.ObjectUtils.getDiffProps(e,D.defaultProps)}},w=c.forwardRef((function(e,i){var u=D.getProps(e),m=u.id?u.id:l.UniqueComponentId(),y=E(c.useState(m),2),h=y[0],v=E(c.useState(!1),2),w=v[0],x=v[1],S=E(c.useState(!1),2),k=S[0],z=S[1],C=E(c.useState(u.maximized),2),O=C[0],H=C[1],N=c.useRef(null),P=c.useRef(null),R=c.useRef(null),I=c.useRef(null),j=c.useRef(null),M=c.useRef(null),U=c.useRef(!1),X=c.useRef(!1),A=c.useRef(null),T=c.useRef(null),Y=c.useRef(null),_=c.useRef(m),q=u.onMaximize?u.maximized:O,L=E(r.useEventListener({type:"keydown",listener:function(e){return ie(e)}}),2),B=L[0],Z=L[1],V=E(r.useEventListener({type:"mousemove",target:function(){return window.document},listener:function(e){return pe(e)}}),2),J=V[0],W=V[1],F=E(r.useEventListener({type:"mouseup",target:function(){return window.document},listener:function(e){return fe(e)}}),2),$=F[0],K=F[1],G=E(r.useEventListener({type:"mousemove",target:function(){return window.document},listener:function(e){return ce(e)}}),2),Q=G[0],ee=G[1],te=E(r.useEventListener({type:"mouseup",target:function(){return window.document},listener:function(e){return se(e)}}),2),ne=te[0],re=te[1],ae=function(e){u.onHide(),e.preventDefault()},oe=function(e){u.dismissableMask&&u.modal&&P.current===e.target&&ae(e),u.onMaskClick&&u.onMaskClick(e)},le=function(e){u.onMaximize?u.onMaximize({originalEvent:e,maximized:!q}):H((function(e){return!e})),e.preventDefault()},ie=function(e){var t=e.currentTarget;if(t&&t.primeDialogParams){var n=t.primeDialogParams,r=n.length,a=n[r-1]?n[r-1].id:void 0;if(a===h){var o=document.getElementById(a);if(u.closable&&u.closeOnEscape&&"Escape"===e.key)ae(e),e.stopImmediatePropagation(),n.splice(r-1,1);else if("Tab"===e.key){e.preventDefault();var i=l.DomHandler.getFocusableElements(o);if(i&&i.length>0)if(document.activeElement){var c=i.indexOf(document.activeElement);e.shiftKey?-1===c||0===c?i[i.length-1].focus():i[c-1].focus():-1===c||c===i.length-1?i[0].focus():i[c+1].focus()}else i[0].focus()}}}},ue=function(e){l.DomHandler.hasClass(e.target,"p-dialog-header-icon")||l.DomHandler.hasClass(e.target.parentElement,"p-dialog-header-icon")||u.draggable&&(U.current=!0,A.current=e.pageX,T.current=e.pageY,N.current.style.margin="0",l.DomHandler.addClass(document.body,"p-unselectable-text"),u.onDragStart&&u.onDragStart(e))},ce=function(e){if(U.current){var t=l.DomHandler.getOuterWidth(N.current),n=l.DomHandler.getOuterHeight(N.current),r=e.pageX-A.current,a=e.pageY-T.current,o=N.current.getBoundingClientRect(),i=o.left+r,c=o.top+a,s=l.DomHandler.getViewport();N.current.style.position="fixed",u.keepInViewport?(i>=u.minX&&i+t<s.width&&(A.current=e.pageX,N.current.style.left=i+"px"),c>=u.minY&&c+n<s.height&&(T.current=e.pageY,N.current.style.top=c+"px")):(A.current=e.pageX,N.current.style.left=i+"px",T.current=e.pageY,N.current.style.top=c+"px"),u.onDrag&&u.onDrag(e)}},se=function(e){U.current&&(U.current=!1,l.DomHandler.removeClass(document.body,"p-unselectable-text"),u.onDragEnd&&u.onDragEnd(e))},de=function(e){u.resizable&&(X.current=!0,A.current=e.pageX,T.current=e.pageY,l.DomHandler.addClass(document.body,"p-unselectable-text"),u.onResizeStart&&u.onResizeStart(e))},me=function(e,t,n){!n&&(n=l.DomHandler.getViewport());var r=parseInt(e);return/^(\d+|(\.\d+))(\.\d+)?%$/.test(e)?r*(n[t]/100):r},pe=function(e){if(X.current){var t=e.pageX-A.current,n=e.pageY-T.current,r=l.DomHandler.getOuterWidth(N.current),a=l.DomHandler.getOuterHeight(N.current),o=N.current.getBoundingClientRect(),i=l.DomHandler.getViewport(),c=!parseInt(N.current.style.top)||!parseInt(N.current.style.left),s=me(N.current.style.minWidth,"width",i),d=me(N.current.style.minHeight,"height",i),m=r+t,p=a+n;c&&(m+=t,p+=n),(!s||m>s)&&o.left+m<i.width&&(N.current.style.width=m+"px"),(!d||p>d)&&o.top+p<i.height&&(N.current.style.height=p+"px"),A.current=e.pageX,T.current=e.pageY,u.onResize&&u.onResize(e)}},fe=function(e){X.current&&(X.current=!1,l.DomHandler.removeClass(document.body,"p-unselectable-text"),u.onResizeEnd&&u.onResizeEnd(e))},ge=function(){N.current.style.position="",N.current.style.left="",N.current.style.top="",N.current.style.margin=""},be=function(){N.current.setAttribute(_.current,"")},ye=function(){var e;u.onShow&&u.onShow(),u.focusOnShow&&!((e=document.activeElement)&&N.current&&N.current.contains(e))&&u.closable&&u.showHeader&&M.current.focus(),Ee()},he=function(){u.modal&&l.DomHandler.addClass(P.current,"p-component-overlay-leave"),u.blockScroll&&l.DomHandler.removeClass(document.body,"p-overflow-hidden")},ve=function(){U.current=!1,l.ZIndexUtils.clear(P.current),x(!1),De()},Ee=function(){we(),(u.blockScroll||u.maximizable&&q)&&l.DomHandler.addClass(document.body,"p-overflow-hidden")},De=function(){xe();var e=u.maximizable&&q;u.modal?(document.primeDialogParams&&document.primeDialogParams.some((function(e){return e.hasBlockScroll}))||e)&&l.DomHandler.removeClass(document.body,"p-overflow-hidden"):(u.blockScroll||e)&&l.DomHandler.removeClass(document.body,"p-overflow-hidden")},we=function(){u.draggable&&(Q(),ne()),u.resizable&&(J(),$()),B();var e,t={id:h,hasBlockScroll:u.blockScroll};document.primeDialogParams=document.primeDialogParams?[].concat(p(e=document.primeDialogParams)||f(e)||g(e)||b(),[t]):[t]},xe=function(){ee(),re(),W(),K(),Z(),document.primeDialogParams=document.primeDialogParams&&document.primeDialogParams.filter((function(e){return e.id!==h}))},Se=function(){Y.current=l.DomHandler.createInlineStyle(s.default.nonce);var e="";for(var t in u.breakpoints)e+="\n @media screen and (max-width: ".concat(t,") {\n .p-dialog[").concat(_.current,"] {\n width: ").concat(u.breakpoints[t]," !important;\n }\n }\n ");Y.current.innerHTML=e};r.useMountEffect((function(){u.visible&&x(!0),u.breakpoints&&Se()})),r.useUpdateEffect((function(){u.visible&&!w&&x(!0),u.visible!==k&&w&&z(u.visible)})),r.useUpdateEffect((function(){w&&(l.ZIndexUtils.set("modal",P.current,s.default.autoZIndex,u.baseZIndex||s.default.zIndex.modal),z(!0))}),[w]),r.useUpdateEffect((function(){u.blockScroll||l.DomHandler[q?"addClass":"removeClass"](document.body,"p-overflow-hidden")}),[u.maximized,O]),r.useUnmountEffect((function(){De(),l.DomHandler.removeInlineStyle(Y.current),l.ZIndexUtils.clear(P.current)})),c.useImperativeHandle(i,(function(){return{props:u,resetPosition:ge,getElement:function(){return N.current},getMask:function(){return P.current},getContent:function(){return R.current},getHeader:function(){return I.current},getFooter:function(){return j.current},getCloseButton:function(){return M.current}}}));var ke,ze,Ce,Oe,He,Ne,Pe,Re,Ie,je=function(){if(u.closable){var e=u.ariaCloseIconLabel||t.localeOption("close");return c.createElement("button",{ref:M,type:"button",className:"p-dialog-header-icon p-dialog-header-close p-link","aria-label":e,onClick:ae},c.createElement("span",{className:"p-dialog-header-close-icon pi pi-times","aria-hidden":"true"}),c.createElement(o.Ripple,null))}return null},Me=function(){if(u.showHeader){var e=je(),t=(s=l.classNames("p-dialog-header-maximize-icon pi",{"pi-window-maximize":!q,"pi-window-minimize":q}),u.maximizable?c.createElement("button",{type:"button",className:"p-dialog-header-icon p-dialog-header-maximize p-link",onClick:le},c.createElement("span",{className:s}),c.createElement(o.Ripple,null)):null),n=l.ObjectUtils.getJSXElement(u.icons,u),r=l.ObjectUtils.getJSXElement(u.header,u),a=h+"_header",i=l.classNames("p-dialog-header",u.headerClassName);return c.createElement("div",{ref:I,style:u.headerStyle,className:i,onMouseDown:ue},c.createElement("div",{id:a,className:"p-dialog-title"},r),c.createElement("div",{className:"p-dialog-header-icons"},n,t,e))}var s;return null},Ue=function(){var e=l.classNames("p-dialog-content",u.contentClassName);return c.createElement("div",{id:h+"_content",ref:R,className:e,style:u.contentStyle},u.children)},Xe=function(){var e=l.ObjectUtils.getJSXElement(u.footer,u);return e&&c.createElement("div",{ref:j,className:"p-dialog-footer"},e)};return w&&(ze=D.getOtherProps(u),Ce=l.classNames("p-dialog p-component",u.className,{"p-dialog-rtl":u.rtl,"p-dialog-maximized":q,"p-dialog-default":!q,"p-input-filled":"filled"===s.default.inputStyle,"p-ripple-disabled":!1===s.default.ripple}),Oe=l.classNames("p-dialog-mask",(ke=["center","left","right","top","top-left","top-right","bottom","bottom-left","bottom-right"].find((function(e){return e===u.position||e.replace("-","")===u.position})))?"p-dialog-".concat(ke):"",{"p-component-overlay p-component-overlay-enter":u.modal,"p-dialog-visible":w,"p-dialog-draggable":u.draggable,"p-dialog-resizable":u.resizable},u.maskClassName),He=Me(),Ne=Ue(),Pe=Xe(),Re=u.resizable?c.createElement("span",{className:"p-resizable-handle",style:{zIndex:90},onMouseDown:de}):null,Ie=c.createElement("div",{ref:P,style:u.maskStyle,className:Oe,onClick:oe},c.createElement(n.CSSTransition,{nodeRef:N,classNames:"p-dialog",timeout:{enter:"center"===u.position?150:300,exit:"center"===u.position?150:300},in:k,options:u.transitionOptions,unmountOnExit:!0,onEnter:be,onEntered:ye,onExiting:he,onExited:ve},c.createElement("div",d({ref:N,id:h,className:Ce,style:u.style,onClick:u.onClick,role:"dialog"},ze,{"aria-labelledby":h+"_header","aria-describedby":h+"_content","aria-modal":u.modal}),He,Ne,Pe,Re))),c.createElement(a.Portal,{element:Ie,appendTo:u.appendTo,visible:!0}))}));w.displayName="Dialog",exports.Dialog=w;