bro-toastify
Version:
A framework-agnostic toast notification library
4 lines (3 loc) • 9.99 kB
JavaScript
'use client';
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),t=require("react-dom");function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(e),i=function(){return i=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var i in t=arguments[n])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},i.apply(this,arguments)};function o(e,t,n){if(n||2===arguments.length)for(var r,i=0,o=t.length;i<o;i++)!r&&i in t||(r||(r=Array.prototype.slice.call(t,0,i)),r[i]=t[i]);return e.concat(r||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var a={default:{type:"fade",duration:300,easing:"ease"},success:{type:"fade",duration:300,easing:"ease"},error:{type:"fade",duration:300,easing:"ease"},info:{type:"fade",duration:300,easing:"ease"},warning:{type:"fade",duration:500,easing:"ease"},loading:{type:"fade",duration:300,easing:"ease"},show:{type:"fade",duration:300,easing:"ease"},promises:{type:"fade",duration:300,easing:"ease"},fade:{type:"fade",duration:300,easing:"ease"},"top-slide":{type:"top-slide",duration:300,easing:"ease"},"right-slide":{type:"right-slide",duration:300,easing:"ease"},"bottom-slide":{type:"bottom-slide",duration:300,easing:"ease"},"left-slide":{type:"left-slide",duration:300,easing:"ease"},zoom:{type:"zoom",duration:300,easing:"ease"},flip:{type:"flip",duration:300,easing:"ease"},bounce:{type:"bounce",duration:300,easing:"ease"},none:{type:"none",duration:0,easing:"ease"}};function s(e,t,n){void 0===t&&(t=a.default),void 0===n&&(n=!0);var r=t.type;if("none"!==r){var i={fade:"animate-fade","top-slide":n?"animate-topSlide":"animate-topSlideReverse","right-slide":n?"animate-rightSlide":"animate-rightSlideReverse","bottom-slide":n?"animate-bottomSlide":"animate-bottomSlideReverse","left-slide":n?"animate-leftSlide":"animate-leftSlideReverse",zoom:"animate-zoom",flip:"animate-flip",bounce:"animate-bounce",none:""}[r];i&&(e.classList.add(i),e.addEventListener("animationend",(function(){e.classList.remove(i)}),{once:!0}))}}var u="fade",d=new Map;function c(e){u=e}var f={type:"default",duration:3e3,position:"top-right",dismissible:!0,pauseOnHover:!0,customIcon:void 0,customClass:void 0},l=new Map,m=new Map,p=function(){return Date.now().toString(36)+Math.random().toString(36).substring(2,5)};function g(e){var t=d.get(e);t&&(clearTimeout(t),d.set(e,null))}function b(e,t){var n=l.get(e);if(n&&t>0&&"loading"!==n.type){var r=setTimeout((function(){v(e)}),t);d.set(e,r)}}function y(e){var t;if(e.message){var n=e.type||"default",r=e.containerOptions||{},o=r.animation?i({},a[r.animation]):a[u],s=i(i(i({},f),e),{duration:"loading"===e.type?0:null!==(t=e.duration)&&void 0!==t?t:f.duration,animation:i(i(i({},a[n]),o),e.animation)}),c=e.id||p(),m=i(i({},s),{id:c,createdAt:Date.now()});if(l.set(c,m),x("create",m),s.duration&&s.duration>0&&"undefined"!=typeof window){var g=setTimeout((function(){v(c)}),s.duration);d.set(c,g)}return m}console.error("BroToastify: message is required")}function v(e){var t,n;if("string"==typeof e?(t=e,n=Array.from(l.values()).find((function(e){return e.id===t}))):(t=e.id,n=e),n){var r=d.get(t);r&&(clearTimeout(r),d.delete(t)),l.delete(t),x("dismiss",n),n.onClose&&n.onClose()}}function h(){Array.from(l.values()).forEach((function(e){v(e.id)}))}function w(e,t){return"undefined"==typeof window?{off:function(){}}:(m.has(e)||m.set(e,[]),m.get(e).push(t),{off:function(){var n=m.get(e);if(n){var r=n.indexOf(t);-1!==r&&n.splice(r,1)}}})}function x(e,t){if("undefined"!=typeof window){var n=m.get(e);n&&n.forEach((function(e){return e(t)}))}}var E=new Map;function T(e){if("undefined"==typeof window)return null;if(E.has(e)){var t=E.get(e);return document.body.contains(t)||document.body.appendChild(t),t}console.debug("Creating container for position:",e);var n=document.createElement("div");return n.className="bro-toastify-container ".concat(function(e){return{"top-right":"top-4 right-4 items-end","top-left":"top-4 left-4 items-start","top-center":"top-4 left-1/2 -translate-x-1/2 items-center","bottom-right":"bottom-4 right-4 items-end","bottom-left":"bottom-4 left-4 items-start","bottom-center":"bottom-4 left-1/2 -translate-x-1/2 items-center"}[e||"top-right"]}(e)),n.setAttribute("role","region"),n.setAttribute("aria-live","polite"),n.setAttribute("aria-atomic","true"),document.body.appendChild(n),E.set(e,n),n}var A={show:function(e,t){return y(i({message:e,type:"show"},t))},default:function(e,t){return y(i({message:e,type:"default"},t))},success:function(e,t){return y(i({message:e,type:"success"},t))},error:function(e,t){return y(i({message:e,type:"error"},t))},info:function(e,t){return y(i({message:e,type:"info"},t))},warning:function(e,t){return y(i({message:e,type:"warning"},t))},loading:function(e,t){var n=p();return y(i({id:n,message:e,type:"loading"},t)),{id:n}},promises:function(e,t,n){var r=p();return y(i({id:r,message:t.loading,type:"loading"},n)),e.then((function(e){return v(r),y(i({message:t.success,type:"success"},n)),e})).catch((function(e){throw v(r),y(i({message:t.error,type:"error"},n)),e})),{id:r}},isToastActive:function(e){return!!Array.from(l.values()).find((function(t){return t.id===e}))},dismiss:function(e){return v(e)},dismissible:function(e){return v(e)},clearAll:h},S=function(n){var u=n.position,d=void 0===u?"top-right":u,f=n.newestOnTop,l=void 0===f||f,m=n.dismissible,p=void 0!==m&&m,y=n.animation,h=void 0===y?"fade":y,x=e.useState([]),E=x[0],T=x[1],A=e.useState(!1),S=A[0],C=A[1],k=e.useRef(new Map),M=e.useRef(new Map);e.useEffect((function(){C(!0),c(h);var e=w("create",(function(e){var t=i(i({},e),{animation:e.animation||a[h]});T((function(e){return l?o([t],e,!0):o(o([],e,!0),[t],!1)}))})),t=w("dismiss",(function(e){var t=k.current.get(e.id);if(t){s(t,e.animation||a[h],!1);var n=function(){T((function(t){return t.filter((function(t){return t.id!==e.id}))})),k.current.delete(e.id),M.current.delete(e.id)};t.addEventListener("animationend",n,{once:!0}),setTimeout(n,1e3)}else T((function(t){return t.filter((function(t){return t.id!==e.id}))})),M.current.delete(e.id)}));return function(){e.off(),t.off()}}),[l,h]),e.useEffect((function(){E.forEach((function(e){var t=k.current.get(e.id);t&&!t.dataset.animated&&(s(t,e.animation||a[h],!0),t.dataset.animated="true")}))}),[E,h]);if(!S)return null;var N="top-right"===d?"top-4 right-4 items-end":"top-left"===d?"top-4 left-4 items-start":"bottom-right"===d?"bottom-4 right-4 items-end":"bottom-left"===d?"bottom-4 left-4 items-start":"top-center"===d?"top-4 left-1/2 -translate-x-1/2 items-center":"bottom-4 left-1/2 -translate-x-1/2 items-center";return t.createPortal(r.default.createElement("div",{className:"bro-toastify-container fixed z-[9999] flex flex-col gap-3 max-w-full max-h-[100vh] overflow-y-auto p-4 ".concat(N)},E.map((function(e){var t,n=null!==(t=e.type)&&void 0!==t?t:"default",i="default"===n?"bg-white/80 backdrop-blur-md border border-gray-200 text-gray-900":"success"===n?"bg-green-500/90 backdrop-blur-md border border-green-600 text-white":"error"===n?"bg-red-500/90 backdrop-blur-md border border-red-600 text-white":"info"===n?"bg-blue-500/90 backdrop-blur-md border border-blue-600 text-white":"warning"===n?"bg-yellow-500/90 backdrop-blur-md border border-yellow-600 text-white":"bg-white/80 backdrop-blur-md border border-gray-200 text-gray-900";return r.default.createElement("div",{key:e.id,id:"toast-".concat(e.id),className:"relative min-w-[300px] max-w-[500px] p-4 rounded-xl shadow-md flex flex-col opacity-100 transition-all duration-300 hover:scale-105 hover:shadow-lg ".concat(i," ").concat(e.customClass||""),ref:function(t){t?k.current.set(e.id,t):k.current.delete(e.id)},onMouseEnter:function(){g(e.id)},onMouseLeave:function(){return function(e){var t=E.find((function(t){return t.id===e}));t&&t.duration&&t.duration>0&&"loading"!==t.type&&b(e,t.duration)}(e.id)}},e.title&&r.default.createElement("div",{className:"font-semibold text-base mb-1.5 tracking-tight"},e.title),"loading"===e.type?r.default.createElement("div",{className:"flex items-center gap-3"},r.default.createElement("div",{className:"border-4 border-gray-200 border-t-blue-500 rounded-full w-5 h-5 animate-spin"}),r.default.createElement("div",{className:"text-sm text-gray-900 animate-[fade_0.3s_ease-in-out]"},e.message)):r.default.createElement("div",{className:"text-sm tracking-tight"},e.message),p&&"loading"!==e.type&&"promises"!==e.type&&r.default.createElement("button",{className:"absolute top-2 right-2 w-6 h-6 flex items-center justify-center rounded-full bg-gray-200/50 text-gray-600 text-sm font-semibold hover:bg-gray-300/70 hover:text-gray-900 transition-all duration-200","aria-label":"Close",onClick:function(){return v(e.id)}},"×"))}))),document.body)};S.displayName="Toaster";var C=Object.freeze({__proto__:null,Toaster:S,broToastify:function(){return{show:function(e,t){return A.show(e,t)},success:function(e,t){return A.success(e,t)},error:function(e,t){return A.error(e,t)},info:function(e,t){return A.info(e,t)},warning:function(e,t){return A.warning(e,t)},loading:function(e,t){return A.loading(e,t)},promises:function(e,t,n){return A.promises(e,t,n)},isToastActive:function(e){return A.isToastActive(e)},dismiss:function(e){return A.dismiss(e)},clearAll:function(){return A.clearAll()}}}});exports.React=C,exports.Toaster=S,exports.clearBroToastify=h,exports.createBroToastify=y,exports.createContainer=T,exports.default=A,exports.dismissBroToastify=v,exports.getContainer=function(e){if("undefined"==typeof window)return null;if(E.has(e)){var t=E.get(e);return document.body.contains(t)||document.body.appendChild(t),t}return T(e)},exports.on=w,exports.pauseTimer=g,exports.removeAllContainers=function(){"undefined"!=typeof window&&(console.debug("Removing all containers"),E.forEach((function(e){document.body.contains(e)&&e.remove()})),E.clear())},exports.resumeTimer=b,exports.setToasterAnimation=c;
//# sourceMappingURL=index.cjs.js.map