@t007/toast 0.0.6 → 0.0.10
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.
- package/README.md +8 -1
- package/dist/index.global.js +42 -23
- package/dist/index.js +18 -18
- package/dist/standalone.js +42 -23
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
- [Advanced Customization](#advanced-customization)
|
|
24
24
|
- [Author](#author)
|
|
25
25
|
- [Acknowledgments](#acknowledgments)
|
|
26
|
+
- [Star History](#star-history)
|
|
26
27
|
|
|
27
28
|
---
|
|
28
29
|
|
|
@@ -233,4 +234,10 @@ You can easily override the progress bar, animations, and container spacing by t
|
|
|
233
234
|
|
|
234
235
|
## Acknowledgments
|
|
235
236
|
|
|
236
|
-
Designed to bring native mobile OS notification physics to the web. Part of the `@t007` utility ecosystem.
|
|
237
|
+
Designed to bring native mobile OS notification physics to the web. Part of the `@t007` utility ecosystem.
|
|
238
|
+
|
|
239
|
+
## Star History
|
|
240
|
+
|
|
241
|
+
If you find this project useful, please consider giving it a star! ⭐
|
|
242
|
+
|
|
243
|
+
[](https://github.com/Tobi007-del/t007-tools)
|
package/dist/index.global.js
CHANGED
|
@@ -1,11 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
(() => {
|
|
3
|
+
// ../utils/node_modules/sia-reactor/dist/chunk-DW7Z7YEA.js
|
|
4
|
+
function isDef(val) {
|
|
5
|
+
return "undefined" !== typeof val;
|
|
6
|
+
}
|
|
7
|
+
function isObj(obj, checkArr = true) {
|
|
8
|
+
return "object" === typeof obj && obj !== null && (checkArr ? !Array.isArray(obj) : true);
|
|
9
|
+
}
|
|
10
|
+
|
|
3
11
|
// ../utils/dist/index.js
|
|
12
|
+
function isSym(val) {
|
|
13
|
+
return "symbol" === typeof val;
|
|
14
|
+
}
|
|
15
|
+
function isNum(val) {
|
|
16
|
+
return "number" === typeof val;
|
|
17
|
+
}
|
|
18
|
+
function isStr(val) {
|
|
19
|
+
return "string" === typeof val;
|
|
20
|
+
}
|
|
21
|
+
function isFunc(val) {
|
|
22
|
+
return "function" === typeof val;
|
|
23
|
+
}
|
|
4
24
|
function uid(prefix = "") {
|
|
5
25
|
return prefix + Date.now().toString(36) + "_" + performance.now().toString(36).replace(".", "") + "_" + Math.random().toString(36).slice(2);
|
|
6
26
|
}
|
|
7
27
|
function isSameURL(src1, src2) {
|
|
8
|
-
if (
|
|
28
|
+
if (!isStr(src1) || !isStr(src2) || !src1 || !src2) return false;
|
|
9
29
|
try {
|
|
10
30
|
const u1 = new URL(src1, window.location.href), u2 = new URL(src2, window.location.href);
|
|
11
31
|
return decodeURIComponent(u1.origin + u1.pathname) === decodeURIComponent(u2.origin + u2.pathname);
|
|
@@ -30,8 +50,8 @@
|
|
|
30
50
|
}
|
|
31
51
|
var VIRTUAL_RESOURCE = /* @__PURE__ */ Symbol.for("T007_VIRTUAL_RESOURCE");
|
|
32
52
|
function loadResource(req, type = "style", { module, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, attempts = 3, retryKey = false } = {}, w = window) {
|
|
33
|
-
w.t007._resourceCache ??= {};
|
|
34
|
-
if (req === VIRTUAL_RESOURCE ||
|
|
53
|
+
w.t007 ??= {}, w.t007._resourceCache ??= {};
|
|
54
|
+
if (req === VIRTUAL_RESOURCE || isSym(req)) return Promise.resolve();
|
|
35
55
|
const src = req;
|
|
36
56
|
if (w.t007._resourceCache[src]) return w.t007._resourceCache[src];
|
|
37
57
|
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;
|
|
@@ -64,8 +84,7 @@
|
|
|
64
84
|
while (proto && proto !== Object.prototype) {
|
|
65
85
|
for (const method of Object.getOwnPropertyNames(proto)) {
|
|
66
86
|
if (method === "constructor") continue;
|
|
67
|
-
if (
|
|
68
|
-
callback(method, owner);
|
|
87
|
+
if (isFunc(Object.getOwnPropertyDescriptor(proto, method)?.value)) callback(method, owner);
|
|
69
88
|
}
|
|
70
89
|
proto = Object.getPrototypeOf(proto);
|
|
71
90
|
}
|
|
@@ -75,7 +94,7 @@
|
|
|
75
94
|
owner2[method] = owner2[method].bind(owner2);
|
|
76
95
|
});
|
|
77
96
|
}
|
|
78
|
-
if (
|
|
97
|
+
if (isDef(window)) {
|
|
79
98
|
window.t007 ??= {};
|
|
80
99
|
t007.VIRTUAL_RESOURCE = VIRTUAL_RESOURCE;
|
|
81
100
|
window.T007_TOAST_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest`;
|
|
@@ -101,7 +120,7 @@
|
|
|
101
120
|
this.opts = { ...options };
|
|
102
121
|
this.id = this.opts.id ??= uid(this.opts.idPrefix ?? "t007_toast_");
|
|
103
122
|
t007.toasts.set(this.id, this);
|
|
104
|
-
|
|
123
|
+
!isNum(this.opts.delay) ? this.init() : this.queue.push(setTimeout(this.init, this.opts.delay));
|
|
105
124
|
this.update(this.opts);
|
|
106
125
|
}
|
|
107
126
|
init() {
|
|
@@ -110,11 +129,11 @@
|
|
|
110
129
|
this.destroyed = false;
|
|
111
130
|
}
|
|
112
131
|
update(options) {
|
|
113
|
-
if (!options ||
|
|
132
|
+
if (!options || isObj(options)) return this.opts.id;
|
|
114
133
|
try {
|
|
115
134
|
this.opts = { ...this.opts, ...options };
|
|
116
135
|
const run = () => Object.keys(options).forEach((key) => this[key] = options[key]);
|
|
117
|
-
|
|
136
|
+
!isNum(this.opts.delay) ? run() : this.queue.push(setTimeout(run, this.opts.delay));
|
|
118
137
|
this.opts.delay = null;
|
|
119
138
|
} catch (err) {
|
|
120
139
|
console.error("toast update failed:", err);
|
|
@@ -137,7 +156,7 @@
|
|
|
137
156
|
}
|
|
138
157
|
set bodyHTML(value) {
|
|
139
158
|
this.toastElement.querySelectorAll(".t007-toast > *:not(.t007-toast-cancel-button)").forEach((el) => el.remove());
|
|
140
|
-
this.toastElement.insertAdjacentHTML("afterbegin", `${value ?
|
|
159
|
+
this.toastElement.insertAdjacentHTML("afterbegin", `${value ? isFunc(value) ? value() : value : ""}`);
|
|
141
160
|
}
|
|
142
161
|
set render(value) {
|
|
143
162
|
const bodyText = () => this.toastElement.querySelector(".t007-toast-body-text");
|
|
@@ -145,7 +164,7 @@
|
|
|
145
164
|
this._setUpBodyHTML();
|
|
146
165
|
this.toastElement.querySelector(".t007-toast-body").prepend(bodyText() || createEl("p", { className: "t007-toast-body-text" }));
|
|
147
166
|
const text = bodyText();
|
|
148
|
-
text.innerHTML = text.dataset.render =
|
|
167
|
+
text.innerHTML = text.dataset.render = isFunc(value) ? value() : value;
|
|
149
168
|
} else bodyText()?.remove();
|
|
150
169
|
}
|
|
151
170
|
set actions(value) {
|
|
@@ -196,7 +215,7 @@
|
|
|
196
215
|
className: "t007-toast-icon t007-toast-loader"
|
|
197
216
|
})
|
|
198
217
|
);
|
|
199
|
-
loader().innerHTML =
|
|
218
|
+
loader().innerHTML = isStr(value) ? value : t007.TOAST_ICONS.loading;
|
|
200
219
|
} else {
|
|
201
220
|
loader()?.remove();
|
|
202
221
|
this.icon = this.opts.icon;
|
|
@@ -257,7 +276,7 @@
|
|
|
257
276
|
if (!this.#isPaused) {
|
|
258
277
|
this.#timeVisible += time - lastTime;
|
|
259
278
|
this.onTimeUpdate?.(this.#timeVisible);
|
|
260
|
-
if (
|
|
279
|
+
if (isNum(this.autoClose) && this.#timeVisible >= this.autoClose) return this._remove("smooth", true);
|
|
261
280
|
}
|
|
262
281
|
lastTime = time;
|
|
263
282
|
this.#autoCloseInterval = requestAnimationFrame(loop);
|
|
@@ -278,7 +297,7 @@
|
|
|
278
297
|
this.nprogress = 0;
|
|
279
298
|
cancelAnimationFrame(this.#progressInterval);
|
|
280
299
|
const loop = () => {
|
|
281
|
-
if (
|
|
300
|
+
if (isNum(this.autoClose) && !this.#isPaused) this.nprogress = 1 - this.#timeVisible / this.autoClose;
|
|
282
301
|
this.#progressInterval = requestAnimationFrame(loop);
|
|
283
302
|
};
|
|
284
303
|
if (!value) this.#progressInterval = requestAnimationFrame(loop);
|
|
@@ -325,7 +344,7 @@
|
|
|
325
344
|
this.toastElement.onpointerup = value ? this._handleToastPointerUp : null;
|
|
326
345
|
}
|
|
327
346
|
_handleToastPointerStart(e) {
|
|
328
|
-
if (
|
|
347
|
+
if (isStr(this._ptrType) && e.pointerType !== this._ptrType) return;
|
|
329
348
|
if (e.touches?.length > 1) return;
|
|
330
349
|
!e.target?.matches('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])') && this.toastElement.setPointerCapture(e.pointerId);
|
|
331
350
|
this.#isPaused = true;
|
|
@@ -353,7 +372,7 @@
|
|
|
353
372
|
this._ptrTicker = true;
|
|
354
373
|
}
|
|
355
374
|
_handleToastPointerUp(e) {
|
|
356
|
-
if (
|
|
375
|
+
if (isStr(this._ptrType) && e.pointerType !== this._ptrType) return;
|
|
357
376
|
cancelAnimationFrame(this._ptrRAF);
|
|
358
377
|
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");
|
|
359
378
|
this.#isPaused = this._ptrTicker = this._ptrDirSet = this._ptrDir = false;
|
|
@@ -420,24 +439,24 @@
|
|
|
420
439
|
type: ""
|
|
421
440
|
}),
|
|
422
441
|
promise(base, promise = new Promise((res, rej) => setTimeout(Math.round(Math.random()) ? res : rej, 3e3)), { pending, success, error } = {}) {
|
|
423
|
-
if (!promise ||
|
|
424
|
-
const NFC = (input, type) =>
|
|
442
|
+
if (!promise || isFunc(promise.then)) return console.error("toast.promise() requires a valid promise");
|
|
443
|
+
const NFC = (input, type) => isStr(input) ? { render: input, type } : isObj(input) ? { ...input, type } : { type };
|
|
425
444
|
const pendingConfig = NFC(pending);
|
|
426
445
|
const pendingToastId = base.loading(pendingConfig.render || "Promise pending...", { ...pendingConfig });
|
|
427
446
|
promise.then(
|
|
428
447
|
(response) => {
|
|
429
448
|
const successConfig = NFC(success || "Promise resolved", "success");
|
|
430
449
|
const { render, bodyHTML } = successConfig;
|
|
431
|
-
if (
|
|
432
|
-
if (
|
|
450
|
+
if (isFunc(render)) successConfig.render = (response2) => render(response2);
|
|
451
|
+
if (isFunc(bodyHTML)) successConfig.bodyHTML = (response2) => bodyHTML(response2);
|
|
433
452
|
base.success(pendingToastId, successConfig);
|
|
434
453
|
return response;
|
|
435
454
|
},
|
|
436
455
|
(err) => {
|
|
437
456
|
const errorConfig = NFC(error || "Promise rejected", "error");
|
|
438
457
|
const { render, bodyHTML } = errorConfig;
|
|
439
|
-
if (
|
|
440
|
-
if (
|
|
458
|
+
if (isFunc(render)) errorConfig.render = (err2) => render(err2);
|
|
459
|
+
if (isFunc(bodyHTML)) errorConfig.bodyHTML = (err2) => bodyHTML(err2);
|
|
441
460
|
base.error(pendingToastId, errorConfig);
|
|
442
461
|
return Promise.reject(err);
|
|
443
462
|
}
|
|
@@ -477,7 +496,7 @@
|
|
|
477
496
|
};
|
|
478
497
|
var toast = toaster();
|
|
479
498
|
var index_default = toast;
|
|
480
|
-
if (
|
|
499
|
+
if (isDef(window)) {
|
|
481
500
|
t007.toast = toast;
|
|
482
501
|
t007.toasting = toasting;
|
|
483
502
|
t007.toaster = toaster;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/index.js
|
|
2
|
-
import { clamp, uid, bindAllMethods, createEl, loadResource } from "@t007/utils";
|
|
2
|
+
import { isDef, isStr, isNum, isObj, isFunc, clamp, uid, bindAllMethods, createEl, loadResource } from "@t007/utils";
|
|
3
3
|
var T007_Toast = class {
|
|
4
4
|
#autoCloseInterval;
|
|
5
5
|
#progressInterval;
|
|
@@ -14,7 +14,7 @@ var T007_Toast = class {
|
|
|
14
14
|
this.opts = { ...options };
|
|
15
15
|
this.id = this.opts.id ??= uid(this.opts.idPrefix ?? "t007_toast_");
|
|
16
16
|
t007.toasts.set(this.id, this);
|
|
17
|
-
|
|
17
|
+
!isNum(this.opts.delay) ? this.init() : this.queue.push(setTimeout(this.init, this.opts.delay));
|
|
18
18
|
this.update(this.opts);
|
|
19
19
|
}
|
|
20
20
|
init() {
|
|
@@ -23,11 +23,11 @@ var T007_Toast = class {
|
|
|
23
23
|
this.destroyed = false;
|
|
24
24
|
}
|
|
25
25
|
update(options) {
|
|
26
|
-
if (!options ||
|
|
26
|
+
if (!options || isObj(options)) return this.opts.id;
|
|
27
27
|
try {
|
|
28
28
|
this.opts = { ...this.opts, ...options };
|
|
29
29
|
const run = () => Object.keys(options).forEach((key) => this[key] = options[key]);
|
|
30
|
-
|
|
30
|
+
!isNum(this.opts.delay) ? run() : this.queue.push(setTimeout(run, this.opts.delay));
|
|
31
31
|
this.opts.delay = null;
|
|
32
32
|
} catch (err) {
|
|
33
33
|
console.error("toast update failed:", err);
|
|
@@ -50,7 +50,7 @@ var T007_Toast = class {
|
|
|
50
50
|
}
|
|
51
51
|
set bodyHTML(value) {
|
|
52
52
|
this.toastElement.querySelectorAll(".t007-toast > *:not(.t007-toast-cancel-button)").forEach((el) => el.remove());
|
|
53
|
-
this.toastElement.insertAdjacentHTML("afterbegin", `${value ?
|
|
53
|
+
this.toastElement.insertAdjacentHTML("afterbegin", `${value ? isFunc(value) ? value() : value : ""}`);
|
|
54
54
|
}
|
|
55
55
|
set render(value) {
|
|
56
56
|
const bodyText = () => this.toastElement.querySelector(".t007-toast-body-text");
|
|
@@ -58,7 +58,7 @@ var T007_Toast = class {
|
|
|
58
58
|
this._setUpBodyHTML();
|
|
59
59
|
this.toastElement.querySelector(".t007-toast-body").prepend(bodyText() || createEl("p", { className: "t007-toast-body-text" }));
|
|
60
60
|
const text = bodyText();
|
|
61
|
-
text.innerHTML = text.dataset.render =
|
|
61
|
+
text.innerHTML = text.dataset.render = isFunc(value) ? value() : value;
|
|
62
62
|
} else bodyText()?.remove();
|
|
63
63
|
}
|
|
64
64
|
set actions(value) {
|
|
@@ -109,7 +109,7 @@ var T007_Toast = class {
|
|
|
109
109
|
className: "t007-toast-icon t007-toast-loader"
|
|
110
110
|
})
|
|
111
111
|
);
|
|
112
|
-
loader().innerHTML =
|
|
112
|
+
loader().innerHTML = isStr(value) ? value : t007.TOAST_ICONS.loading;
|
|
113
113
|
} else {
|
|
114
114
|
loader()?.remove();
|
|
115
115
|
this.icon = this.opts.icon;
|
|
@@ -170,7 +170,7 @@ var T007_Toast = class {
|
|
|
170
170
|
if (!this.#isPaused) {
|
|
171
171
|
this.#timeVisible += time - lastTime;
|
|
172
172
|
this.onTimeUpdate?.(this.#timeVisible);
|
|
173
|
-
if (
|
|
173
|
+
if (isNum(this.autoClose) && this.#timeVisible >= this.autoClose) return this._remove("smooth", true);
|
|
174
174
|
}
|
|
175
175
|
lastTime = time;
|
|
176
176
|
this.#autoCloseInterval = requestAnimationFrame(loop);
|
|
@@ -191,7 +191,7 @@ var T007_Toast = class {
|
|
|
191
191
|
this.nprogress = 0;
|
|
192
192
|
cancelAnimationFrame(this.#progressInterval);
|
|
193
193
|
const loop = () => {
|
|
194
|
-
if (
|
|
194
|
+
if (isNum(this.autoClose) && !this.#isPaused) this.nprogress = 1 - this.#timeVisible / this.autoClose;
|
|
195
195
|
this.#progressInterval = requestAnimationFrame(loop);
|
|
196
196
|
};
|
|
197
197
|
if (!value) this.#progressInterval = requestAnimationFrame(loop);
|
|
@@ -238,7 +238,7 @@ var T007_Toast = class {
|
|
|
238
238
|
this.toastElement.onpointerup = value ? this._handleToastPointerUp : null;
|
|
239
239
|
}
|
|
240
240
|
_handleToastPointerStart(e) {
|
|
241
|
-
if (
|
|
241
|
+
if (isStr(this._ptrType) && e.pointerType !== this._ptrType) return;
|
|
242
242
|
if (e.touches?.length > 1) return;
|
|
243
243
|
!e.target?.matches('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])') && this.toastElement.setPointerCapture(e.pointerId);
|
|
244
244
|
this.#isPaused = true;
|
|
@@ -266,7 +266,7 @@ var T007_Toast = class {
|
|
|
266
266
|
this._ptrTicker = true;
|
|
267
267
|
}
|
|
268
268
|
_handleToastPointerUp(e) {
|
|
269
|
-
if (
|
|
269
|
+
if (isStr(this._ptrType) && e.pointerType !== this._ptrType) return;
|
|
270
270
|
cancelAnimationFrame(this._ptrRAF);
|
|
271
271
|
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");
|
|
272
272
|
this.#isPaused = this._ptrTicker = this._ptrDirSet = this._ptrDir = false;
|
|
@@ -333,24 +333,24 @@ var toasting = {
|
|
|
333
333
|
type: ""
|
|
334
334
|
}),
|
|
335
335
|
promise(base, promise = new Promise((res, rej) => setTimeout(Math.round(Math.random()) ? res : rej, 3e3)), { pending, success, error } = {}) {
|
|
336
|
-
if (!promise ||
|
|
337
|
-
const NFC = (input, type) =>
|
|
336
|
+
if (!promise || isFunc(promise.then)) return console.error("toast.promise() requires a valid promise");
|
|
337
|
+
const NFC = (input, type) => isStr(input) ? { render: input, type } : isObj(input) ? { ...input, type } : { type };
|
|
338
338
|
const pendingConfig = NFC(pending);
|
|
339
339
|
const pendingToastId = base.loading(pendingConfig.render || "Promise pending...", { ...pendingConfig });
|
|
340
340
|
promise.then(
|
|
341
341
|
(response) => {
|
|
342
342
|
const successConfig = NFC(success || "Promise resolved", "success");
|
|
343
343
|
const { render, bodyHTML } = successConfig;
|
|
344
|
-
if (
|
|
345
|
-
if (
|
|
344
|
+
if (isFunc(render)) successConfig.render = (response2) => render(response2);
|
|
345
|
+
if (isFunc(bodyHTML)) successConfig.bodyHTML = (response2) => bodyHTML(response2);
|
|
346
346
|
base.success(pendingToastId, successConfig);
|
|
347
347
|
return response;
|
|
348
348
|
},
|
|
349
349
|
(err) => {
|
|
350
350
|
const errorConfig = NFC(error || "Promise rejected", "error");
|
|
351
351
|
const { render, bodyHTML } = errorConfig;
|
|
352
|
-
if (
|
|
353
|
-
if (
|
|
352
|
+
if (isFunc(render)) errorConfig.render = (err2) => render(err2);
|
|
353
|
+
if (isFunc(bodyHTML)) errorConfig.bodyHTML = (err2) => bodyHTML(err2);
|
|
354
354
|
base.error(pendingToastId, errorConfig);
|
|
355
355
|
return Promise.reject(err);
|
|
356
356
|
}
|
|
@@ -390,7 +390,7 @@ var toaster = (defOptions = {}, idPrefix = "t007_toast_") => {
|
|
|
390
390
|
};
|
|
391
391
|
var toast = toaster();
|
|
392
392
|
var index_default = toast;
|
|
393
|
-
if (
|
|
393
|
+
if (isDef(window)) {
|
|
394
394
|
t007.toast = toast;
|
|
395
395
|
t007.toasting = toasting;
|
|
396
396
|
t007.toaster = toaster;
|
package/dist/standalone.js
CHANGED
|
@@ -1,9 +1,29 @@
|
|
|
1
|
+
// ../utils/node_modules/sia-reactor/dist/chunk-DW7Z7YEA.js
|
|
2
|
+
function isDef(val) {
|
|
3
|
+
return "undefined" !== typeof val;
|
|
4
|
+
}
|
|
5
|
+
function isObj(obj, checkArr = true) {
|
|
6
|
+
return "object" === typeof obj && obj !== null && (checkArr ? !Array.isArray(obj) : true);
|
|
7
|
+
}
|
|
8
|
+
|
|
1
9
|
// ../utils/dist/index.js
|
|
10
|
+
function isSym(val) {
|
|
11
|
+
return "symbol" === typeof val;
|
|
12
|
+
}
|
|
13
|
+
function isNum(val) {
|
|
14
|
+
return "number" === typeof val;
|
|
15
|
+
}
|
|
16
|
+
function isStr(val) {
|
|
17
|
+
return "string" === typeof val;
|
|
18
|
+
}
|
|
19
|
+
function isFunc(val) {
|
|
20
|
+
return "function" === typeof val;
|
|
21
|
+
}
|
|
2
22
|
function uid(prefix = "") {
|
|
3
23
|
return prefix + Date.now().toString(36) + "_" + performance.now().toString(36).replace(".", "") + "_" + Math.random().toString(36).slice(2);
|
|
4
24
|
}
|
|
5
25
|
function isSameURL(src1, src2) {
|
|
6
|
-
if (
|
|
26
|
+
if (!isStr(src1) || !isStr(src2) || !src1 || !src2) return false;
|
|
7
27
|
try {
|
|
8
28
|
const u1 = new URL(src1, window.location.href), u2 = new URL(src2, window.location.href);
|
|
9
29
|
return decodeURIComponent(u1.origin + u1.pathname) === decodeURIComponent(u2.origin + u2.pathname);
|
|
@@ -28,8 +48,8 @@ function assignEl(el, props, dataset, styles) {
|
|
|
28
48
|
}
|
|
29
49
|
var VIRTUAL_RESOURCE = /* @__PURE__ */ Symbol.for("T007_VIRTUAL_RESOURCE");
|
|
30
50
|
function loadResource(req, type = "style", { module, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, attempts = 3, retryKey = false } = {}, w = window) {
|
|
31
|
-
w.t007._resourceCache ??= {};
|
|
32
|
-
if (req === VIRTUAL_RESOURCE ||
|
|
51
|
+
w.t007 ??= {}, w.t007._resourceCache ??= {};
|
|
52
|
+
if (req === VIRTUAL_RESOURCE || isSym(req)) return Promise.resolve();
|
|
33
53
|
const src = req;
|
|
34
54
|
if (w.t007._resourceCache[src]) return w.t007._resourceCache[src];
|
|
35
55
|
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;
|
|
@@ -62,8 +82,7 @@ function onAllMethods(owner, callback) {
|
|
|
62
82
|
while (proto && proto !== Object.prototype) {
|
|
63
83
|
for (const method of Object.getOwnPropertyNames(proto)) {
|
|
64
84
|
if (method === "constructor") continue;
|
|
65
|
-
if (
|
|
66
|
-
callback(method, owner);
|
|
85
|
+
if (isFunc(Object.getOwnPropertyDescriptor(proto, method)?.value)) callback(method, owner);
|
|
67
86
|
}
|
|
68
87
|
proto = Object.getPrototypeOf(proto);
|
|
69
88
|
}
|
|
@@ -73,7 +92,7 @@ function bindAllMethods(owner) {
|
|
|
73
92
|
owner2[method] = owner2[method].bind(owner2);
|
|
74
93
|
});
|
|
75
94
|
}
|
|
76
|
-
if (
|
|
95
|
+
if (isDef(window)) {
|
|
77
96
|
window.t007 ??= {};
|
|
78
97
|
t007.VIRTUAL_RESOURCE = VIRTUAL_RESOURCE;
|
|
79
98
|
window.T007_TOAST_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest`;
|
|
@@ -99,7 +118,7 @@ var T007_Toast = class {
|
|
|
99
118
|
this.opts = { ...options };
|
|
100
119
|
this.id = this.opts.id ??= uid(this.opts.idPrefix ?? "t007_toast_");
|
|
101
120
|
t007.toasts.set(this.id, this);
|
|
102
|
-
|
|
121
|
+
!isNum(this.opts.delay) ? this.init() : this.queue.push(setTimeout(this.init, this.opts.delay));
|
|
103
122
|
this.update(this.opts);
|
|
104
123
|
}
|
|
105
124
|
init() {
|
|
@@ -108,11 +127,11 @@ var T007_Toast = class {
|
|
|
108
127
|
this.destroyed = false;
|
|
109
128
|
}
|
|
110
129
|
update(options) {
|
|
111
|
-
if (!options ||
|
|
130
|
+
if (!options || isObj(options)) return this.opts.id;
|
|
112
131
|
try {
|
|
113
132
|
this.opts = { ...this.opts, ...options };
|
|
114
133
|
const run = () => Object.keys(options).forEach((key) => this[key] = options[key]);
|
|
115
|
-
|
|
134
|
+
!isNum(this.opts.delay) ? run() : this.queue.push(setTimeout(run, this.opts.delay));
|
|
116
135
|
this.opts.delay = null;
|
|
117
136
|
} catch (err) {
|
|
118
137
|
console.error("toast update failed:", err);
|
|
@@ -135,7 +154,7 @@ var T007_Toast = class {
|
|
|
135
154
|
}
|
|
136
155
|
set bodyHTML(value) {
|
|
137
156
|
this.toastElement.querySelectorAll(".t007-toast > *:not(.t007-toast-cancel-button)").forEach((el) => el.remove());
|
|
138
|
-
this.toastElement.insertAdjacentHTML("afterbegin", `${value ?
|
|
157
|
+
this.toastElement.insertAdjacentHTML("afterbegin", `${value ? isFunc(value) ? value() : value : ""}`);
|
|
139
158
|
}
|
|
140
159
|
set render(value) {
|
|
141
160
|
const bodyText = () => this.toastElement.querySelector(".t007-toast-body-text");
|
|
@@ -143,7 +162,7 @@ var T007_Toast = class {
|
|
|
143
162
|
this._setUpBodyHTML();
|
|
144
163
|
this.toastElement.querySelector(".t007-toast-body").prepend(bodyText() || createEl("p", { className: "t007-toast-body-text" }));
|
|
145
164
|
const text = bodyText();
|
|
146
|
-
text.innerHTML = text.dataset.render =
|
|
165
|
+
text.innerHTML = text.dataset.render = isFunc(value) ? value() : value;
|
|
147
166
|
} else bodyText()?.remove();
|
|
148
167
|
}
|
|
149
168
|
set actions(value) {
|
|
@@ -194,7 +213,7 @@ var T007_Toast = class {
|
|
|
194
213
|
className: "t007-toast-icon t007-toast-loader"
|
|
195
214
|
})
|
|
196
215
|
);
|
|
197
|
-
loader().innerHTML =
|
|
216
|
+
loader().innerHTML = isStr(value) ? value : t007.TOAST_ICONS.loading;
|
|
198
217
|
} else {
|
|
199
218
|
loader()?.remove();
|
|
200
219
|
this.icon = this.opts.icon;
|
|
@@ -255,7 +274,7 @@ var T007_Toast = class {
|
|
|
255
274
|
if (!this.#isPaused) {
|
|
256
275
|
this.#timeVisible += time - lastTime;
|
|
257
276
|
this.onTimeUpdate?.(this.#timeVisible);
|
|
258
|
-
if (
|
|
277
|
+
if (isNum(this.autoClose) && this.#timeVisible >= this.autoClose) return this._remove("smooth", true);
|
|
259
278
|
}
|
|
260
279
|
lastTime = time;
|
|
261
280
|
this.#autoCloseInterval = requestAnimationFrame(loop);
|
|
@@ -276,7 +295,7 @@ var T007_Toast = class {
|
|
|
276
295
|
this.nprogress = 0;
|
|
277
296
|
cancelAnimationFrame(this.#progressInterval);
|
|
278
297
|
const loop = () => {
|
|
279
|
-
if (
|
|
298
|
+
if (isNum(this.autoClose) && !this.#isPaused) this.nprogress = 1 - this.#timeVisible / this.autoClose;
|
|
280
299
|
this.#progressInterval = requestAnimationFrame(loop);
|
|
281
300
|
};
|
|
282
301
|
if (!value) this.#progressInterval = requestAnimationFrame(loop);
|
|
@@ -323,7 +342,7 @@ var T007_Toast = class {
|
|
|
323
342
|
this.toastElement.onpointerup = value ? this._handleToastPointerUp : null;
|
|
324
343
|
}
|
|
325
344
|
_handleToastPointerStart(e) {
|
|
326
|
-
if (
|
|
345
|
+
if (isStr(this._ptrType) && e.pointerType !== this._ptrType) return;
|
|
327
346
|
if (e.touches?.length > 1) return;
|
|
328
347
|
!e.target?.matches('button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])') && this.toastElement.setPointerCapture(e.pointerId);
|
|
329
348
|
this.#isPaused = true;
|
|
@@ -351,7 +370,7 @@ var T007_Toast = class {
|
|
|
351
370
|
this._ptrTicker = true;
|
|
352
371
|
}
|
|
353
372
|
_handleToastPointerUp(e) {
|
|
354
|
-
if (
|
|
373
|
+
if (isStr(this._ptrType) && e.pointerType !== this._ptrType) return;
|
|
355
374
|
cancelAnimationFrame(this._ptrRAF);
|
|
356
375
|
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");
|
|
357
376
|
this.#isPaused = this._ptrTicker = this._ptrDirSet = this._ptrDir = false;
|
|
@@ -418,24 +437,24 @@ var toasting = {
|
|
|
418
437
|
type: ""
|
|
419
438
|
}),
|
|
420
439
|
promise(base, promise = new Promise((res, rej) => setTimeout(Math.round(Math.random()) ? res : rej, 3e3)), { pending, success, error } = {}) {
|
|
421
|
-
if (!promise ||
|
|
422
|
-
const NFC = (input, type) =>
|
|
440
|
+
if (!promise || isFunc(promise.then)) return console.error("toast.promise() requires a valid promise");
|
|
441
|
+
const NFC = (input, type) => isStr(input) ? { render: input, type } : isObj(input) ? { ...input, type } : { type };
|
|
423
442
|
const pendingConfig = NFC(pending);
|
|
424
443
|
const pendingToastId = base.loading(pendingConfig.render || "Promise pending...", { ...pendingConfig });
|
|
425
444
|
promise.then(
|
|
426
445
|
(response) => {
|
|
427
446
|
const successConfig = NFC(success || "Promise resolved", "success");
|
|
428
447
|
const { render, bodyHTML } = successConfig;
|
|
429
|
-
if (
|
|
430
|
-
if (
|
|
448
|
+
if (isFunc(render)) successConfig.render = (response2) => render(response2);
|
|
449
|
+
if (isFunc(bodyHTML)) successConfig.bodyHTML = (response2) => bodyHTML(response2);
|
|
431
450
|
base.success(pendingToastId, successConfig);
|
|
432
451
|
return response;
|
|
433
452
|
},
|
|
434
453
|
(err) => {
|
|
435
454
|
const errorConfig = NFC(error || "Promise rejected", "error");
|
|
436
455
|
const { render, bodyHTML } = errorConfig;
|
|
437
|
-
if (
|
|
438
|
-
if (
|
|
456
|
+
if (isFunc(render)) errorConfig.render = (err2) => render(err2);
|
|
457
|
+
if (isFunc(bodyHTML)) errorConfig.bodyHTML = (err2) => bodyHTML(err2);
|
|
439
458
|
base.error(pendingToastId, errorConfig);
|
|
440
459
|
return Promise.reject(err);
|
|
441
460
|
}
|
|
@@ -475,7 +494,7 @@ var toaster = (defOptions = {}, idPrefix = "t007_toast_") => {
|
|
|
475
494
|
};
|
|
476
495
|
var toast = toaster();
|
|
477
496
|
var src_default = toast;
|
|
478
|
-
if (
|
|
497
|
+
if (isDef(window)) {
|
|
479
498
|
t007.toast = toast;
|
|
480
499
|
t007.toasting = toasting;
|
|
481
500
|
t007.toaster = toaster;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t007/toast",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.10",
|
|
4
4
|
"description": "A lightweight, pure JS toast system.",
|
|
5
5
|
"author": "Oketade Oluwatobiloba <tobioketade007@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -55,6 +55,6 @@
|
|
|
55
55
|
"promise"
|
|
56
56
|
],
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@t007/utils": "
|
|
58
|
+
"@t007/utils": "^0.0.11"
|
|
59
59
|
}
|
|
60
60
|
}
|