react-toastsnack
Version:
An unopinionated notification queue for React
24 lines (21 loc) • 6.54 kB
JavaScript
/**
* 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 _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var React=_interopDefault(require("react"));function useDuration(e,t,n,r){var a=React.useRef(null),i=React.useCallback((function(){n&&r&&(a.current=setTimeout((function(){return r(e)}),n))}),[e,n,r]);React.useEffect((function(){return t&&i(),function(){a.current&&clearTimeout(a.current)}}),[t,i])}var NODE_ENV=process.env.NODE_ENV,invariant=function(e,t,n,r,a,i,u,o){if("production"!==NODE_ENV&&void 0===t)throw new Error("invariant requires an error message argument");if(!e){var s;if(void 0===t)s=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var c=[n,r,a,i,u,o],f=0;(s=new Error(t.replace(/%s/g,(function(){return c[f++]})))).name="Invariant Violation"}throw s.framesToPop=1,s}},invariant_1=invariant,ReactToastSnackContext=React.createContext(void 0);function useToastSnack(){var e=React.useContext(ReactToastSnackContext);return invariant_1(void 0!==e,"[useToastSnack]: ReactToastSnackContext is undefined"),e}
/*! *****************************************************************************
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 __assign=function(){return(__assign=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var a in t=arguments[n])Object.prototype.hasOwnProperty.call(t,a)&&(e[a]=t[a]);return e}).apply(this,arguments)};function __rest(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 a=0;for(r=Object.getOwnPropertySymbols(e);a<r.length;a++)t.indexOf(r[a])<0&&Object.prototype.propertyIsEnumerable.call(e,r[a])&&(n[r[a]]=e[r[a]])}return n}function __spreadArrays(){for(var e=0,t=0,n=arguments.length;t<n;t++)e+=arguments[t].length;var r=Array(e),a=0;for(t=0;t<n;t++)for(var i=arguments[t],u=0,o=i.length;u<o;u++,a++)r[a]=i[u];return r}function withToastSnack(e){return React.forwardRef((function(t,n){return React.createElement(ReactToastSnackContext.Consumer,null,(function(r){return invariant_1(void 0!==r,"[withToastSnack]: ReactToastSnackContext is undefined"),React.createElement(e,__assign({},t,{ref:n,toastSnack:r}))}))}))}var count=0;function nextId(e){return"string"==typeof e?e:"ReactToastSnack:"+count++}var DEFAULT_DELAY=400,DEFAULT_OFFSET=8,DEFAULT_HEIGHT=48,ReactToastSnackQueue=function(){function e(e,t,n,r,a,i){void 0===e&&(e=[]),void 0===n&&(n=!1),void 0===r&&(r=DEFAULT_DELAY),void 0===a&&(a=DEFAULT_HEIGHT),void 0===i&&(i=DEFAULT_OFFSET),this._max=t,this._last=null,this._delay=r,this._height=a,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=nextId(e.id);return Object.assign({id:t,open:!0,height:this._height},e)},e._count=0,e}();function offsets(e,t){var n=3*t,r=0,a={};for(a[0]=n;e[r+1];){var i=n+e[r].height+t;a[r+1]=i,n=i,r+=1}return e.map((function(e,t){return __assign(__assign({},e),{offset:a[t]||0})}))}function ReactToastSnackReducer(e,t){var n=t.queue,r=t.input,a=t.type,i=n.getSettings(),u=i.max,o=i.dismiss,s=i.delay,c=i.offset,f=null;function d(){var t=u&&e.length>=u,r=e;if(!n.getLength()||t&&!o)return null;t&&(r=e.slice(1));var a=n.dequeue();return a?__spreadArrays(r,[a]):null}switch(a){case"enqueue":case"dequeue":"enqueue"===a&&n.enqueue(r),f=d();break;case"update":var l=e.find((function(e){return e.id===r.id})),p=(r.id,__rest(r,["id"]));l&&(f=e.map((function(e){return e.id!==r.id?e:__assign(__assign({},e),p)})));break;case"exited":if(l=e.find((function(e){return e.id===r.id}))){f=e.filter((function(e){return e.id!==r.id}));var _=setTimeout((function(){f=d(),clearTimeout(_)}),s)}}return f?offsets(f,c):e}var ReactToastSnackProvider=function(e){var t=e.max,n=e.delay,r=e.offset,a=e.height,i=e.initial,u=e.dismiss,o=e.children,s=e.renderer,c=e.methods,f=void 0===c?{}:c,d=React.useReducer(ReactToastSnackReducer,[]),l=d[0],p=d[1],_=React.useRef(new ReactToastSnackQueue(i,t,u,n,a,r)),h=React.useCallback((function(e){var t=_.current,n=nextId(e.id);return p({queue:t,type:"enqueue",input:__assign(__assign({},e),{id:n})}),n}),[]),v=React.useCallback((function(e){var t=_.current;p({queue:t,type:"update",input:e})}),[]),g=React.useCallback((function(e){var t=_.current;p({queue:t,type:"update",input:{id:e,open:!1}})}),[]),R=React.useCallback((function(e){var t=_.current;p({queue:t,type:"exited",input:{id:e}})}),[]),T=React.useMemo((function(){return __assign({create:h,update:v},Object.keys(f).reduce((function(e,t){return e[t]=f[t](h,v),e}),{}))}),[h,v,f]);return React.createElement(ReactToastSnackContext.Provider,{value:T},o,l.map((function(e){return React.createElement(s,{key:e.id,toastSnack:e,onUpdate:v,onExited:R,onClose:g})})))};exports.ReactToastSnackContext=ReactToastSnackContext,exports.ReactToastSnackProvider=ReactToastSnackProvider,exports.useDuration=useDuration,exports.useToastSnack=useToastSnack,exports.withToastSnack=withToastSnack;