UNPKG

react-meerkat

Version:
2 lines (1 loc) 6.59 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("react-transition-group"),require("prop-types"),require("react-dom")):"function"==typeof define&&define.amd?define(["exports","react","react-transition-group","prop-types","react-dom"],e):e(t.ReactMeerkat={},t.React,t.ReactTransitionGroup,t.PropTypes,t.ReactDOM)}(this,function(t,e,o,n,r){"use strict";var i="default"in e?e.default:e;n=n&&n.hasOwnProperty("default")?n.default:n;var a=e.createContext(),s=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},u=function(){function t(t,e){for(var o=0;o<e.length;o++){var n=e[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}return function(e,o,n){return o&&t(e.prototype,o),n&&t(e,n),e}}(),c=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var o=arguments[e];for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(t[n]=o[n])}return t},p=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)},l=function(t,e){var o={};for(var n in t)e.indexOf(n)>=0||Object.prototype.hasOwnProperty.call(t,n)&&(o[n]=t[n]);return o},f=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e},d=function(t){var e=t.position,o=t.zIndex;switch(e){case"top left":return{position:"fixed",top:0,right:"auto",bottom:"auto",left:0,zIndex:o};case"top right":return{position:"fixed",top:0,right:0,bottom:"auto",left:"auto",zIndex:o};case"bottom left":return{position:"fixed",top:"auto",right:"auto",bottom:0,left:0,zIndex:o};case"bottom right":return{position:"fixed",top:"auto",right:0,bottom:0,left:"auto",zIndex:o};case"top center":return{position:"fixed",top:0,right:"auto",bottom:"auto",left:"50%",transform:"translate(-50%, 0%)",zIndex:o};case"bottom center":return{position:"fixed",top:"auto",right:"auto",bottom:0,left:"50%",transform:"translate(-50%, 0%)",zIndex:o}}},m=function(t){p(e,t);function e(){var t,o,n;s(this,e);for(var r=arguments.length,i=Array(r),a=0;a<r;a++)i[a]=arguments[a];return o=n=f(this,(t=e.__proto__||Object.getPrototypeOf(e)).call.apply(t,[this].concat(i))),n.styles=d(n.props.options),f(n,o)}return u(e,[{key:"render",value:function(){var t=this.props,e=t.children,o=(t.options,l(t,["children","options"]));return i.createElement("div",c({style:this.styles},o),e)}}]),e}(e.Component),h={fade:{transition:"opacity 250ms ease",opacity:0},scale:{transform:"scale(1)",transition:"all 250ms ease-in-out"}},y={fade:{entering:{opacity:0},entered:{opacity:1}},scale:{entering:{transform:"scale(0)"},entered:{transform:"scale(1)"},exiting:{transform:"scale(0)"},exited:{transform:"scale(1)"}}},v=function(t){var e=t.children,n=t.type,r=l(t,["children","type"]);return i.createElement(o.Transition,c({},r,{timeout:250}),function(t){return i.createElement("div",{style:c({},h[n],y[n][t])},e)})},b=function(t){p(e,t);function e(){var t,o,n;s(this,e);for(var r=arguments.length,i=Array(r),a=0;a<r;a++)i[a]=arguments[a];return o=n=f(this,(t=e.__proto__||Object.getPrototypeOf(e)).call.apply(t,[this].concat(i))),n.state={root:null,alerts:[]},n.timerId=[],n.show=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=Math.random().toString(36).substr(2,9),r=n.props,i=r.timeout,a=r.type,s={id:o,message:t,options:c({timeout:i,type:a},e)};if(s.close=function(){return n.remove(s)},s.options.timeout){var u=setTimeout(function(){n.remove(s),n.timerId.splice(n.timerId.indexOf(u),1)},s.options.timeout);n.timerId.push(u)}return n.setState(function(t){return{alerts:t.alerts.concat(s)}},function(){s.options.onOpen&&s.options.onOpen()}),s},n.remove=function(t){n.setState(function(e){var o=e.alerts.length,n=e.alerts.filter(function(e){return e.id!==t.id});return o>n.length&&t.options.onClose&&t.options.onClose(),{alerts:n}})},n.success=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return e.type="success",n.show(t,e)},n.error=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return e.type="error",n.show(t,e)},n.info=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"",e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return e.type="info",n.show(t,e)},f(n,o)}return u(e,[{key:"componentDidMount",value:function(){var t=document.createElement("div");document.body.appendChild(t),this.setState({root:t})}},{key:"componentWillUnmount",value:function(){this.timerId.forEach(clearTimeout);var t=this.state.root;t&&document.body.removeChild(t)}},{key:"render",value:function(){var t=this.state,e=t.root,n=t.alerts,s=this.props,u=s.children,p=s.offset,l=s.position,f=s.timeout,d=s.type,h=s.transition,y=s.zIndex,b=s.template,g={offset:p,position:l,timeout:f,type:d,transition:h,zIndex:y},O=c({},this.state,{show:this.show,remove:this.remove,success:this.success,error:this.error,info:this.info});return i.createElement(a.Provider,{value:O},u,e&&r.createPortal(i.createElement(m,{options:g},i.createElement(o.TransitionGroup,null,n.map(function(t){return i.createElement(v,{type:g.transition,key:t.id},i.createElement(b,c({style:{margin:g.offset}},t)))}))),e))}}]),e}(e.Component);b.propTypes={offset:n.string,position:n.oneOf(["top left","top right","top center","bottom left","bottom right","bottom center"]),timeout:n.number,type:n.oneOf(["info","success","error"]),transition:n.oneOf(["fade","scale"]),zIndex:n.number,template:n.oneOfType([n.element,n.func]).isRequired},b.defaultProps={offset:"10px",position:"top center",timeout:0,type:"info",transition:"fade",zIndex:100};var g=a.Consumer;t.Provider=b,t.Alert=g,t.withAlert=function(t){var o=function(e){p(o,e);function o(){return s(this,o),f(this,(o.__proto__||Object.getPrototypeOf(o)).apply(this,arguments))}return u(o,[{key:"render",value:function(){var e=this;return i.createElement(a.Consumer,null,function(o){return i.createElement(t,c({},e.props,{alert:o}))})}}]),o}(e.Component);return o.displayName="WithAlert("+(t.displayName||t.name||"Component")+")",o},Object.defineProperty(t,"__esModule",{value:!0})});