customizable-toast-notification
Version:
A lightweight, zero-dependency toast notification library for modern JavaScript applications with production-grade reliability and comprehensive system mechanisms
14 lines (13 loc) • 9.03 kB
JavaScript
/*!
* Customizable Toast Notifications 3.4.0
* Build: 2025-07-19 18:22:36
* Cache-Buster: 1752949356123
* Author: Priyanshu Patel
* Email: priyanshu.alt191@gmail.com
* License: Apache-2.0
* Dependencies: None
* Created: July 31, 2024
* A lightweight toast notification library for any JavaScript framework
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).customizableToast={})}(this,function(t){"use strict";function e(t="toast"){return`${t}-${Date.now()}-${Math.floor(1e5*Math.random())}`}function n(t){if(!t)return!0;try{if(t.parentNode)return t.parentNode.removeChild(t),!0}catch(e){try{if(t.remove)return t.remove(),!0}catch(e){try{return t.style.display="none",t.style.opacity="0",t.style.pointerEvents="none",t.style.position="absolute",t.style.left="-9999px",!0}catch(t){return!1}}}return!0}function o(t){try{let e=document.getElementById(`toast-container-${t}`);return e||(e=document.createElement("div"),e.id=`toast-container-${t}`,e.style.position="fixed",e.style.zIndex="9999",function(t,e){t.style.top="auto",t.style.bottom="auto",t.style.left="auto",t.style.right="auto",t.style.transform="none",e.includes("bottom")?t.style.bottom="10px":e.includes("top")&&(t.style.top="10px"),e.includes("right")?t.style.right="10px":e.includes("left")?t.style.left="10px":e.includes("center")&&(t.style.top="50%",t.style.left="50%",t.style.transform="translate(-50%, -50%)")}(e,t),function(t,e){if(!t||!e)return!1;try{return t.contains(e)||t.appendChild(e),!0}catch(n){try{return t.insertAdjacentElement("beforeend",e),!0}catch(n){try{if(t===document.body)return e.style.position="fixed",e.style.top="10px",e.style.right="10px",e.style.zIndex="99999",document.body.insertAdjacentHTML("beforeend",e.outerHTML||""),!0}catch(t){return!1}}}}(document.body,e)),e}catch(t){return document.body}}function r(t,e,n){const o=function(t){try{if("number"==typeof t&&t>0)return t;if("string"==typeof t){if(t.endsWith("s")&&!t.endsWith("ms")){const e=1e3*parseFloat(t);return e>0?e:500}const e=parseFloat(t);return e>0?e:500}}catch(t){}return 500}(e.animationDuration||"0.5s");Object.assign(t.style,{background:e.backgroundColor||e.color||"#333",color:e.textColor||"white",padding:"12px 20px",marginBottom:"10px",borderRadius:"5px",display:"flex",alignItems:"center",justifyContent:"space-between",boxShadow:"0 4px 6px rgba(0, 0, 0, 0.1)",minWidth:"250px",maxWidth:"400px",opacity:"0",position:"relative",transition:`opacity ${e.animationDuration||"0.5s"} ${e.animationEasing||"ease"}, transform ${e.animationDuration||"0.5s"} ${e.animationEasing||"ease"}`,transform:"translateY(20px)",zIndex:"9999"}),t._animationDuration=o;const r=document.createElement("span");r.textContent=e.message||"Notification",r.style.flex="1",t.appendChild(r),e.showCloseButton&&function(t,e,n){const o=document.createElement("button");o.innerHTML="×",Object.assign(o.style,{background:"none",border:"none",color:e.textColor||"white",fontSize:"18px",marginLeft:"10px",cursor:"pointer"}),o.onclick=()=>n(t),t.appendChild(o),t._cleanupCloseButton=()=>{o.onclick=null}}(t,e,n),e.showProgressBar&&function(t,e){const n=document.createElement("div");Object.assign(n.style,{position:"absolute",left:"0",height:e.progressHeight||"4px",background:e.progressColor||"rgba(255, 255, 255, 0.3)",width:"100%",transition:`width ${e.duration||3e3}ms linear`,["top"===e.progressPosition?"top":"bottom"]:"0"}),t.appendChild(n),setTimeout(()=>{!function(t){try{return t?.offsetWidth||0}catch(t){return 0}}(n),n.style.width="0%"},100)}(t,e),function(t){setTimeout(()=>{t.style.opacity="1",t.style.transform="translateY(0)"},50)}(t)}function i(t,n){let o,i=!1;try{o=function(t,n){try{const o=document.createElement(t);return o.id=e(n),o}catch(o){try{const o=document.createElement("div");return o.id=e(n||"fallback"),o.setAttribute("data-original-tag",t),o}catch(t){return{id:e(n||"emergency"),style:{},appendChild:()=>{},setAttribute:()=>{},innerHTML:"",textContent:"",onclick:null,_isEmergencyElement:!0}}}}("div","toast"),o._isEmergencyElement&&(i=!0)}catch(t){i=!0,o={id:`emergency-toast-${Date.now()}`,style:{},appendChild:()=>{},innerHTML:"",textContent:"",_isEmergencyElement:!0}}if(!i)try{return r(o,t,n),o}catch(t){i=!0}if(i&&!o._isEmergencyElement)try{return function(t,e,n){Object.assign(t.style,{background:"#333",color:"white",padding:"10px 15px",margin:"10px",borderRadius:"3px",position:"fixed",top:"20px",right:"20px",zIndex:"99999",maxWidth:"300px",wordWrap:"break-word"}),t.textContent=e.message||"Notification",e.showCloseButton&&(t.style.cursor="pointer",t.onclick=()=>n(t)),t._animationDuration=500}(o,t,n),o}catch(t){}return function(t,e){try{const n=document.createElement("div");return n.id=`emergency-${Date.now()}`,n.innerHTML=`\n <div style="\n background: #333 !important;\n color: white !important;\n padding: 10px 15px !important;\n position: fixed !important;\n top: 20px !important;\n right: 20px !important;\n z-index: 99999 !important;\n border-radius: 3px !important;\n max-width: 250px !important;\n word-wrap: break-word !important;\n font-family: Arial, sans-serif !important;\n font-size: 14px !important;\n box-shadow: 0 2px 10px rgba(0,0,0,0.3) !important;\n cursor: pointer !important;\n " onclick="this.parentNode.removeChild(this)">\n ${t.message||"Notification"}\n <span style="float: right; margin-left: 10px; font-weight: bold;">×</span>\n </div>\n `,n._animationDuration=500,n._isEmergencyToast=!0,setTimeout(()=>{try{n.parentNode&&n.parentNode.removeChild(n),e(n)}catch(t){}},t.duration||3e3),n}catch(n){return setTimeout(()=>{try{alert(t.message||"Toast Notification"),e(null)}catch(t){e(null)}},100),null}}(t,n)}let a=!1,s=null,c=new Set,l=!1;function u(t){if(!a){a=!0;try{const e=o(t.position),n=i(t,d);if(!n)throw new Error("Toast creation returned null");s=n,e.appendChild?e.appendChild(n):document.body.appendChild(n);const r=setTimeout(()=>{d(n)},t.duration||3e3);return c.add(r),void function(t){if(l)return;l=!0;const e=setTimeout(()=>{y()},2.5*t);c.add(e)}(t.duration||3e3)}catch(t){}try{const e=function(t){const e=document.createElement("div");return e.style.cssText="\n position: fixed !important;\n top: 20px !important;\n right: 20px !important;\n background: #333 !important;\n color: white !important;\n padding: 12px 20px !important;\n border-radius: 5px !important;\n z-index: 99999 !important;\n max-width: 300px !important;\n font-family: Arial, sans-serif !important;\n cursor: pointer !important;\n ",e.textContent=t.message||"Notification",e.onclick=()=>d(e),e}(t);return s=e,document.body.appendChild(e),void setTimeout(()=>{d(e)},t.duration||3e3)}catch(t){}try{alert(t.message||"Notification"),a=!1}catch(t){a=!1}}}function d(t){try{if(!t)return void m();if(t.style&&!t._isEmergencyToast){t.style.opacity="0",t.style.transform="translateY(-20px)";const e=t._animationDuration||500;setTimeout(()=>{p(t)},e)}else p(t)}catch(t){y()}}function p(t){try{t._cleanupCloseButton&&t._cleanupCloseButton(),n(t),m()}catch(t){y()}}function m(){a=!1,s=null,c.forEach(t=>{try{clearTimeout(t)}catch(t){}}),c.clear(),l=!1}function y(){try{s&&n(s);document.querySelectorAll('[id*="toast"], [id*="emergency"]').forEach(t=>{try{n(t)}catch(t){}}),m()}catch(t){a=!1,s=null}}let f={success:"#28a745",error:"#dc3545",warning:"#ffc107",info:"#17a2b8"},h={success:"Operation completed successfully!",error:"Something went wrong!",warning:"Warning message!",info:"Information message!"};function g(t={}){try{const e=function(t){const e={duration:3e3,position:"bottom-right",type:"info",backgroundColor:void 0,textColor:"white",showCloseButton:!1,animationDuration:"0.5s",animationEasing:"ease",showProgressBar:!1,progressColor:void 0,progressHeight:"4px",progressPosition:"bottom"};let n={...e};t&&"object"==typeof t&&!Array.isArray(t)&&(Object.keys(e).forEach(e=>{t.hasOwnProperty(e)&&(n[e]=t[e])}),void 0!==t.message&&(n.message=t.message));try{!n.backgroundColor&&n.type&&f[n.type]&&(n.backgroundColor=f[n.type]),!n.message&&n.type&&h[n.type]&&(n.message=h[n.type])}catch(t){}n.message&&"string"==typeof n.message||(n.message="Notification");("number"!=typeof n.duration||n.duration<100)&&(n.duration=3e3);return n}(t);u(e)}catch(e){const n="string"==typeof t?.message?t.message.substring(0,200):"Toast notification failed";try{alert(n)}catch(t){}}}function x(t){try{t&&"object"==typeof t&&!Array.isArray(t)&&(f={...f,...t})}catch(t){}}function b(t){try{t&&"object"==typeof t&&!Array.isArray(t)&&(h={...h,...t})}catch(t){}}try{"undefined"!=typeof window&&(window.customizableToast={createToast:g,setDefaultColors:x,setDefaultMessages:b})}catch(t){}t.createToast=g,t.setDefaultColors=x,t.setDefaultMessages=b,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=index.umd.js.map