UNPKG

react-toastsnack

Version:

An unopinionated notification queue for React

25 lines (22 loc) 6 kB
/** * react-toastsnack v1.4.0 * * Copyright (c) 2019 babangsund * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).ReactToastSnack={},e.React)}(this,(function(e,t){"use strict";t=t&&t.hasOwnProperty("default")?t.default:t;var n=process.env.NODE_ENV,r=function(e,t,r,u,i,o,a,s){if("production"!==n&&void 0===t)throw new Error("invariant requires an error message argument");if(!e){var c;if(void 0===t)c=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var f=[r,u,i,o,a,s],d=0;(c=new Error(t.replace(/%s/g,(function(){return f[d++]})))).name="Invariant Violation"}throw c.framesToPop=1,c}},u=t.createContext(void 0); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ var i=function(){return(i=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var u in t=arguments[n])Object.prototype.hasOwnProperty.call(t,u)&&(e[u]=t[u]);return e}).apply(this,arguments)};var o=0;function a(e){return"string"==typeof e?e:"ReactToastSnack:"+o++}var s=400,c=8,f=48,d=function(){function e(e,t,n,r,u,i){void 0===e&&(e=[]),void 0===n&&(n=!1),void 0===r&&(r=s),void 0===u&&(u=f),void 0===i&&(i=c),this._max=t,this._last=null,this._delay=r,this._height=u,this._offset=i,this._dismiss=n,this._queue=e?e.map(this.formatInput):[]}return e.prototype._enqueue=function(e){this._queue.push(e)},e.prototype._dequeue=function(){var e=this._queue.shift()||null;return this._last=e,e},e.prototype.enqueue=function(e){var t=this.formatInput(e);return this._enqueue(t),t.id},e.prototype.dequeue=function(){return 0===this._queue.length?null:this._dequeue()},e.prototype.getLast=function(){return this._last},e.prototype.getLength=function(){return this._queue.length},e.prototype.getSettings=function(){return{max:this._max,delay:this._delay,offset:this._offset,height:this._height,dismiss:this._dismiss}},e.prototype.formatInput=function(e){var t=a(e.id);return Object.assign({id:t,open:!0,height:this._height},e)},e._count=0,e}();function l(e,t){var n=t.queue,r=t.input,u=t.type,o=n.getSettings(),a=o.max,s=o.dismiss,c=o.delay,f=o.offset,d=null;function l(){var t=a&&e.length>=a,r=e;if(!n.getLength()||t&&!s)return null;t&&(r=e.slice(1));var u=n.dequeue();return u?function(){for(var e=0,t=0,n=arguments.length;t<n;t++)e+=arguments[t].length;var r=Array(e),u=0;for(t=0;t<n;t++)for(var i=arguments[t],o=0,a=i.length;o<a;o++,u++)r[u]=i[o];return r}(r,[u]):null}switch(u){case"enqueue":case"dequeue":"enqueue"===u&&n.enqueue(r),d=l();break;case"update":var p=e.find((function(e){return e.id===r.id})),h=(r.id,function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(r=Object.getOwnPropertySymbols(e);u<r.length;u++)t.indexOf(r[u])<0&&Object.prototype.propertyIsEnumerable.call(e,r[u])&&(n[r[u]]=e[r[u]])}return n}(r,["id"]));p&&(d=e.map((function(e){return e.id!==r.id?e:i(i({},e),h)})));break;case"exited":if(p=e.find((function(e){return e.id===r.id}))){d=e.filter((function(e){return e.id!==r.id}));var v=setTimeout((function(){d=l(),clearTimeout(v)}),c)}}return d?function(e,t){var n=3*t,r=0,u={};for(u[0]=n;e[r+1];){var o=n+e[r].height+t;u[r+1]=o,n=o,r+=1}return e.map((function(e,t){return i(i({},e),{offset:u[t]||0})}))}(d,f):e}e.ReactToastSnackContext=u,e.ReactToastSnackProvider=function(e){var n=e.max,r=e.delay,o=e.offset,s=e.height,c=e.initial,f=e.dismiss,p=e.children,h=e.renderer,v=e.methods,m=void 0===v?{}:v,y=t.useReducer(l,[]),g=y[0],_=y[1],q=t.useRef(new d(c,n,f,r,s,o)),k=t.useCallback((function(e){var t=q.current,n=a(e.id);return _({queue:t,type:"enqueue",input:i(i({},e),{id:n})}),n}),[]),b=t.useCallback((function(e){var t=q.current;_({queue:t,type:"update",input:e})}),[]),x=t.useCallback((function(e){var t=q.current;_({queue:t,type:"update",input:{id:e,open:!1}})}),[]),O=t.useCallback((function(e){var t=q.current;_({queue:t,type:"exited",input:{id:e}})}),[]),w=t.useMemo((function(){return i({create:k,update:b},Object.keys(m).reduce((function(e,t){return e[t]=m[t](k,b),e}),{}))}),[k,b,m]);return t.createElement(u.Provider,{value:w},p,g.map((function(e){return t.createElement(h,{key:e.id,toastSnack:e,onUpdate:b,onExited:O,onClose:x})})))},e.useDuration=function(e,n,r,u){var i=t.useRef(null),o=t.useCallback((function(){r&&u&&(i.current=setTimeout((function(){return u(e)}),r))}),[e,r,u]);t.useEffect((function(){return n&&o(),function(){i.current&&clearTimeout(i.current)}}),[n,o])},e.useToastSnack=function(){var e=t.useContext(u);return r(void 0!==e,"[useToastSnack]: ReactToastSnackContext is undefined"),e},e.withToastSnack=function(e){return t.forwardRef((function(n,o){return t.createElement(u.Consumer,null,(function(u){return r(void 0!==u,"[withToastSnack]: ReactToastSnackContext is undefined"),t.createElement(e,i({},n,{ref:o,toastSnack:u}))}))}))},Object.defineProperty(e,"__esModule",{value:!0})}));