UNPKG

react-toastsnack

Version:

An unopinionated notification queue for React

24 lines (21 loc) 5.69 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. */ import e from"react";function t(t,n,r,u){var i=e.useRef(null),o=e.useCallback((function(){r&&u&&(i.current=setTimeout((function(){return u(t)}),r))}),[t,r,u]);e.useEffect((function(){return n&&o(),function(){i.current&&clearTimeout(i.current)}}),[n,o])}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=e.createContext(void 0);function i(){var t=e.useContext(u);return r(void 0!==t,"[useToastSnack]: ReactToastSnackContext is undefined"),t} /*! ***************************************************************************** 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 o=function(){return(o=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)};function a(t){return e.forwardRef((function(n,i){return e.createElement(u.Consumer,null,(function(u){return r(void 0!==u,"[withToastSnack]: ReactToastSnackContext is undefined"),e.createElement(t,o({},n,{ref:i,toastSnack:u}))}))}))}var s=0;function c(e){return"string"==typeof e?e:"ReactToastSnack:"+s++}var f=400,d=8,l=48,p=function(){function e(e,t,n,r,u,i){void 0===e&&(e=[]),void 0===n&&(n=!1),void 0===r&&(r=f),void 0===u&&(u=l),void 0===i&&(i=d),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=c(e.id);return Object.assign({id:t,open:!0,height:this._height},e)},e._count=0,e}();function h(e,t){var n=t.queue,r=t.input,u=t.type,i=n.getSettings(),a=i.max,s=i.dismiss,c=i.delay,f=i.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:o(o({},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 i=n+e[r].height+t;u[r+1]=i,n=i,r+=1}return e.map((function(e,t){return o(o({},e),{offset:u[t]||0})}))}(d,f):e}var v=function(t){var n=t.max,r=t.delay,i=t.offset,a=t.height,s=t.initial,f=t.dismiss,d=t.children,l=t.renderer,v=t.methods,m=void 0===v?{}:v,g=e.useReducer(h,[]),y=g[0],_=g[1],q=e.useRef(new p(s,n,f,r,a,i)),b=e.useCallback((function(e){var t=q.current,n=c(e.id);return _({queue:t,type:"enqueue",input:o(o({},e),{id:n})}),n}),[]),k=e.useCallback((function(e){var t=q.current;_({queue:t,type:"update",input:e})}),[]),x=e.useCallback((function(e){var t=q.current;_({queue:t,type:"update",input:{id:e,open:!1}})}),[]),O=e.useCallback((function(e){var t=q.current;_({queue:t,type:"exited",input:{id:e}})}),[]),w=e.useMemo((function(){return o({create:b,update:k},Object.keys(m).reduce((function(e,t){return e[t]=m[t](b,k),e}),{}))}),[b,k,m]);return e.createElement(u.Provider,{value:w},d,y.map((function(t){return e.createElement(l,{key:t.id,toastSnack:t,onUpdate:k,onExited:O,onClose:x})})))};export{u as ReactToastSnackContext,v as ReactToastSnackProvider,t as useDuration,i as useToastSnack,a as withToastSnack};