bro-toastify
Version:
A framework-agnostic toast notification library
4 lines (3 loc) • 9.73 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 o=function(){return o=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},o.apply(this,arguments)};function a(e,t,n){if(n||2===arguments.length)for(var i,r=0,o=t.length;r<o;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;function l(e){u=e}var f={type:"default",duration:3e3,position:"top-right",dismissible:!0,pauseOnHover:!0,customIcon:void 0,customClass:void 0},m=new Map,p=new Map,g=function(){return Date.now().toString(36)+Math.random().toString(36).substring(2,5)};function b(e){var t=c.get(e);t&&(clearTimeout(t),c.set(e,null))}function y(e,t){var n=m.get(e);if(n&&t>0&&"loading"!==n.type){var i=setTimeout((function(){h(e)}),t);c.set(e,i)}}function v(e){var t;if(e.message){var n=e.type||"default",i=e.containerOptions||{},r=i.animation?o({},s[i.animation]):s[u],a=o(o(o({},f),e),{duration:"loading"===e.type?0:null!==(t=e.duration)&&void 0!==t?t:f.duration,animation:o(o(o({},s[n]),r),e.animation)}),d=e.id||g(),l=o(o({},a),{id:d,createdAt:Date.now()});if(m.set(d,l),E("create",l),a.duration&&a.duration>0&&"undefined"!=typeof window){var p=setTimeout((function(){h(d)}),a.duration);c.set(d,p)}return l}console.error("BroToastify: message is required")}function h(e){var t,n;if("string"==typeof e?(t=e,n=Array.from(m.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)),m.delete(t),E("dismiss",n),n.onClose&&n.onClose()}}function w(){Array.from(m.values()).forEach((function(e){h(e.id)}))}function x(e,t){return"undefined"==typeof window?{off:function(){}}:(p.has(e)||p.set(e,[]),p.get(e).push(t),{off:function(){var n=p.get(e);if(n){var i=n.indexOf(t);-1!==i&&n.splice(i,1)}}})}function E(e,t){if("undefined"!=typeof window){var n=p.get(e);n&&n.forEach((function(e){return e(t)}))}}var A=new Map;function T(e){if("undefined"==typeof window)return null;if(A.has(e)){var t=A.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),A.set(e,n),n}function S(e){if("undefined"==typeof window)return null;if(A.has(e)){var t=A.get(e);return document.body.contains(t)||document.body.appendChild(t),t}return T(e)}function k(){"undefined"!=typeof window&&(console.debug("Removing all containers"),A.forEach((function(e){document.body.contains(e)&&e.remove()})),A.clear())}var C={show:function(e,t){return v(o({message:e,type:"show"},t))},default:function(e,t){return v(o({message:e,type:"default"},t))},success:function(e,t){return v(o({message:e,type:"success"},t))},error:function(e,t){return v(o({message:e,type:"error"},t))},info:function(e,t){return v(o({message:e,type:"info"},t))},warning:function(e,t){return v(o({message:e,type:"warning"},t))},loading:function(e,t){var n=g();return v(o({id:n,message:e,type:"loading"},t)),{id:n}},promises:function(e,t,n){var i=g();return v(o({id:i,message:t.loading,type:"loading"},n)),e.then((function(e){return h(i),v(o({message:t.success,type:"success"},n)),e})).catch((function(e){throw h(i),v(o({message:t.error,type:"error"},n)),e})),{id:i}},isToastActive:function(e){return!!Array.from(m.values()).find((function(t){return t.id===e}))},dismiss:function(e){return h(e)},dismissible:function(e){return h(e)},clearAll:w},N=function(u){var c=u.position,f=void 0===c?"top-right":c,m=u.newestOnTop,p=void 0===m||m,g=u.dismissible,v=void 0!==g&&g,w=u.animation,E=void 0===w?"fade":w,A=t([]),T=A[0],S=A[1],k=t(!1),C=k[0],N=k[1],M=n(new Map),O=n(new Map);i((function(){N(!0),l(E);var e=x("create",(function(e){var t=o(o({},e),{animation:e.animation||s[E]});S((function(e){return p?a([t],e,!0):a(a([],e,!0),[t],!1)}))})),t=x("dismiss",(function(e){var t=M.current.get(e.id);if(t){d(t,e.animation||s[E],!1);var n=function(){S((function(t){return t.filter((function(t){return t.id!==e.id}))})),M.current.delete(e.id),O.current.delete(e.id)};t.addEventListener("animationend",n,{once:!0}),setTimeout(n,1e3)}else S((function(t){return t.filter((function(t){return t.id!==e.id}))})),O.current.delete(e.id)}));return function(){e.off(),t.off()}}),[p,E]),i((function(){T.forEach((function(e){var t=M.current.get(e.id);t&&!t.dataset.animated&&(d(t,e.animation||s[E],!0),t.dataset.animated="true")}))}),[T,E]);if(!C)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)},T.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?M.current.set(t.id,e):M.current.delete(t.id)},onMouseEnter:function(){b(t.id)},onMouseLeave:function(){return function(e){var t=T.find((function(t){return t.id===e}));t&&t.duration&&t.duration>0&&"loading"!==t.type&&y(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),v&&"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 h(t.id)}},"×"))}))),document.body)};N.displayName="Toaster";var M=Object.freeze({__proto__:null,Toaster:N,broToastify:function(){return{show:function(e,t){return C.show(e,t)},success:function(e,t){return C.success(e,t)},error:function(e,t){return C.error(e,t)},info:function(e,t){return C.info(e,t)},warning:function(e,t){return C.warning(e,t)},loading:function(e,t){return C.loading(e,t)},promises:function(e,t,n){return C.promises(e,t,n)},isToastActive:function(e){return C.isToastActive(e)},dismiss:function(e){return C.dismiss(e)},clearAll:function(){return C.clearAll()}}}});export{M as React,N as Toaster,w as clearBroToastify,v as createBroToastify,T as createContainer,C as default,h as dismissBroToastify,S as getContainer,x as on,b as pauseTimer,k as removeAllContainers,y as resumeTimer,l as setToasterAnimation};
//# sourceMappingURL=index.esm.js.map