UNPKG

@hackbyte/vue-toaster

Version:

A modern Vue 3 toaster library with TypeScript support

2 lines (1 loc) 2.87 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("vue");function y(a){return{all:a=a||new Map,on:function(e,t){var s=a.get(e);s?s.push(t):a.set(e,[t])},off:function(e,t){var s=a.get(e);s&&(t?s.splice(s.indexOf(t)>>>0,1):a.set(e,[]))},emit:function(e,t){var s=a.get(e);s&&s.slice().map(function(n){n(t)}),(s=a.get("*"))&&s.slice().map(function(n){n(e,t)})}}}const r=y(),T={class:"message-block"},v=c.defineComponent({__name:"Toaster",props:{position:{type:String,default:"bottom-right"},enableActions:{type:Boolean,default:!0}},setup(a){const e=a,t=c.ref(!1),s=c.reactive({message:"",class:"",type:"",icon:"",state:""}),n=[{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"}],i=()=>{t.value=!1};return r.on("openToast",f=>{const{type:u,message:d,state:g}=f;t.value=!0,n.forEach(m=>{m.type===u&&(s.message=d,s.type=u,s.class=m.class,s.icon=m.icon,s.state=g)})}),r.on("closeToast",()=>{t.value=!1}),(f,u)=>t.value?(c.openBlock(),c.createElementBlock("div",{key:0,class:c.normalizeClass(["toaster",s.class,e.position])},[c.createElementVNode("div",T,[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:i},"✕")):c.createCommentVNode("",!0)],2)):c.createCommentVNode("",!0)}});var o=(a=>(a.ongoing="ongoing",a.finished="finished",a))(o||{});const l=(a,e)=>{r.emit("openToast",{type:(e==null?void 0:e.type)||"info",message:a,state:o.finished}),setTimeout(()=>{r.emit("closeToast")},(e==null?void 0:e.duration)||4e3)},h=(a,e,t)=>(r.emit("openToast",{type:"info",message:(t==null?void 0:t.progress)||"Loading",state:o.ongoing}),new Promise((s,n)=>{a.then(i=>{r.emit("openToast",{type:"success",message:e,state:o.finished}),s(i),setTimeout(()=>{r.emit("closeToast")},(t==null?void 0:t.duration)||4e3)}).catch(i=>{r.emit("openToast",{type:"error",message:(t==null?void 0:t.error)||"An error occurred",state:o.finished}),n(i),setTimeout(()=>{r.emit("closeToast")},(t==null?void 0:t.duration)||4e3)})})),b={success:(a,e)=>{l(a,{type:"success",duration:e==null?void 0:e.duration})},error:(a,e)=>{l(a,{type:"error",duration:e==null?void 0:e.duration})},warning:(a,e)=>{l(a,{type:"warning",duration:e==null?void 0:e.duration})},info:(a,e)=>{l(a,{type:"info",duration:e==null?void 0:e.duration})}};exports.APIToast=h;exports.Toaster=v;exports.toast=b;