UNPKG

@hackbyte/vue-toaster

Version:

A modern Vue 3 toaster library with TypeScript support

2 lines (1 loc) 3.08 kB
(function(n,c){typeof exports=="object"&&typeof module<"u"?c(exports,require("vue")):typeof define=="function"&&define.amd?define(["exports","vue"],c):(n=typeof globalThis<"u"?globalThis:n||self,c(n["vue-toaster"]={},n.Vue))})(this,function(n,c){"use strict";function g(t){return{all:t=t||new Map,on:function(e,a){var s=t.get(e);s?s.push(a):t.set(e,[a])},off:function(e,a){var s=t.get(e);s&&(a?s.splice(s.indexOf(a)>>>0,1):t.set(e,[]))},emit:function(e,a){var s=t.get(e);s&&s.slice().map(function(i){i(a)}),(s=t.get("*"))&&s.slice().map(function(i){i(e,a)})}}}const r=g(),y={class:"message-block"},T=c.defineComponent({__name:"Toaster",props:{position:{type:String,default:"bottom-right"},enableActions:{type:Boolean,default:!0}},setup(t){const e=t,a=c.ref(!1),s=c.reactive({message:"",class:"",type:"",icon:"",state:""}),i=[{type:"success",class:"toaster-success",icon:"fa fa-check-circle success-icon"},{type:"error",class:"toaster-error",icon:"fa fa-exclamation-circle error-icon"},{type:"warning",class:"toaster-warning",icon:"fa fa-exclamation-triangle warning-icon"},{type:"info",class:"toaster-info",icon:"fas fa-info-circle info-icon"}],l=()=>{a.value=!1};return r.on("openToast",u=>{const{type:d,message:k,state:E}=u;a.value=!0,i.forEach(m=>{m.type===d&&(s.message=k,s.type=d,s.class=m.class,s.icon=m.icon,s.state=E)})}),r.on("closeToast",()=>{a.value=!1}),(u,d)=>a.value?(c.openBlock(),c.createElementBlock("div",{key:0,class:c.normalizeClass(["toaster",s.class,e.position])},[c.createElementVNode("div",y,[c.createElementVNode("i",{class:c.normalizeClass([s.icon,"base-icon"])},null,2),c.createElementVNode("span",null,[c.createTextVNode(c.toDisplayString(s.message)+" ",1),c.createElementVNode("span",{class:c.normalizeClass(s.state==="ongoing"?"animated-dots":"unanimated-dots")},"...",2)])]),e.enableActions?(c.openBlock(),c.createElementBlock("div",{key:0,class:"btn",onClick:l},"✕")):c.createCommentVNode("",!0)],2)):c.createCommentVNode("",!0)}});var o=(t=>(t.ongoing="ongoing",t.finished="finished",t))(o||{});const f=(t,e)=>{r.emit("openToast",{type:(e==null?void 0:e.type)||"info",message:t,state:o.finished}),setTimeout(()=>{r.emit("closeToast")},(e==null?void 0:e.duration)||4e3)},h=(t,e,a)=>(r.emit("openToast",{type:"info",message:(a==null?void 0:a.progress)||"Loading",state:o.ongoing}),new Promise((s,i)=>{t.then(l=>{r.emit("openToast",{type:"success",message:e,state:o.finished}),s(l),setTimeout(()=>{r.emit("closeToast")},(a==null?void 0:a.duration)||4e3)}).catch(l=>{r.emit("openToast",{type:"error",message:(a==null?void 0:a.error)||"An error occurred",state:o.finished}),i(l),setTimeout(()=>{r.emit("closeToast")},(a==null?void 0:a.duration)||4e3)})})),b={success:(t,e)=>{f(t,{type:"success",duration:e==null?void 0:e.duration})},error:(t,e)=>{f(t,{type:"error",duration:e==null?void 0:e.duration})},warning:(t,e)=>{f(t,{type:"warning",duration:e==null?void 0:e.duration})},info:(t,e)=>{f(t,{type:"info",duration:e==null?void 0:e.duration})}};n.APIToast=h,n.Toaster=T,n.toast=b,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});