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