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