react-snackbars-stack
Version:
A simple and flexible stackable snackbars for React
3 lines (2 loc) • 12.1 kB
JavaScript
"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@mui/material"),r=t(require("@mui/material/Snackbar")),n=require("react"),i=t(n),o={anchorOrigin:{vertical:"bottom",horizontal:"left"},maxSnackbars:4,theme:"light",transitionType:"slide"},a={severity:"info",duration:3e3,preventDuplicate:!1},c=n.createContext(void 0);function u(t,e,r,n,i,o,a){try{var c=t[o](a),u=c.value}catch(t){return void r(t)}c.done?e(u):Promise.resolve(u).then(n,i)}function s(t){return function(){var e=this,r=arguments;return new Promise((function(n,i){var o=t.apply(e,r);function a(t){u(o,n,i,a,c,"next",t)}function c(t){u(o,n,i,a,c,"throw",t)}a(void 0)}))}}function l(){return(l=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)({}).hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t}).apply(null,arguments)}function f(){f=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,i=Object.defineProperty||function(t,e,r){t[e]=r.value},o="function"==typeof Symbol?Symbol:{},a=o.iterator||"@@iterator",c=o.asyncIterator||"@@asyncIterator",u=o.toStringTag||"@@toStringTag";function s(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,r){return t[e]=r}}function l(t,e,r,n){var o=Object.create((e&&e.prototype instanceof m?e:m).prototype),a=new P(n||[]);return i(o,"_invoke",{value:A(t,r,a)}),o}function h(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=l;var p="suspendedStart",v="executing",d="completed",y={};function m(){}function g(){}function w(){}var b={};s(b,a,(function(){return this}));var x=Object.getPrototypeOf,E=x&&x(x(C([])));E&&E!==r&&n.call(E,a)&&(b=E);var k=w.prototype=m.prototype=Object.create(b);function L(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function S(t,e){function r(i,o,a,c){var u=h(t[i],t,o);if("throw"!==u.type){var s=u.arg,l=s.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,a,c)}),(function(t){r("throw",t,a,c)})):e.resolve(l).then((function(t){s.value=t,a(s)}),(function(t){return r("throw",t,a,c)}))}c(u.arg)}var o;i(this,"_invoke",{value:function(t,n){function i(){return new e((function(e,i){r(t,n,e,i)}))}return o=o?o.then(i,i):i()}})}function A(e,r,n){var i=p;return function(o,a){if(i===v)throw Error("Generator is already running");if(i===d){if("throw"===o)throw a;return{value:t,done:!0}}for(n.method=o,n.arg=a;;){var c=n.delegate;if(c){var u=O(c,n);if(u){if(u===y)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(i===p)throw i=d,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);i=v;var s=h(e,r,n);if("normal"===s.type){if(i=n.done?d:"suspendedYield",s.arg===y)continue;return{value:s.arg,done:n.done}}"throw"===s.type&&(i=d,n.method="throw",n.arg=s.arg)}}}function O(e,r){var n=r.method,i=e.iterator[n];if(i===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,O(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var o=h(i,e.iterator,r.arg);if("throw"===o.type)return r.method="throw",r.arg=o.arg,r.delegate=null,y;var a=o.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,y):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function _(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function D(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function P(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function C(e){if(e||""===e){var r=e[a];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var i=-1,o=function r(){for(;++i<e.length;)if(n.call(e,i))return r.value=e[i],r.done=!1,r;return r.value=t,r.done=!0,r};return o.next=o}}throw new TypeError(typeof e+" is not iterable")}return g.prototype=w,i(k,"constructor",{value:w,configurable:!0}),i(w,"constructor",{value:g,configurable:!0}),g.displayName=s(w,u,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===g||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,w):(t.__proto__=w,s(t,u,"GeneratorFunction")),t.prototype=Object.create(k),t},e.awrap=function(t){return{__await:t}},L(S.prototype),s(S.prototype,c,(function(){return this})),e.AsyncIterator=S,e.async=function(t,r,n,i,o){void 0===o&&(o=Promise);var a=new S(l(t,r,n,i),o);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},L(k),s(k,u,"Generator"),s(k,a,(function(){return this})),s(k,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},e.values=C,P.prototype={constructor:P,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(D),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function i(n,i){return c.type="throw",c.arg=e,r.next=n,i&&(r.method="next",r.arg=t),!!i}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],c=a.completion;if("root"===a.tryLoc)return i("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),s=n.call(a,"finallyLoc");if(u&&s){if(this.prev<a.catchLoc)return i(a.catchLoc,!0);if(this.prev<a.finallyLoc)return i(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return i(a.catchLoc,!0)}else{if(!s)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return i(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var i=this.tryEntries[r];if(i.tryLoc<=this.prev&&n.call(i,"finallyLoc")&&this.prev<i.finallyLoc){var o=i;break}}o&&("break"===t||"continue"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var a=o?o.completion:{};return a.type=t,a.arg=e,o?(this.method="next",this.next=o.finallyLoc,y):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),D(r),y}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var i=n.arg;D(r)}return i}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:C(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),y}},e}var h=new(function(){function t(){this.listeners=new Set,this.active=!1,this.queue=[],this.processing=!1,this.activeMessages=new Set}var e=t.prototype;return e.emit=function(){var t=s(f().mark((function t(e){var r,n=this;return f().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(this.active){t.next=2;break}return t.abrupt("return");case 2:if(!e.preventDuplicate||!this.activeMessages.has(e.message)){t.next=4;break}return t.abrupt("return");case 4:if(this.queue.push(e),e.preventDuplicate&&this.activeMessages.add(e.message),this.processing){t.next=14;break}this.processing=!0,r=f().mark((function t(){var e;return f().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(e=n.queue.shift())){t.next=5;break}return n.listeners.forEach((function(t){return t(e)})),t.next=5,new Promise((function(t){return setTimeout(t,100)}));case 5:case"end":return t.stop()}}),t)}));case 9:if(!(this.queue.length>0)){t.next=13;break}return t.delegateYield(r(),"t0",11);case 11:t.next=9;break;case 13:this.processing=!1;case 14:case"end":return t.stop()}}),t,this)})));return function(e){return t.apply(this,arguments)}}(),e.subscribe=function(t){var e=this;return this.listeners.add(t),function(){e.listeners.delete(t),e.queue=[],e.activeMessages.clear()}},e.setActive=function(t){this.active=t},e.removeFromActiveMessages=function(t){this.activeMessages.delete(t)},t}()),p=e.createTheme({palette:{mode:"dark"}}),v=e.createTheme({palette:{mode:"light"}}),d={left:"right",right:"left",up:"down",down:"up"},y=function(t){var u,s=t.anchorOrigin,f=void 0===s?o.anchorOrigin:s,y=t.maxSnackbars,m=void 0===y?o.maxSnackbars:y,g=t.theme,w=void 0===g?o.theme:g,b=t.transitionType,x=void 0===b?o.transitionType:b,E=t.icon,k=t.children,L=n.useReducer((function(t,e){return function(t,e,r){switch(e.type){case"ADD_SNACKBAR":return[].concat(t,[e.payload]).slice(-r);case"CLOSE_SNACKBAR":return t.map((function(t){return t.id===e.payload?(h.removeFromActiveMessages(t.message),l({},t,{open:!1})):t}));case"REMOVE_SNACKBAR":return t.filter((function(t){return t.id!==e.payload}));case"CLEAR_ALL":return t.forEach((function(t){h.removeFromActiveMessages(t.message)})),[];default:return t}}(t,e,m)}),[]),S=L[0],A=L[1],O=n.useRef(Date.now());n.useEffect((function(){document.querySelectorAll("[data-snackbar-provider]").length>1&&console.warn("Multiple SnackbarProvider instances detected!"),h.setActive(!0);var t=h.subscribe((function(t){A({type:"ADD_SNACKBAR",payload:t})}));return function(){h.setActive(!1),t(),A({type:"CLEAR_ALL"})}}),[]);var _=n.useCallback((function(t){return function(e,r){"clickaway"!==r&&(A({type:"CLOSE_SNACKBAR",payload:t}),setTimeout((function(){A({type:"REMOVE_SNACKBAR",payload:t})}),150))}}),[]),D=n.useMemo((function(){return function(t,r){switch(t){case"slide":return function(t){return i.createElement(e.Slide,Object.assign({},t,{direction:d[r.horizontal]}))};case"grow":return e.Grow;case"fade":return e.Fade;case"zoom":return e.Zoom;default:return e.Slide}}(x,f)}),[x,f]),P=n.useCallback((function(t,e){void 0===e&&(e={});var r={id:Date.now(),open:!0,message:t,severity:e.severity||a.severity,duration:e.duration||a.duration,preventDuplicate:e.preventDuplicate||a.preventDuplicate};m===S.length&&_(S[0].id)(),A({type:"ADD_SNACKBAR",payload:r})}),[m,S,_]);return i.createElement(c.Provider,{value:{enqueueSnackbar:P}},i.createElement(e.ThemeProvider,{theme:"dark"===w?p:v},i.createElement(e.Portal,null,i.createElement("div",{"data-snackbar-provider":O.current,style:(u={position:"fixed"},u[f.vertical]=0,u[f.horizontal]=0,u.zIndex=9999,u.pointerEvents="none",u)},S.map((function(t,n){var o;return i.createElement(r,{key:t.id,open:t.open,TransitionComponent:D,autoHideDuration:t.duration,onClose:_(t.id),anchorOrigin:f,sx:(o={"& .MuiSnackbar-root":{position:"static"},pointerEvents:"auto",transition:"all 0.1s ease-in-out"},o[f.vertical]=60*n+24+"px !important",o)},i.createElement(e.Alert,{variant:"filled",onClose:_(t.id),severity:t.severity,sx:{width:"100%"},icon:!0===E?void 0:E},t.message))}))))),k)},m=n.memo(y);exports.DEFAULT_ENQUEUE_OPTIONS=a,exports.OPPOSITE_DIRECTIONS={left:"right",right:"left",up:"down",down:"up"},exports.SnackbarDefaults=o,exports.SnackbarProvider=m,exports.enqueueSnackbar=function(t,e){void 0===e&&(e={});var r={id:Date.now(),open:!0,message:t,severity:e.severity||a.severity,duration:e.duration||a.duration,preventDuplicate:e.preventDuplicate||a.preventDuplicate};h.emit(r)},exports.useSnackbar=function(){var t=n.useContext(c);if(!t)throw new Error("useSnackbar must be used within a SnackbarProvider");return t};
//# sourceMappingURL=react-snackbars-stack.cjs.production.min.js.map