UNPKG

reoverlay

Version:

The missing solution for managing modals in React.

2 lines (1 loc) 6.87 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("react"),r=(e=t)&&"object"==typeof e&&"default"in e?e.default:e;function n(e){return(n="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})(e)}function o(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function a(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function i(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?a(Object(r),!0).forEach((function(t){o(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):a(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function l(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(e)))return;var r=[],n=!0,o=!1,a=void 0;try{for(var i,l=e[Symbol.iterator]();!(n=(i=l.next()).done)&&(r.push(i.value),!t||r.length!==t);n=!0);}catch(e){o=!0,a=e}finally{try{n||null==l.return||l.return()}finally{if(o)throw a}}return r}(e,t)||s(e,t)||function(){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 s(e,t){if(e){if("string"==typeof e)return c(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?c(e,t):void 0}}function c(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}var u,f=(u=new Map,{on:function(e,t){u.has(e)||u.set(e,[]),u.get(e).push(t)},off:function(e){u.delete(e)},emit:function(e,t){u.has(e)&&u.get(e).forEach((function(e){return e(t)}))}}),p=function(e){return"string"==typeof e||e instanceof String},y=function(e){return e[e.length-1]},d="config",h="show_modal",m="hide_modal",v="show_modal",b="hide_modal",g="change_modal",w="hide_all",M=function(e,r){switch(e){case d:if(!function(e){return Array.isArray(e)}(r))throw new Error("Reoverlay: Config data is probably not a valid array. Make sure you pass a valid array to the config method");r.forEach((function(e){if(!e.name||!e.component)throw new Error("Reoverlay: Make sure your config array contains objects with a 'name' and 'component' property.")}));var o=r.map((function(e){return e.name}));if((l=o).some((function(e){return l.filter((function(t){return e===t})).length>1})))throw new Error("Reoverlay: Make sure your config array data is unique.");return!0;case h:var a=function(){throw new Error("Reoverlay: Method 'showModal' has one required argument. Please pass on a React Component, or a pre-configuered identifier string.")};if(r||a(),p(r))return"string";if((i=r)&&"[object Function]"==={}.toString.call(i)||t.isValidElement(r))return"component";a();break;case m:if(p(r))return!0;throw new Error("Reoverlay: Method 'hideModal' has one optional argument. Expected a 'string', got a ".concat(n(r)));default:return!1}var i,l};let O=(e=21)=>{let t="",r=e;for(;r--;)t+="useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict"[64*Math.random()|0];return t};var S={modals:new Map,snappshots:new Map,config:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];M(d,t),t.forEach((function(t){e.modals.set(t.name,t.component)}))},showModal:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=M(h,e);if("string"===r){var n=e,o=this.modals.has(n);if(!o)throw new Error("Reoverlay: Modal not found. You're probably trying to access a pre-configured modal which does not exist. Make sure you already \n passed \"".concat(n,'" to Reoverlay.config().'));var a=this.modals.get(n);this.applyModal({component:a,props:t,modalKey:n,type:v})}else this.applyModal({component:e,props:t,modalKey:O(),type:v})},getSnappshotsArray:function(){var e,t=[],r=function(e){if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(e=s(e))){var t=0,r=function(){};return{s:r,n:function(){return t>=e.length?{done:!0}:{done:!1,value:e[t++]}},e:function(e){throw e},f:r}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var n,o,a=!0,i=!1;return{s:function(){n=e[Symbol.iterator]()},n:function(){var e=n.next();return a=e.done,e},e:function(e){i=!0,o=e},f:function(){try{a||null==n.return||n.return()}finally{if(i)throw o}}}}(this.snappshots.entries());try{for(r.s();!(e=r.n()).done;){var n=l(e.value,2),o=n[0],a=n[1];t.push(i({modalKey:o},a))}}catch(e){r.e(e)}finally{r.f()}return t},hideModal:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;if(e){M(m,e);var t=e,r=this.snappshots.has(t);if(!r)throw new Error("Reoverlay: Snappshot not found. You're probably trying to hide a modal that does not exist.");var n=this.snappshots.get(t);this.applyModal(i(i({},n),{},{modalKey:t,type:b}))}else{var o=this.getSnappshotsArray(),a=y(o)||null;a?this.applyModal(i(i({},a),{},{type:b})):console.error("Reoverlay: There's no active modal to be hidden.")}},hideAll:function(){this.applyModal({type:w})},applyModal:function(e){var t=e.component,r=e.props,n=e.modalKey;switch(e.type){case v:this.snappshots.set(n,{component:t,props:r});break;case w:this.snappshots.clear();break;default:this.snappshots.delete(n)}var o=this.getSnappshotsArray();f.emit(g,o)}},E=function(e){var n=e.children,o=e.wrapperClassName,a=e.contentContainerClassName,i=e.animation,l=e.onClose,s=t.useRef(null);return r.createElement("div",{ref:s,role:"dialog",className:"reOverlay__modalWrapper -ro-".concat(i," ").concat(o),onClick:function(e){e.target===s.current&&function(e){l(e)}(e)}},r.createElement("div",{className:"reOverlay__modalContainer ".concat(a)},n))};E.defaultProps={children:null,wrapperClassName:"",contentContainerClassName:"",animation:"fade",onClose:function(){return S.hideModal()}},exports.ModalContainer=function(){var e=l(t.useState([]),2),n=e[0],o=e[1];return t.useEffect((function(){return f.on(g,(function(e){o(e)})),function(){f.off()}}),[n]),r.createElement("div",{className:"reOverlay"},n.map((function(e){var n=e.modalKey,o=e.component,a=e.props,i=o;return r.createElement(t.Fragment,{key:"id-".concat(n)},r.createElement(i,a))})))},exports.ModalWrapper=E,exports.Reoverlay=S;