UNPKG

bro-toastify

Version:

A framework-agnostic toast notification library

4 lines (3 loc) 10.2 kB
'use client'; !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("react-dom")):"function"==typeof define&&define.amd?define(["exports","react","react-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).BroToastify={},e.React,e.ReactDOM)}(this,(function(e,t,n){"use strict";function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=i(t),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 u(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 d="fade",f=new Map;function c(e){d=e}var l={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 y(e){var t=f.get(e);t&&(clearTimeout(t),f.set(e,null))}function b(e,t){var n=m.get(e);if(n&&t>0&&"loading"!==n.type){var i=setTimeout((function(){h(e)}),t);f.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[d],a=o(o(o({},l),e),{duration:"loading"===e.type?0:null!==(t=e.duration)&&void 0!==t?t:l.duration,animation:o(o(o({},s[n]),r),e.animation)}),u=e.id||g(),c=o(o({},a),{id:u,createdAt:Date.now()});if(m.set(u,c),T("create",c),a.duration&&a.duration>0&&"undefined"!=typeof window){var p=setTimeout((function(){h(u)}),a.duration);f.set(u,p)}return c}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=f.get(t);i&&(clearTimeout(i),f.delete(t)),m.delete(t),T("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 T(e,t){if("undefined"!=typeof window){var n=p.get(e);n&&n.forEach((function(e){return e(t)}))}}var E=new Map;function A(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 S={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},C=function(e){var i=e.position,d=void 0===i?"top-right":i,f=e.newestOnTop,l=void 0===f||f,m=e.dismissible,p=void 0!==m&&m,g=e.animation,v=void 0===g?"fade":g,w=t.useState([]),T=w[0],E=w[1],A=t.useState(!1),S=A[0],C=A[1],M=t.useRef(new Map),k=t.useRef(new Map);t.useEffect((function(){C(!0),c(v);var e=x("create",(function(e){var t=o(o({},e),{animation:e.animation||s[v]});E((function(e){return l?a([t],e,!0):a(a([],e,!0),[t],!1)}))})),t=x("dismiss",(function(e){var t=M.current.get(e.id);if(t){u(t,e.animation||s[v],!1);var n=function(){E((function(t){return t.filter((function(t){return t.id!==e.id}))})),M.current.delete(e.id),k.current.delete(e.id)};t.addEventListener("animationend",n,{once:!0}),setTimeout(n,1e3)}else E((function(t){return t.filter((function(t){return t.id!==e.id}))})),k.current.delete(e.id)}));return function(){e.off(),t.off()}}),[l,v]),t.useEffect((function(){T.forEach((function(e){var t=M.current.get(e.id);t&&!t.dataset.animated&&(u(t,e.animation||s[v],!0),t.dataset.animated="true")}))}),[T,v]);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 n.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)},T.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?M.current.set(e.id,t):M.current.delete(e.id)},onMouseEnter:function(){y(e.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&&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 h(e.id)}},"×"))}))),document.body)};C.displayName="Toaster";var M=Object.freeze({__proto__:null,Toaster:C,broToastify:function(){return{show:function(e,t){return S.show(e,t)},success:function(e,t){return S.success(e,t)},error:function(e,t){return S.error(e,t)},info:function(e,t){return S.info(e,t)},warning:function(e,t){return S.warning(e,t)},loading:function(e,t){return S.loading(e,t)},promises:function(e,t,n){return S.promises(e,t,n)},isToastActive:function(e){return S.isToastActive(e)},dismiss:function(e){return S.dismiss(e)},clearAll:function(){return S.clearAll()}}}});e.React=M,e.Toaster=C,e.clearBroToastify=w,e.createBroToastify=v,e.createContainer=A,e.default=S,e.dismissBroToastify=h,e.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 A(e)},e.on=x,e.pauseTimer=y,e.removeAllContainers=function(){"undefined"!=typeof window&&(console.debug("Removing all containers"),E.forEach((function(e){document.body.contains(e)&&e.remove()})),E.clear())},e.resumeTimer=b,e.setToasterAnimation=c,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=index.umd.js.map