@t007/toast 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,521 @@
1
+ "use strict";
2
+ (() => {
3
+ // ../utils/dist/index.js
4
+ function clamp(min = 0, val, max = Infinity) {
5
+ return Math.min(Math.max(val, min), max);
6
+ }
7
+ function uid(prefix = "") {
8
+ return prefix + Date.now().toString(36) + "_" + performance.now().toString(36).replace(".", "") + "_" + Math.random().toString(36).slice(2);
9
+ }
10
+ function isSameURL(src1, src2) {
11
+ if (typeof src1 !== "string" || typeof src2 !== "string" || !src1 || !src2) return false;
12
+ try {
13
+ const u1 = new URL(src1, window.location.href), u2 = new URL(src2, window.location.href);
14
+ return decodeURIComponent(u1.origin + u1.pathname) === decodeURIComponent(u2.origin + u2.pathname);
15
+ } catch {
16
+ return src1.replace(/\\/g, "/").split("?")[0].trim() === src2.replace(/\\/g, "/").split("?")[0].trim();
17
+ }
18
+ }
19
+ function createEl(tag, props = {}, dataset = {}, styles = {}) {
20
+ return assignEl(tag ? document?.createElement(tag) : void 0, props, dataset, styles) ?? null;
21
+ }
22
+ function assignEl(el, props = {}, dataset = {}, styles = {}) {
23
+ if (!el) return;
24
+ for (const k of Object.keys(props)) if (props[k] !== void 0) el[k] = props[k];
25
+ for (const k of Object.keys(dataset)) if (dataset[k] !== void 0) el.dataset[k] = String(dataset[k]);
26
+ for (const k of Object.keys(styles)) if (styles[k] !== void 0) el.style[k] = styles[k];
27
+ }
28
+ function loadResource(src, type = "style", { module, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, attempts = 3, retryKey = false } = {}, w = window) {
29
+ w.t007 ??= {}, w.t007._resourceCache ??= {};
30
+ if (w.t007._resourceCache[src]) return w.t007._resourceCache[src];
31
+ const existing = type === "script" ? Array.prototype.find.call(w.document.scripts, (s) => isSameURL(s.src, src)) : type === "style" ? Array.prototype.find.call(w.document.styleSheets, (s) => isSameURL(s.href, src)) : null;
32
+ if (existing) return w.t007._resourceCache[src] = Promise.resolve(existing);
33
+ w.t007._resourceCache[src] = new Promise((resolve, reject) => {
34
+ (function tryLoad(remaining, el) {
35
+ const onerror = () => {
36
+ el?.remove?.();
37
+ if (remaining > 1) {
38
+ setTimeout(tryLoad, 1e3, remaining - 1);
39
+ console.warn(`Retrying ${type} load (${attempts - remaining + 1}): ${src}...`);
40
+ } else {
41
+ delete w.t007._resourceCache[src];
42
+ reject(new Error(`${type} load failed after ${attempts} attempts: ${src}`));
43
+ }
44
+ };
45
+ const url = retryKey && remaining < attempts ? `${src}${src.includes("?") ? "&" : "?"}_${retryKey}=${Date.now()}` : src;
46
+ if (type === "script") w.document.body.append(el = createEl("script", { src: url, type: module ? "module" : "text/javascript", crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, onload: () => resolve(el), onerror }) || "");
47
+ else if (type === "style") w.document.head.append(el = createEl("link", { rel: "stylesheet", href: url, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, onload: () => resolve(el), onerror }) || "");
48
+ else reject(new Error(`Unsupported resource type: ${type}`));
49
+ })(attempts);
50
+ });
51
+ return w.t007._resourceCache[src];
52
+ }
53
+ function onAllMethods(owner, callback) {
54
+ let proto = owner;
55
+ while (proto && proto !== Object.prototype) {
56
+ for (const method of Object.getOwnPropertyNames(proto)) {
57
+ if (method === "constructor") continue;
58
+ if ("function" !== typeof Object.getOwnPropertyDescriptor(proto, method)?.value) continue;
59
+ callback(method, owner);
60
+ }
61
+ proto = Object.getPrototypeOf(proto);
62
+ }
63
+ }
64
+ function bindAllMethods(owner) {
65
+ onAllMethods(owner, (method, owner2) => {
66
+ owner2[method] = owner2[method].bind(owner2);
67
+ });
68
+ }
69
+ var _SCROLLERS = /* @__PURE__ */ new WeakMap();
70
+ var _SCROLLER_R_OBSERVER = typeof window !== "undefined" ? new ResizeObserver((entries) => entries.forEach(({ target }) => _SCROLLERS.get(target)?.update())) : null;
71
+ var _SCROLLER_M_OBSERVER = typeof window !== "undefined" ? new MutationObserver((entries) => {
72
+ const els = /* @__PURE__ */ new Set();
73
+ for (const entry of entries) {
74
+ let node = entry.target instanceof Element ? entry.target : null;
75
+ while (node && !_SCROLLERS.has(node)) node = node.parentElement;
76
+ if (node) els.add(node);
77
+ }
78
+ for (const el of els) _SCROLLERS.get(el)?.update();
79
+ }) : null;
80
+
81
+ // src/index.js
82
+ var T007_Toast = class {
83
+ #autoCloseInterval;
84
+ #progressInterval;
85
+ #timeVisible = 0;
86
+ #isPaused = false;
87
+ #shouldUnPause;
88
+ queue = [];
89
+ destroyed = true;
90
+ #visiblityChange = () => this.#shouldUnPause = document.visibilityState === "visible";
91
+ constructor(options) {
92
+ bindAllMethods(this);
93
+ this.opts = { ...options };
94
+ this.id = this.opts.id ??= uid(this.opts.idPrefix ?? "t007_toast_");
95
+ t007.toasts.set(this.id, this);
96
+ "number" !== typeof this.opts.delay ? this.init() : this.queue.push(setTimeout(this.init, this.opts.delay));
97
+ this.update(this.opts);
98
+ }
99
+ init() {
100
+ this.toastElement = createEl("div", { className: "t007-toast", id: this.id, ariaAtomic: "true" }, { idPrefix: this.opts.idPrefix });
101
+ requestAnimationFrame(() => this.toastElement.classList.add("t007-toast-show"));
102
+ this.destroyed = false;
103
+ }
104
+ update(options) {
105
+ if (!options || typeof options !== "object") return this.opts.id;
106
+ try {
107
+ this.opts = { ...this.opts, ...options };
108
+ const run = () => Object.keys(options).forEach((key) => this[key] = options[key]);
109
+ "number" !== typeof this.opts.delay ? run() : this.queue.push(setTimeout(run, this.opts.delay));
110
+ this.opts.delay = null;
111
+ } catch (err) {
112
+ console.error("toast update failed:", err);
113
+ }
114
+ return this.opts.id;
115
+ }
116
+ play = () => setTimeout(() => this.#isPaused = false);
117
+ pause = () => this.#isPaused = true;
118
+ set rootElement(value) {
119
+ const container = value?.querySelector(`.t007-toast-container[data-position="${this.opts.position}"]`);
120
+ container?.style.setProperty("--t007-toast-container-position", value === document.body ? "fixed" : "absolute");
121
+ container && !value.contains(container) && value.append(container);
122
+ }
123
+ set type(value) {
124
+ this.toastElement.classList.remove("info", "success", "error", "warning");
125
+ value && this.toastElement.classList.add(value);
126
+ this.toastElement.role = value === "error" || value === "warning" ? "alert" : "status";
127
+ this.toastElement.ariaLive = value === "error" || value === "warning" ? "assertive" : "polite";
128
+ if (value) this.icon = this.opts.icon;
129
+ }
130
+ set bodyHTML(value) {
131
+ this.toastElement.querySelectorAll(".t007-toast > *:not(.t007-toast-cancel-button)").forEach((el) => el.remove());
132
+ this.toastElement.insertAdjacentHTML("afterbegin", `${value ? typeof value === "function" ? value() : value : ""}`);
133
+ }
134
+ set render(value) {
135
+ const bodyText = () => this.toastElement.querySelector(".t007-toast-body-text");
136
+ if (value) {
137
+ this._setUpBodyHTML();
138
+ this.toastElement.querySelector(".t007-toast-body").prepend(bodyText() || createEl("p", { className: "t007-toast-body-text" }));
139
+ const text = bodyText();
140
+ text.innerHTML = text.dataset.render = typeof value === "function" ? value() : value;
141
+ } else bodyText()?.remove();
142
+ }
143
+ set actions(value) {
144
+ const actionsWrapper = () => this.toastElement.querySelector(".t007-toast-actions-wrapper"), values = value ? Object.entries(value) : [];
145
+ if (values.length) {
146
+ this._setUpBodyHTML();
147
+ this.toastElement.querySelector(".t007-toast-body").insertAdjacentElement("afterend", actionsWrapper() || createEl("div", { className: "t007-toast-actions-wrapper" }));
148
+ const wrapper = actionsWrapper();
149
+ wrapper.innerHTML = values.map(([label]) => label ? `<button class="t007-toast-action-button" data-action="${label}">${label}</button>` : "").join("");
150
+ wrapper.querySelectorAll(".t007-toast-action-button").forEach((btn, i) => btn.onclick = (e) => values[i][1]?.(e, this));
151
+ } else actionsWrapper()?.remove();
152
+ }
153
+ set image(value) {
154
+ const image = () => this.toastElement.querySelector(".t007-toast-image");
155
+ if (value) {
156
+ this._setUpBodyHTML();
157
+ this.toastElement.querySelector(".t007-toast-image-wrapper").prepend(
158
+ image() || createEl("img", {
159
+ className: "t007-toast-image",
160
+ alt: "toast-image"
161
+ })
162
+ );
163
+ const img = image();
164
+ img.src = value;
165
+ img.onload = img.onerror = () => img.dataset.loaded = img.complete && img.naturalWidth > 0;
166
+ } else image()?.remove();
167
+ }
168
+ get icon() {
169
+ return this.opts.icon === true ? t007.TOAST_ICONS[this.opts.type] || "" : this.opts.icon || "";
170
+ }
171
+ set icon(value) {
172
+ if (this.opts.isLoading) return;
173
+ const icon = () => this.toastElement.querySelector(".t007-toast-icon:not(.t007-toast-loader)");
174
+ if (value) {
175
+ this._setUpBodyHTML();
176
+ this.toastElement.querySelector(".t007-toast-image-wrapper").appendChild(icon() || createEl("span", { className: "t007-toast-icon" }));
177
+ const icn = icon();
178
+ icn.innerHTML = icn.dataset.icon = this.icon;
179
+ } else icon()?.remove();
180
+ }
181
+ set isLoading(value) {
182
+ const loader = () => this.toastElement.querySelector(".t007-toast-loader");
183
+ if (value) {
184
+ this._setUpBodyHTML();
185
+ this.toastElement.querySelectorAll(".t007-toast-icon:not(.t007-toast-loader)").forEach((i) => i.remove());
186
+ this.toastElement.querySelector(".t007-toast-image-wrapper").appendChild(
187
+ loader() || createEl("span", {
188
+ className: "t007-toast-icon t007-toast-loader"
189
+ })
190
+ );
191
+ loader().innerHTML = typeof value === "string" ? value : t007.TOAST_ICONS.loading;
192
+ } else {
193
+ loader()?.remove();
194
+ this.icon = this.opts.icon;
195
+ }
196
+ }
197
+ set closeButton(value) {
198
+ const btn = this.toastElement.querySelector(".t007-toast-cancel-button");
199
+ if (value)
200
+ this.toastElement.appendChild(
201
+ btn || createEl("button", {
202
+ title: "Close",
203
+ ariaLabel: "Close notification",
204
+ className: "t007-toast-cancel-button",
205
+ innerHTML: "&times;",
206
+ onclick: this._remove
207
+ })
208
+ );
209
+ else btn?.remove();
210
+ }
211
+ get animation() {
212
+ if (this.opts.animation === true || this.opts.animation === "slide")
213
+ switch (this.opts.position) {
214
+ case "top-right":
215
+ case "center-right":
216
+ case "bottom-right":
217
+ return "slide-left";
218
+ case "top-center":
219
+ case "center-center":
220
+ case "bottom-center":
221
+ return this.opts.position === "top-center" ? "slide-down" : "slide-up";
222
+ case "top-left":
223
+ case "center-left":
224
+ case "bottom-left":
225
+ default:
226
+ return "slide-right";
227
+ }
228
+ return this.opts.animation;
229
+ }
230
+ set animation(value) {
231
+ this.toastElement.dataset.animation = this.animation;
232
+ }
233
+ get autoClose() {
234
+ return this.opts.autoClose === true ? t007.TOAST_DURATIONS[this.opts.type] || t007.TOAST_DURATIONS.info : this.opts.autoClose;
235
+ }
236
+ set autoClose(value) {
237
+ cancelAnimationFrame(this.#autoCloseInterval);
238
+ this.#timeVisible = 0;
239
+ let lastTime;
240
+ const loop = (time) => {
241
+ if (this.#shouldUnPause) {
242
+ lastTime = null;
243
+ this.#shouldUnPause = false;
244
+ }
245
+ if (lastTime == null) {
246
+ lastTime = time;
247
+ return this.#autoCloseInterval = requestAnimationFrame(loop);
248
+ }
249
+ if (!this.#isPaused) {
250
+ this.#timeVisible += time - lastTime;
251
+ this.onTimeUpdate?.(this.#timeVisible);
252
+ if ("number" == typeof this.autoClose && this.#timeVisible >= this.autoClose) return this._remove("smooth", true);
253
+ }
254
+ lastTime = time;
255
+ this.#autoCloseInterval = requestAnimationFrame(loop);
256
+ };
257
+ if (value) this.#autoCloseInterval = requestAnimationFrame(loop);
258
+ }
259
+ set position(value) {
260
+ const currentContainer = this.toastElement.parentElement;
261
+ const container = this.opts.rootElement?.querySelector(`.t007-toast-container[data-position="${value}"]`) || this._createContainer(value);
262
+ container[this.opts.newestOnTop ? "prepend" : "append"](this.toastElement);
263
+ if (!(currentContainer == null || currentContainer.hasChildNodes())) currentContainer.remove();
264
+ }
265
+ set closeOnClick(value) {
266
+ this.toastElement.onclick = value ? () => this._remove() : null;
267
+ }
268
+ set hideProgressBar(value) {
269
+ this.toastElement.classList.toggle("progress", !value);
270
+ this.nprogress = 0;
271
+ cancelAnimationFrame(this.#progressInterval);
272
+ const loop = () => {
273
+ if ("number" == typeof this.autoClose && !this.#isPaused) this.nprogress = 1 - this.#timeVisible / this.autoClose;
274
+ this.#progressInterval = requestAnimationFrame(loop);
275
+ };
276
+ if (!value) this.#progressInterval = requestAnimationFrame(loop);
277
+ }
278
+ get nprogress() {
279
+ return Number(this.toastElement.style.getProperty("--progress"));
280
+ }
281
+ set nprogress(value) {
282
+ this.toastElement.style.setProperty("--progress", value);
283
+ }
284
+ set pauseOnHover(value) {
285
+ this.toastElement.onmouseover = value ? this.pause : null;
286
+ this.toastElement.onmouseleave = value ? this.play : null;
287
+ this.toastElement[value ? "addEventListener" : "removeEventListener"]("touchend", this.play);
288
+ }
289
+ set pauseOnFocusLoss(value) {
290
+ value ? document.addEventListener("visibilitychange", this.#visiblityChange) : document.removeEventListener("visibilitychange", this.#visiblityChange);
291
+ }
292
+ set tag(value) {
293
+ this.toastElement.dataset.tag = value;
294
+ }
295
+ set renotify(value) {
296
+ value && this.opts.tag && t007.toasts.entries().forEach(([id, toast2]) => id !== this.id && (toast2.opts.tag ?? 1) === (this.opts.tag ?? 0) && toast2._remove("instant"));
297
+ }
298
+ get vibrate() {
299
+ return this.opts.vibrate === true ? t007.TOAST_VIBRATIONS[this.opts.type] || t007.TOAST_VIBRATIONS.info : this.opts.vibrate;
300
+ }
301
+ set vibrate(value) {
302
+ value && navigator?.vibrate?.(this.vibrate);
303
+ }
304
+ set maxToasts(value) {
305
+ const toastsInContainer = [...this.toastElement?.parentElement?.children || []];
306
+ if (!toastsInContainer.length) return;
307
+ for (let i = 0; i < toastsInContainer.length - value; i++) {
308
+ [...t007.toasts.values()].find((t) => t.toastElement === (this.opts.newestOnTop ? toastsInContainer[toastsInContainer.length - 1 - i] : toastsInContainer[i]))?._remove("instant");
309
+ }
310
+ }
311
+ set newestOnTop(value) {
312
+ this.toastElement?.parentElement?.[value ? "prepend" : "append"](this.toastElement);
313
+ }
314
+ set dragToClose(value) {
315
+ this.toastElement.dataset.dragToClose = this._ptrType = value;
316
+ this.toastElement.onpointerdown = value ? this._handleToastPointerStart : null;
317
+ this.toastElement.onpointerup = value ? this._handleToastPointerUp : null;
318
+ }
319
+ _handleToastPointerStart(e) {
320
+ if (typeof this._ptrType === "string" && e.pointerType !== this._ptrType) return;
321
+ if (e.touches?.length > 1) return;
322
+ !e.target?.matches('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])') && this.toastElement.setPointerCapture(e.pointerId);
323
+ this.#isPaused = true;
324
+ this._ptrTicker = this._ptrDirSet = this._ptrDir = false;
325
+ this._ptrStartX = e.clientX ?? e.targetTouches[0]?.clientX;
326
+ this._ptrStartY = e.clientY ?? e.targetTouches[0]?.clientY;
327
+ this.toastElement.addEventListener("pointermove", this._handleToastPointerMove, { passive: false });
328
+ this.toastElement.style.setProperty("transition", "none", "important");
329
+ }
330
+ _handleToastPointerMove(e) {
331
+ e.preventDefault();
332
+ if (this._ptrTicker) return;
333
+ this._ptrRAF = requestAnimationFrame(() => {
334
+ const has = (str) => this.opts.dragToCloseDir.includes(str), x = e.clientX ?? e.targetTouches[0]?.clientX, y = e.clientY ?? e.targetTouches[0]?.clientY;
335
+ this._ptrDir ||= Math.abs(x - this._ptrStartX) >= Math.abs(y - this._ptrStartY) ? "x" : "y";
336
+ this._ptrDeltaX = (has("|") ? this._ptrDir == "x" : has("x")) && !has(x - this._ptrStartX > 0 ? "-" : "+") ? x - this._ptrStartX : 0;
337
+ this._ptrDeltaY = (has("|") ? this._ptrDir == "y" : has("y")) && !has(y - this._ptrStartY > 0 ? "+" : "-") ? y - this._ptrStartY : 0;
338
+ this.toastElement.style.setProperty("transform", `translate(${this._ptrDeltaX}px, ${this._ptrDeltaY}px)`, "important");
339
+ const xR = Math.abs(this._ptrDeltaX) / this.toastElement.offsetWidth, yR = Math.abs(this._ptrDeltaY) / this.toastElement.offsetHeight;
340
+ this.toastElement.style.setProperty("opacity", clamp(0, 1 - (yR > 0.5 ? yR : xR), 1), "important");
341
+ if (!this._ptrDirSet && !xR && !yR) this._ptrDir = false;
342
+ if (this._ptrDir) this._ptrDirSet = has("||");
343
+ this._ptrTicker = false;
344
+ });
345
+ this._ptrTicker = true;
346
+ }
347
+ _handleToastPointerUp(e) {
348
+ if (typeof this._ptrType === "string" && e.pointerType !== this._ptrType) return;
349
+ cancelAnimationFrame(this._ptrRAF);
350
+ if (Math.abs(this._ptrDeltaX) > this.toastElement.offsetWidth * (this.opts.dragToClosePercent.x ?? this.opts.dragToClosePercent / 100) || Math.abs(this._ptrDeltaY) > this.toastElement.offsetHeight * (this.opts.dragToClosePercent.y ?? this.opts.dragToClosePercent / 100)) return this._remove("instant");
351
+ this.#isPaused = this._ptrTicker = this._ptrDirSet = this._ptrDir = false;
352
+ this.toastElement.removeEventListener("pointermove", this._handleToastPointerMove, { passive: false });
353
+ this.toastElement.style.removeProperty("transition");
354
+ this.toastElement.style.removeProperty("transform");
355
+ this.toastElement.style.removeProperty("opacity");
356
+ }
357
+ _remove(manner = "smooth", timeElapsed = false) {
358
+ if (!this.opts.isLoading) t007.toasts.delete(this.id);
359
+ this.queue.forEach(clearTimeout);
360
+ document.removeEventListener("visibilitychange", this.#visiblityChange);
361
+ cancelAnimationFrame(this.#autoCloseInterval);
362
+ cancelAnimationFrame(this.#progressInterval);
363
+ if (this.destroyed || manner === "instant" || !this.animation) this._cleanUpToast();
364
+ else this.toastElement.onanimationend = this._cleanUpToast;
365
+ this.toastElement.classList.remove("t007-toast-show");
366
+ this.onClose?.(timeElapsed);
367
+ }
368
+ _createContainer(position) {
369
+ const container = document.createElement("div");
370
+ container.classList.add("t007-toast-container");
371
+ container.style.setProperty("--t007-toast-container-position", this.opts.rootElement === document.body ? "fixed" : "absolute");
372
+ container.dataset.position = position;
373
+ this.opts.rootElement?.append(container);
374
+ return container;
375
+ }
376
+ _setUpBodyHTML() {
377
+ this.toastElement.querySelectorAll(".t007-toast > *:not(.t007-toast-image-wrapper, .t007-toast-body, .t007-toast-actions-wrapper, .t007-toast-cancel-button)").forEach((el) => el.remove());
378
+ const imageWrapper = () => this.toastElement.querySelector(".t007-toast-image-wrapper");
379
+ if (!imageWrapper()) this.toastElement.prepend(createEl("div", { className: "t007-toast-image-wrapper" }));
380
+ if (!this.toastElement.querySelector(".t007-toast-body")) imageWrapper().insertAdjacentElement("afterend", createEl("div", { className: "t007-toast-body" }));
381
+ }
382
+ _cleanUpToast() {
383
+ const container = this.toastElement.parentElement;
384
+ this.toastElement.remove();
385
+ if (!container?.hasChildNodes()) container?.remove();
386
+ this.destroyed = true;
387
+ }
388
+ };
389
+ var toasting = {
390
+ update(base, id, options) {
391
+ const toast2 = t007.toasts.get(id);
392
+ toast2?.queue.forEach(clearTimeout);
393
+ return !!toast2 && (toast2.destroyed ? base(options.render, { ...toast2.opts, id, ...options }) : toast2.update(options));
394
+ },
395
+ message: (base, defaults, action) => base[action] = (renderOrId, options = {}) => {
396
+ options = { ...options, type: action === "warn" ? "warning" : action };
397
+ if (!t007.toasts.get(renderOrId)) return base(renderOrId, options);
398
+ const { autoClose, closeButton, closeOnClick, dragToClose } = defaults();
399
+ return base.update(renderOrId, {
400
+ ...t007.toasts.get(renderOrId)?.opts.isLoading ? { autoClose, closeButton, closeOnClick, dragToClose } : {},
401
+ ...options,
402
+ isLoading: false
403
+ });
404
+ },
405
+ loading: (base, renderOrId, options = {}) => (t007.toasts.get(renderOrId) ? base.update : base)(renderOrId, {
406
+ autoClose: false,
407
+ closeButton: false,
408
+ closeOnClick: false,
409
+ dragToClose: false,
410
+ ...options,
411
+ isLoading: options.isLoading || true,
412
+ type: ""
413
+ }),
414
+ promise(base, promise = new Promise((res, rej) => setTimeout(Math.round(Math.random()) ? res : rej, 3e3)), { pending, success, error } = {}) {
415
+ if (!promise || typeof promise.then !== "function") return console.error("toast.promise() requires a valid promise");
416
+ const NFC = (input, type) => typeof input === "string" ? { render: input, type } : typeof input === "object" ? { ...input, type } : { type };
417
+ const pendingConfig = NFC(pending);
418
+ const pendingToastId = base.loading(pendingConfig.render || "Promise pending...", { ...pendingConfig });
419
+ promise.then(
420
+ (response) => {
421
+ const successConfig = NFC(success || "Promise resolved", "success");
422
+ const { render, bodyHTML } = successConfig;
423
+ if (typeof render === "function") successConfig.render = (response2) => render(response2);
424
+ if (typeof bodyHTML === "function") successConfig.bodyHTML = (response2) => bodyHTML(response2);
425
+ base.success(pendingToastId, successConfig);
426
+ return response;
427
+ },
428
+ (err) => {
429
+ const errorConfig = NFC(error || "Promise rejected", "error");
430
+ const { render, bodyHTML } = errorConfig;
431
+ if (typeof render === "function") errorConfig.render = (err2) => render(err2);
432
+ if (typeof bodyHTML === "function") errorConfig.bodyHTML = (err2) => bodyHTML(err2);
433
+ base.error(pendingToastId, errorConfig);
434
+ return Promise.reject(err);
435
+ }
436
+ );
437
+ return promise;
438
+ },
439
+ dismiss(base, id, manner, timeElapsed) {
440
+ return !arguments.length ? base.dismissAll() : t007.toasts.get(id)?._remove(manner, timeElapsed);
441
+ },
442
+ dismissAll(base, idPrefix) {
443
+ t007.toasts.values().forEach((toast2) => (!arguments.length ? true : toast2.id.startsWith(idPrefix)) && toast2._remove());
444
+ },
445
+ doForAll(base, action, options, idPrefix) {
446
+ t007.toasts.keys().forEach((id) => (!arguments.length ? true : id.startsWith(idPrefix)) && base[action]?.(id, options));
447
+ },
448
+ getAll(base, idPrefix) {
449
+ return t007.toasts.values().filter((toast2) => !arguments.length ? true : toast2.id.startsWith(idPrefix));
450
+ }
451
+ };
452
+ var toaster = (defOptions = {}, idPrefix = "t007_toast_") => {
453
+ const defaults = () => ({ ...t007.TOAST_DEFAULT_OPTIONS, ...defOptions }), base = (render, options = {}) => new T007_Toast({
454
+ ...defaults(),
455
+ ...options,
456
+ id: render?.startsWith?.(idPrefix) ? render : options.id,
457
+ render: render?.startsWith?.(idPrefix) ? options.render : render,
458
+ idPrefix
459
+ }).id;
460
+ base.update = (id, options) => toasting.update(base, id, options);
461
+ ["info", "success", "warn", "error"].forEach((action) => toasting.message(base, defaults, action));
462
+ base.loading = (render, options) => toasting.loading(base, render, options);
463
+ base.promise = (promise, options) => toasting.promise(base, promise, options);
464
+ base.dismiss = (id, manner, timeElapsed) => toasting.dismiss(base, id, manner, timeElapsed);
465
+ base.dismissAll = (idPrefix2) => toasting.dismissAll(base, idPrefix2);
466
+ base.doForAll = (action, options, idPrefix2) => toasting.doForAll(base, action, options, idPrefix2);
467
+ base.getAll = (idPrefix2) => toasting.getAll(base, idPrefix2);
468
+ return base;
469
+ };
470
+ var toast = toaster();
471
+ var index_default = toast;
472
+ if (typeof window !== "undefined") {
473
+ window.t007 ??= {};
474
+ t007.toast = toast;
475
+ t007.toasting = toasting;
476
+ t007.toaster = toaster;
477
+ t007.toasts = /* @__PURE__ */ new Map();
478
+ t007.TOAST_DEFAULT_OPTIONS ??= {};
479
+ t007.TOAST_DURATIONS ??= {};
480
+ t007.TOAST_VIBRATIONS ??= {};
481
+ t007.TOAST_ICONS ??= {};
482
+ t007.TOAST_DEFAULT_OPTIONS.rootElement ??= document.body;
483
+ t007.TOAST_DEFAULT_OPTIONS.render ??= "";
484
+ t007.TOAST_DEFAULT_OPTIONS.type ??= "";
485
+ t007.TOAST_DEFAULT_OPTIONS.icon ??= true;
486
+ t007.TOAST_DEFAULT_OPTIONS.image ??= false;
487
+ t007.TOAST_DEFAULT_OPTIONS.autoClose ??= true;
488
+ t007.TOAST_DEFAULT_OPTIONS.position ??= "top-right";
489
+ t007.TOAST_DEFAULT_OPTIONS.isLoading ??= false;
490
+ t007.TOAST_DEFAULT_OPTIONS.closeButton ??= !/Mobi|Android|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
491
+ t007.TOAST_DEFAULT_OPTIONS.closeOnClick ??= false;
492
+ t007.TOAST_DEFAULT_OPTIONS.hideProgressBar ??= false;
493
+ t007.TOAST_DEFAULT_OPTIONS.pauseOnHover ??= true;
494
+ t007.TOAST_DEFAULT_OPTIONS.pauseOnFocusLoss ??= true;
495
+ t007.TOAST_DEFAULT_OPTIONS.dragToClose ??= true;
496
+ t007.TOAST_DEFAULT_OPTIONS.dragToClosePercent ??= 40;
497
+ t007.TOAST_DEFAULT_OPTIONS.dragToCloseDir ??= "x";
498
+ t007.TOAST_DEFAULT_OPTIONS.renotify ??= true;
499
+ t007.TOAST_DEFAULT_OPTIONS.vibrate ??= false;
500
+ t007.TOAST_DEFAULT_OPTIONS.animation ??= true;
501
+ t007.TOAST_DEFAULT_OPTIONS.newestOnTop ??= false;
502
+ t007.TOAST_DEFAULT_OPTIONS.maxToasts ??= 1e3;
503
+ t007.TOAST_DURATIONS.success ??= 2500;
504
+ t007.TOAST_DURATIONS.error ??= 4500;
505
+ t007.TOAST_DURATIONS.warning ??= 3500;
506
+ t007.TOAST_DURATIONS.info ??= 4e3;
507
+ t007.TOAST_VIBRATIONS.success ??= [100, 50, 100];
508
+ t007.TOAST_VIBRATIONS.warning ??= [300, 100, 300];
509
+ t007.TOAST_VIBRATIONS.error ??= [500, 200, 500];
510
+ t007.TOAST_VIBRATIONS.info ??= [200];
511
+ t007.TOAST_ICONS.success ??= `<svg class="no-css-fill" width="24" height="24" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" fill="#27ae60"/><path fill="none" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" d="M7 12l3 3l6-6"/></svg>`;
512
+ t007.TOAST_ICONS.error ??= `<svg class="no-css-fill" width="24" height="24" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" fill="#e74c3c"/><path fill="#fff" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" d="M8 8l8 8M16 8l-8 8"/></svg>`;
513
+ t007.TOAST_ICONS.warning ??= `<svg class="no-css-fill" width="24" height="24" viewBox="0 0 24 24"><path fill="#f1c40f" stroke="#f39c12" stroke-width="2" stroke-linejoin="round" d="M12 3L2.5 20.5A2 2 0 0 0 4.5 23h15a2 2 0 0 0 2-2.5L12 3z"/><circle cx="12" cy="17" r="1.5" fill="#fff"/><path fill="none" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" d="M12 8v6"/></svg>`;
514
+ t007.TOAST_ICONS.info ??= `<svg class="no-css-fill" width="24" height="24" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" fill="#3498db"/><path fill="none" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" d="M12 10v6"/><circle cx="12" cy="7" r="1.5" fill="#fff"/></svg>`;
515
+ t007.TOAST_ICONS.loading ??= `<svg class="no-css-fill" width="24" height="24" viewBox="0 0 16 16" fill="none" style="scale:0.75;"><g fill-rule="evenodd" clip-rule="evenodd"><path fill="whitesmoke" d="M8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8"/><path fill="gray" d="M7.25.75A.75.75 0 0 1 8 0a8 8 0 0 1 8 8 .75.75 0 0 1-1.5 0A6.5 6.5 0 0 0 8 1.5a.75.75 0 0 1-.75-.75"/></g><animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0" to="360" dur="600ms" repeatCount="indefinite"/></svg>`;
516
+ window.T007_TOAST_CSS_SRC ??= `https://unpkg.com/@t007/toast@latest/style.css`;
517
+ loadResource(T007_TOAST_CSS_SRC);
518
+ window.Toast ??= t007.toast;
519
+ console.log("%cT007 Toasts attached to window!", "color: darkturquoise");
520
+ }
521
+ })();