bro-toastify
Version:
A framework-agnostic toast notification library
4 lines (3 loc) • 8.36 kB
JavaScript
'use client';
import e,{useState as t,useRef as n,useEffect as i}from"react";import{createPortal as r}from"react-dom";var a=function(){return a=Object.assign||function(e){for(var t,n=1,i=arguments.length;n<i;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e},a.apply(this,arguments)};function o(e,t,n){if(n||2===arguments.length)for(var i,r=0,a=t.length;r<a;r++)!i&&r in t||(i||(i=Array.prototype.slice.call(t,0,r)),i[r]=t[r]);return e.concat(i||Array.prototype.slice.call(t))}"function"==typeof SuppressedError&&SuppressedError;var s={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 d(e,t,n){void 0===t&&(t=s.default),void 0===n&&(n=!0);var i=t.type;if("none"!==i){var r={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:""}[i];r&&(e.classList.add(r),e.addEventListener("animationend",(function(){e.classList.remove(r)}),{once:!0}))}}var u="fade",c=new Map;var l={type:"default",duration:3e3,position:"top-right",dismissible:!0,pauseOnHover:!0,customIcon:void 0,customClass:void 0},f=new Map,m=new Map,p=function(){return Date.now().toString(36)+Math.random().toString(36).substring(2,5)};function g(e){var t=c.get(e);t&&(clearTimeout(t),c.set(e,null))}function v(e,t){var n=f.get(e);if(n&&t>0&&"loading"!==n.type){var i=setTimeout((function(){b(e)}),t);c.set(e,i)}}function y(e){var t;if(e.message){var n=e.type||"default",i=e.containerOptions||{},r=i.animation?a({},s[i.animation]):s[u],o=a(a(a({},l),e),{duration:"loading"===e.type?0:null!==(t=e.duration)&&void 0!==t?t:l.duration,animation:a(a(a({},s[n]),r),e.animation)}),d=e.id||p(),m=a(a({},o),{id:d,createdAt:Date.now()});if(f.set(d,m),w("create",m),o.duration&&o.duration>0&&"undefined"!=typeof window){var g=setTimeout((function(){b(d)}),o.duration);c.set(d,g)}return m}console.error("BroToastify: message is required")}function b(e){var t,n;if("string"==typeof e?(t=e,n=Array.from(f.values()).find((function(e){return e.id===t}))):(t=e.id,n=e),n){var i=c.get(t);i&&(clearTimeout(i),c.delete(t)),f.delete(t),w("dismiss",n),n.onClose&&n.onClose()}}function h(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 i=n.indexOf(t);-1!==i&&n.splice(i,1)}}})}function w(e,t){if("undefined"!=typeof window){var n=m.get(e);n&&n.forEach((function(e){return e(t)}))}}var x={show:function(e,t){return y(a({message:e,type:"show"},t))},default:function(e,t){return y(a({message:e,type:"default"},t))},success:function(e,t){return y(a({message:e,type:"success"},t))},error:function(e,t){return y(a({message:e,type:"error"},t))},info:function(e,t){return y(a({message:e,type:"info"},t))},warning:function(e,t){return y(a({message:e,type:"warning"},t))},loading:function(e,t){var n=p();return y(a({id:n,message:e,type:"loading"},t)),{id:n}},promises:function(e,t,n){var i=p();return y(a({id:i,message:t.loading,type:"loading"},n)),e.then((function(e){return b(i),y(a({message:t.success,type:"success"},n)),e})).catch((function(e){throw b(i),y(a({message:t.error,type:"error"},n)),e})),{id:i}},isToastActive:function(e){return!!Array.from(f.values()).find((function(t){return t.id===e}))},dismiss:function(e){return b(e)},dismissible:function(e){return b(e)},clearAll:function(){Array.from(f.values()).forEach((function(e){b(e.id)}))}},E=function(c){var l=c.position,f=void 0===l?"top-right":l,m=c.newestOnTop,p=void 0===m||m,y=c.dismissible,w=void 0!==y&&y,x=c.animation,E=void 0===x?"fade":x,A=t([]),S=A[0],T=A[1],k=t(!1),N=k[0],M=k[1],O=n(new Map),C=n(new Map);i((function(){M(!0),function(e){u=e}(E);var e=h("create",(function(e){var t=a(a({},e),{animation:e.animation||s[E]});T((function(e){return p?o([t],e,!0):o(o([],e,!0),[t],!1)}))})),t=h("dismiss",(function(e){var t=O.current.get(e.id);if(t){d(t,e.animation||s[E],!1);var n=function(){T((function(t){return t.filter((function(t){return t.id!==e.id}))})),O.current.delete(e.id),C.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}))})),C.current.delete(e.id)}));return function(){e.off(),t.off()}}),[p,E]),i((function(){S.forEach((function(e){var t=O.current.get(e.id);t&&!t.dataset.animated&&(d(t,e.animation||s[E],!0),t.dataset.animated="true")}))}),[S,E]);if(!N)return null;var z="top-right"===f?"top-4 right-4 items-end":"top-left"===f?"top-4 left-4 items-start":"bottom-right"===f?"bottom-4 right-4 items-end":"bottom-left"===f?"bottom-4 left-4 items-start":"top-center"===f?"top-4 left-1/2 -translate-x-1/2 items-center":"bottom-4 left-1/2 -translate-x-1/2 items-center";return r(e.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(z)},S.map((function(t){var n,i=null!==(n=t.type)&&void 0!==n?n:"default",r="default"===i?"bg-white/80 backdrop-blur-md border border-gray-200 text-gray-900":"success"===i?"bg-green-500/90 backdrop-blur-md border border-green-600 text-white":"error"===i?"bg-red-500/90 backdrop-blur-md border border-red-600 text-white":"info"===i?"bg-blue-500/90 backdrop-blur-md border border-blue-600 text-white":"warning"===i?"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 e.createElement("div",{key:t.id,id:"toast-".concat(t.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(r," ").concat(t.customClass||""),ref:function(e){e?O.current.set(t.id,e):O.current.delete(t.id)},onMouseEnter:function(){g(t.id)},onMouseLeave:function(){return function(e){var t=S.find((function(t){return t.id===e}));t&&t.duration&&t.duration>0&&"loading"!==t.type&&v(e,t.duration)}(t.id)}},t.title&&e.createElement("div",{className:"font-semibold text-base mb-1.5 tracking-tight"},t.title),"loading"===t.type?e.createElement("div",{className:"flex items-center gap-3"},e.createElement("div",{className:"border-4 border-gray-200 border-t-blue-500 rounded-full w-5 h-5 animate-spin"}),e.createElement("div",{className:"text-sm text-gray-900 animate-[fade_0.3s_ease-in-out]"},t.message)):e.createElement("div",{className:"text-sm tracking-tight"},t.message),w&&"loading"!==t.type&&"promises"!==t.type&&e.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 b(t.id)}},"×"))}))),document.body)};E.displayName="Toaster";var A=function(){return{show:function(e,t){return x.show(e,t)},success:function(e,t){return x.success(e,t)},error:function(e,t){return x.error(e,t)},info:function(e,t){return x.info(e,t)},warning:function(e,t){return x.warning(e,t)},loading:function(e,t){return x.loading(e,t)},promises:function(e,t,n){return x.promises(e,t,n)},isToastActive:function(e){return x.isToastActive(e)},dismiss:function(e){return x.dismiss(e)},clearAll:function(){return x.clearAll()}}};export{E as Toaster,A as broToastify};
//# sourceMappingURL=react.esm.js.map