@t007/dialog 0.0.2 → 0.0.3

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.
@@ -1,6 +1,5 @@
1
- (function (exports) {
2
- 'use strict';
3
-
1
+ "use strict";
2
+ (() => {
4
3
  // ../utils/dist/index.js
5
4
  function isSameURL(src1, src2) {
6
5
  if (typeof src1 !== "string" || typeof src2 !== "string" || !src1 || !src2) return false;
@@ -11,14 +10,20 @@
11
10
  return src1.replace(/\\/g, "/").split("?")[0].trim() === src2.replace(/\\/g, "/").split("?")[0].trim();
12
11
  }
13
12
  }
14
- function createEl(tag, props = {}, dataset = {}, styles = {}) {
15
- return assignEl(tag ? document?.createElement(tag) : void 0, props, dataset, styles) ?? null;
13
+ function createEl(tag, props, dataset, styles, el = tag ? document?.createElement(tag) : null) {
14
+ return assignEl(el, props, dataset, styles), el;
16
15
  }
17
- function assignEl(el, props = {}, dataset = {}, styles = {}) {
16
+ function assignEl(el, props, dataset, styles) {
18
17
  if (!el) return;
19
- for (const k of Object.keys(props)) if (props[k] !== void 0) el[k] = props[k];
20
- for (const k of Object.keys(dataset)) if (dataset[k] !== void 0) el.dataset[k] = String(dataset[k]);
21
- for (const k of Object.keys(styles)) if (styles[k] !== void 0) el.style[k] = styles[k];
18
+ if (props) {
19
+ for (const k of Object.keys(props)) if (props[k] !== void 0) el[k] = props[k];
20
+ }
21
+ if (dataset) {
22
+ for (const k of Object.keys(dataset)) if (dataset[k] !== void 0) el.dataset[k] = String(dataset[k]);
23
+ }
24
+ if (styles) {
25
+ for (const k of Object.keys(styles)) if (styles[k] !== void 0) el.style[k] = styles[k];
26
+ }
22
27
  }
23
28
  function loadResource(src, type = "style", { module, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, attempts = 3, retryKey = false } = {}, w = window) {
24
29
  w.t007._resourceCache ??= {};
@@ -205,11 +210,4 @@
205
210
  window.Prompt ??= t007.prompt;
206
211
  console.log("%cT007 Dialogs attached to window!", "color: darkturquoise");
207
212
  }
208
-
209
- exports.alert = alert;
210
- exports.confirm = confirm;
211
- exports.prompt = prompt;
212
-
213
- return exports;
214
-
215
- })({});
213
+ })();
package/dist/index.js CHANGED
@@ -1,74 +1,5 @@
1
- // ../utils/dist/index.js
2
- function isSameURL(src1, src2) {
3
- if (typeof src1 !== "string" || typeof src2 !== "string" || !src1 || !src2) return false;
4
- try {
5
- const u1 = new URL(src1, window.location.href), u2 = new URL(src2, window.location.href);
6
- return decodeURIComponent(u1.origin + u1.pathname) === decodeURIComponent(u2.origin + u2.pathname);
7
- } catch {
8
- return src1.replace(/\\/g, "/").split("?")[0].trim() === src2.replace(/\\/g, "/").split("?")[0].trim();
9
- }
10
- }
11
- function createEl(tag, props = {}, dataset = {}, styles = {}) {
12
- return assignEl(tag ? document?.createElement(tag) : void 0, props, dataset, styles) ?? null;
13
- }
14
- function assignEl(el, props = {}, dataset = {}, styles = {}) {
15
- if (!el) return;
16
- for (const k of Object.keys(props)) if (props[k] !== void 0) el[k] = props[k];
17
- for (const k of Object.keys(dataset)) if (dataset[k] !== void 0) el.dataset[k] = String(dataset[k]);
18
- for (const k of Object.keys(styles)) if (styles[k] !== void 0) el.style[k] = styles[k];
19
- }
20
- function loadResource(src, type = "style", { module, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, attempts = 3, retryKey = false } = {}, w = window) {
21
- w.t007._resourceCache ??= {};
22
- if (w.t007._resourceCache[src]) return w.t007._resourceCache[src];
23
- 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;
24
- if (existing) return w.t007._resourceCache[src] = Promise.resolve(existing);
25
- w.t007._resourceCache[src] = new Promise((resolve, reject) => {
26
- (function tryLoad(remaining, el) {
27
- const onerror = () => {
28
- el?.remove?.();
29
- if (remaining > 1) {
30
- setTimeout(tryLoad, 1e3, remaining - 1);
31
- console.warn(`Retrying ${type} load (${attempts - remaining + 1}): ${src}...`);
32
- } else {
33
- delete w.t007._resourceCache[src];
34
- reject(new Error(`${type} load failed after ${attempts} attempts: ${src}`));
35
- }
36
- };
37
- const url = retryKey && remaining < attempts ? `${src}${src.includes("?") ? "&" : "?"}_${retryKey}=${Date.now()}` : src;
38
- 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 }) || "");
39
- 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 }) || "");
40
- else reject(new Error(`Unsupported resource type: ${type}`));
41
- })(attempts);
42
- });
43
- return w.t007._resourceCache[src];
44
- }
45
- function onAllMethods(owner, callback) {
46
- let proto = owner;
47
- while (proto && proto !== Object.prototype) {
48
- for (const method of Object.getOwnPropertyNames(proto)) {
49
- if (method === "constructor") continue;
50
- if ("function" !== typeof Object.getOwnPropertyDescriptor(proto, method)?.value) continue;
51
- callback(method, owner);
52
- }
53
- proto = Object.getPrototypeOf(proto);
54
- }
55
- }
56
- function bindAllMethods(owner) {
57
- onAllMethods(owner, (method, owner2) => {
58
- owner2[method] = owner2[method].bind(owner2);
59
- });
60
- }
61
- if (typeof window !== "undefined") {
62
- window.t007 ??= {};
63
- window.T007_TOAST_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest`;
64
- window.T007_INPUT_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/input@latest`;
65
- window.T007_DIALOG_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/dialog@latest`;
66
- window.T007_TOAST_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest/dist/style.css`;
67
- window.T007_INPUT_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/input@latest/dist/style.css`;
68
- window.T007_DIALOG_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/dialog@latest/dist/style.css`;
69
- }
70
-
71
1
  // src/index.js
2
+ import { bindAllMethods, createEl, loadResource } from "@t007/utils";
72
3
  var T007_Dialog = class {
73
4
  dialog;
74
5
  confirmBtn;
@@ -202,5 +133,8 @@ if (typeof window !== "undefined") {
202
133
  window.Prompt ??= t007.prompt;
203
134
  console.log("%cT007 Dialogs attached to window!", "color: darkturquoise");
204
135
  }
205
-
206
- export { alert, confirm, prompt };
136
+ export {
137
+ alert,
138
+ confirm,
139
+ prompt
140
+ };
@@ -0,0 +1,215 @@
1
+ // ../utils/dist/index.js
2
+ function isSameURL(src1, src2) {
3
+ if (typeof src1 !== "string" || typeof src2 !== "string" || !src1 || !src2) return false;
4
+ try {
5
+ const u1 = new URL(src1, window.location.href), u2 = new URL(src2, window.location.href);
6
+ return decodeURIComponent(u1.origin + u1.pathname) === decodeURIComponent(u2.origin + u2.pathname);
7
+ } catch {
8
+ return src1.replace(/\\/g, "/").split("?")[0].trim() === src2.replace(/\\/g, "/").split("?")[0].trim();
9
+ }
10
+ }
11
+ function createEl(tag, props, dataset, styles, el = tag ? document?.createElement(tag) : null) {
12
+ return assignEl(el, props, dataset, styles), el;
13
+ }
14
+ function assignEl(el, props, dataset, styles) {
15
+ if (!el) return;
16
+ if (props) {
17
+ for (const k of Object.keys(props)) if (props[k] !== void 0) el[k] = props[k];
18
+ }
19
+ if (dataset) {
20
+ for (const k of Object.keys(dataset)) if (dataset[k] !== void 0) el.dataset[k] = String(dataset[k]);
21
+ }
22
+ if (styles) {
23
+ for (const k of Object.keys(styles)) if (styles[k] !== void 0) el.style[k] = styles[k];
24
+ }
25
+ }
26
+ function loadResource(src, type = "style", { module, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, attempts = 3, retryKey = false } = {}, w = window) {
27
+ w.t007._resourceCache ??= {};
28
+ if (w.t007._resourceCache[src]) return w.t007._resourceCache[src];
29
+ 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;
30
+ if (existing) return w.t007._resourceCache[src] = Promise.resolve(existing);
31
+ w.t007._resourceCache[src] = new Promise((resolve, reject) => {
32
+ (function tryLoad(remaining, el) {
33
+ const onerror = () => {
34
+ el?.remove?.();
35
+ if (remaining > 1) {
36
+ setTimeout(tryLoad, 1e3, remaining - 1);
37
+ console.warn(`Retrying ${type} load (${attempts - remaining + 1}): ${src}...`);
38
+ } else {
39
+ delete w.t007._resourceCache[src];
40
+ reject(new Error(`${type} load failed after ${attempts} attempts: ${src}`));
41
+ }
42
+ };
43
+ const url = retryKey && remaining < attempts ? `${src}${src.includes("?") ? "&" : "?"}_${retryKey}=${Date.now()}` : src;
44
+ 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 }) || "");
45
+ 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 }) || "");
46
+ else reject(new Error(`Unsupported resource type: ${type}`));
47
+ })(attempts);
48
+ });
49
+ return w.t007._resourceCache[src];
50
+ }
51
+ function onAllMethods(owner, callback) {
52
+ let proto = owner;
53
+ while (proto && proto !== Object.prototype) {
54
+ for (const method of Object.getOwnPropertyNames(proto)) {
55
+ if (method === "constructor") continue;
56
+ if ("function" !== typeof Object.getOwnPropertyDescriptor(proto, method)?.value) continue;
57
+ callback(method, owner);
58
+ }
59
+ proto = Object.getPrototypeOf(proto);
60
+ }
61
+ }
62
+ function bindAllMethods(owner) {
63
+ onAllMethods(owner, (method, owner2) => {
64
+ owner2[method] = owner2[method].bind(owner2);
65
+ });
66
+ }
67
+ if (typeof window !== "undefined") {
68
+ window.t007 ??= {};
69
+ window.T007_TOAST_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest`;
70
+ window.T007_INPUT_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/input@latest`;
71
+ window.T007_DIALOG_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/dialog@latest`;
72
+ window.T007_TOAST_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest/dist/style.css`;
73
+ window.T007_INPUT_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/input@latest/dist/style.css`;
74
+ window.T007_DIALOG_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/dialog@latest/dist/style.css`;
75
+ }
76
+
77
+ // src/index.js
78
+ var T007_Dialog = class {
79
+ dialog;
80
+ confirmBtn;
81
+ cancelBtn;
82
+ constructor(resolve) {
83
+ bindAllMethods(this);
84
+ this.resolve = resolve;
85
+ document.body.append(
86
+ this.dialog = createEl("dialog", {
87
+ className: "t007-dialog",
88
+ closedBy: "any"
89
+ })
90
+ );
91
+ this.dialog.addEventListener("cancel", this.cancel);
92
+ }
93
+ show() {
94
+ this.dialog.showModal();
95
+ this.confirmBtn.focus();
96
+ }
97
+ remove() {
98
+ this.dialog.close();
99
+ this.dialog.remove();
100
+ }
101
+ confirm() {
102
+ this.remove();
103
+ this.resolve(true);
104
+ }
105
+ cancel() {
106
+ this.remove();
107
+ this.resolve(false);
108
+ }
109
+ };
110
+ var T007_Alert_Dialog = class extends T007_Dialog {
111
+ constructor({ message, resolve, options }) {
112
+ super(resolve);
113
+ this.render(message, options);
114
+ }
115
+ render(message, options = {}) {
116
+ this.dialog.innerHTML = `
117
+ <div class="t007-dialog-top-section">
118
+ <p class="t007-dialog-question">${message}</p>
119
+ </div>
120
+ <div class="t007-dialog-bottom-section">
121
+ <button class="t007-dialog-confirm-button" type="button">${options.confirmText || "OK"}</button>
122
+ </div>
123
+ `;
124
+ this.confirmBtn = this.dialog.querySelector(".t007-dialog-confirm-button");
125
+ this.confirmBtn.addEventListener("click", this.confirm);
126
+ this.show();
127
+ }
128
+ };
129
+ var T007_Confirm_Dialog = class extends T007_Dialog {
130
+ constructor({ question, resolve, options }) {
131
+ super(resolve);
132
+ this.render(question, options);
133
+ }
134
+ render(question, options = {}) {
135
+ this.dialog.innerHTML = `
136
+ <div class="t007-dialog-top-section">
137
+ <p class="t007-dialog-question">${question}</p>
138
+ </div>
139
+ <div class="t007-dialog-bottom-section">
140
+ <button class="t007-dialog-confirm-button" type="button">${options.confirmText || "OK"}</button>
141
+ <button class="t007-dialog-cancel-button" type="button">${options.cancelText || "Cancel"}</button>
142
+ </div>
143
+ `;
144
+ this.confirmBtn = this.dialog.querySelector(".t007-dialog-confirm-button");
145
+ this.cancelBtn = this.dialog.querySelector(".t007-dialog-cancel-button");
146
+ this.confirmBtn.addEventListener("click", this.confirm);
147
+ this.cancelBtn.addEventListener("click", this.cancel);
148
+ this.show();
149
+ }
150
+ };
151
+ var T007_Prompt_Dialog = class extends T007_Dialog {
152
+ constructor({ question, defaultValue, resolve, options }) {
153
+ super(resolve);
154
+ this.render(question, defaultValue, options);
155
+ }
156
+ async render(question, defaultValue, options = {}) {
157
+ await loadResource(window.T007_INPUT_JS_SRC, "script");
158
+ options.value = defaultValue;
159
+ this.dialog.innerHTML = `
160
+ <form class="t007-input-form" novalidate>
161
+ <div class="t007-dialog-top-section">
162
+ <p class="t007-dialog-question">${question}</p>
163
+ </div>
164
+ <div class="t007-dialog-bottom-section">
165
+ <button class="t007-dialog-confirm-button" type="submit">${options.confirmText || "OK"}</button>
166
+ <button class="t007-dialog-cancel-button" type="button">${options.cancelText || "Cancel"}</button>
167
+ </div>
168
+ </form>
169
+ `;
170
+ (this.form = this.dialog.querySelector("form")).lastElementChild.insertAdjacentElement("beforebegin", t007.FM?.field(options) || createEl("input", options));
171
+ this.form.onSubmit = this.confirm;
172
+ this.confirmBtn = this.dialog.querySelector(".t007-dialog-confirm-button");
173
+ this.cancelBtn = this.dialog.querySelector(".t007-dialog-cancel-button");
174
+ this.cancelBtn.addEventListener("click", this.cancel);
175
+ t007.FM?.handleFormValidation?.(this.form);
176
+ this.show();
177
+ }
178
+ show() {
179
+ this.dialog.showModal();
180
+ this.form.elements[0]?.focus();
181
+ this.form.elements[0]?.select?.();
182
+ }
183
+ confirm() {
184
+ this.remove();
185
+ this.resolve(this.form.elements[0]?.value);
186
+ }
187
+ cancel() {
188
+ this.remove();
189
+ this.resolve(null);
190
+ }
191
+ };
192
+ function alert(message, options) {
193
+ return new Promise((resolve) => new T007_Alert_Dialog({ message, resolve, options }));
194
+ }
195
+ function confirm(question, options) {
196
+ return new Promise((resolve) => new T007_Confirm_Dialog({ question, resolve, options }));
197
+ }
198
+ function prompt(question, defaultValue, options) {
199
+ return new Promise((resolve) => new T007_Prompt_Dialog({ question, defaultValue, resolve, options }));
200
+ }
201
+ if (typeof window !== "undefined") {
202
+ t007.alert = alert;
203
+ t007.confirm = confirm;
204
+ t007.prompt = prompt;
205
+ loadResource(T007_DIALOG_CSS_SRC), loadResource(window.T007_INPUT_JS_SRC, "script");
206
+ window.Alert ??= t007.alert;
207
+ window.Confirm ??= t007.confirm;
208
+ window.Prompt ??= t007.prompt;
209
+ console.log("%cT007 Dialogs attached to window!", "color: darkturquoise");
210
+ }
211
+ export {
212
+ alert,
213
+ confirm,
214
+ prompt
215
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@t007/dialog",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "A lightweight, pure JS dialog system.",
5
5
  "author": "Oketade Oluwatobiloba <tobioketade007@gmail.com>",
6
6
  "license": "MIT",
@@ -16,7 +16,8 @@
16
16
  "type": "module",
17
17
  "main": "./dist/index.js",
18
18
  "module": "./dist/index.js",
19
- "browser": "./dist/index.global.js",
19
+ "unpkg": "./dist/index.global.js",
20
+ "jsdelivr": "./dist/index.global.js",
20
21
  "types": "./src/ts/types/index.d.ts",
21
22
  "style": "./src/css/index.css",
22
23
  "sideEffects": [
@@ -26,17 +27,14 @@
26
27
  ".": {
27
28
  "types": "./src/ts/types/index.d.ts",
28
29
  "import": "./dist/index.js",
29
- "default": "./dist/index.global.js"
30
+ "default": "./dist/index.js"
30
31
  },
32
+ "./standalone": "./dist/standalone.js",
33
+ "./global": "./dist/index.global.js",
31
34
  "./style.css": "./src/css/index.css"
32
35
  },
33
36
  "scripts": {
34
- "build": "tsup src/index.js --format esm,iife --clean --treeshake"
35
- },
36
- "tsup": {
37
- "noExternal": [
38
- "@t007/utils"
39
- ]
37
+ "build": "tsup --config ../../tsup.config.ts"
40
38
  },
41
39
  "files": [
42
40
  "dist",