UNPKG

@hackbyte/vue-toaster

Version:

A modern Vue 3 toaster library with TypeScript support

141 lines (140 loc) 3.95 kB
import { defineComponent as _, ref as b, reactive as k, createElementBlock as g, createCommentVNode as y, openBlock as T, normalizeClass as m, createElementVNode as o, createTextVNode as w, toDisplayString as x } from "vue"; function A(a) { return { all: a = a || /* @__PURE__ */ 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(r) { r(t); }), (s = a.get("*")) && s.slice().map(function(r) { r(e, t); }); } }; } const c = A(), C = { class: "message-block" }, B = /* @__PURE__ */ _({ __name: "Toaster", props: { position: { type: String, default: "bottom-right" }, enableActions: { type: Boolean, default: !0 } }, setup(a) { const e = a, t = b(!1), s = k({ message: "", class: "", type: "", icon: "", state: "" }), r = [ { 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" } ], n = () => { t.value = !1; }; return c.on("openToast", (d) => { const { type: u, message: h, state: v } = d; t.value = !0, r.forEach((f) => { f.type === u && (s.message = h, s.type = u, s.class = f.class, s.icon = f.icon, s.state = v); }); }), c.on("closeToast", () => { t.value = !1; }), (d, u) => t.value ? (T(), g("div", { key: 0, class: m(["toaster", s.class, e.position]) }, [ o("div", C, [ o("i", { class: m([s.icon, "base-icon"]) }, null, 2), o("span", null, [ w(x(s.message) + " ", 1), o("span", { class: m(s.state === "ongoing" ? "animated-dots" : "unanimated-dots") }, "...", 2) ]) ]), e.enableActions ? (T(), g("div", { key: 0, class: "btn", onClick: n }, "✕")) : y("", !0) ], 2)) : y("", !0); } }); var i = /* @__PURE__ */ ((a) => (a.ongoing = "ongoing", a.finished = "finished", a))(i || {}); const l = (a, e) => { c.emit("openToast", { type: (e == null ? void 0 : e.type) || "info", message: a, state: i.finished }), setTimeout(() => { c.emit("closeToast"); }, (e == null ? void 0 : e.duration) || 4e3); }, N = (a, e, t) => (c.emit("openToast", { type: "info", message: (t == null ? void 0 : t.progress) || "Loading", state: i.ongoing }), new Promise((s, r) => { a.then((n) => { c.emit("openToast", { type: "success", message: e, state: i.finished }), s(n), setTimeout(() => { c.emit("closeToast"); }, (t == null ? void 0 : t.duration) || 4e3); }).catch((n) => { c.emit("openToast", { type: "error", message: (t == null ? void 0 : t.error) || "An error occurred", state: i.finished }), r(n), setTimeout(() => { c.emit("closeToast"); }, (t == null ? void 0 : t.duration) || 4e3); }); })), V = { 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 }); } }; export { N as APIToast, B as Toaster, V as toast };