@t007/dialog 0.0.1 → 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.
@@ -10,17 +10,23 @@
10
10
  return src1.replace(/\\/g, "/").split("?")[0].trim() === src2.replace(/\\/g, "/").split("?")[0].trim();
11
11
  }
12
12
  }
13
- function createEl(tag, props = {}, dataset = {}, styles = {}) {
14
- 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;
15
15
  }
16
- function assignEl(el, props = {}, dataset = {}, styles = {}) {
16
+ function assignEl(el, props, dataset, styles) {
17
17
  if (!el) return;
18
- for (const k of Object.keys(props)) if (props[k] !== void 0) el[k] = props[k];
19
- for (const k of Object.keys(dataset)) if (dataset[k] !== void 0) el.dataset[k] = String(dataset[k]);
20
- 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
+ }
21
27
  }
22
28
  function loadResource(src, type = "style", { module, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, attempts = 3, retryKey = false } = {}, w = window) {
23
- w.t007 ??= {}, w.t007._resourceCache ??= {};
29
+ w.t007._resourceCache ??= {};
24
30
  if (w.t007._resourceCache[src]) return w.t007._resourceCache[src];
25
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;
26
32
  if (existing) return w.t007._resourceCache[src] = Promise.resolve(existing);
@@ -60,17 +66,15 @@
60
66
  owner2[method] = owner2[method].bind(owner2);
61
67
  });
62
68
  }
63
- var _SCROLLERS = /* @__PURE__ */ new WeakMap();
64
- var _SCROLLER_R_OBSERVER = typeof window !== "undefined" ? new ResizeObserver((entries) => entries.forEach(({ target }) => _SCROLLERS.get(target)?.update())) : null;
65
- var _SCROLLER_M_OBSERVER = typeof window !== "undefined" ? new MutationObserver((entries) => {
66
- const els = /* @__PURE__ */ new Set();
67
- for (const entry of entries) {
68
- let node = entry.target instanceof Element ? entry.target : null;
69
- while (node && !_SCROLLERS.has(node)) node = node.parentElement;
70
- if (node) els.add(node);
71
- }
72
- for (const el of els) _SCROLLERS.get(el)?.update();
73
- }) : null;
69
+ if (typeof window !== "undefined") {
70
+ window.t007 ??= {};
71
+ window.T007_TOAST_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest`;
72
+ window.T007_INPUT_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/input@latest`;
73
+ window.T007_DIALOG_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/dialog@latest`;
74
+ window.T007_TOAST_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest/dist/style.css`;
75
+ window.T007_INPUT_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/input@latest/dist/style.css`;
76
+ window.T007_DIALOG_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/dialog@latest/dist/style.css`;
77
+ }
74
78
 
75
79
  // src/index.js
76
80
  var T007_Dialog = class {
@@ -197,9 +201,6 @@
197
201
  return new Promise((resolve) => new T007_Prompt_Dialog({ question, defaultValue, resolve, options }));
198
202
  }
199
203
  if (typeof window !== "undefined") {
200
- window.t007 ??= {};
201
- window.T007_DIALOG_CSS_SRC ??= `https://unpkg.com/@t007/dialog@latest/style.css`;
202
- window.T007_INPUT_JS_SRC ??= `https://unpkg.com/@t007/input@latest`;
203
204
  t007.alert = alert;
204
205
  t007.confirm = confirm;
205
206
  t007.prompt = prompt;
package/dist/index.js CHANGED
@@ -124,9 +124,6 @@ function prompt(question, defaultValue, options) {
124
124
  return new Promise((resolve) => new T007_Prompt_Dialog({ question, defaultValue, resolve, options }));
125
125
  }
126
126
  if (typeof window !== "undefined") {
127
- window.t007 ??= {};
128
- window.T007_DIALOG_CSS_SRC ??= `https://unpkg.com/@t007/dialog@latest/style.css`;
129
- window.T007_INPUT_JS_SRC ??= `https://unpkg.com/@t007/input@latest`;
130
127
  t007.alert = alert;
131
128
  t007.confirm = confirm;
132
129
  t007.prompt = prompt;
@@ -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.1",
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,8 +16,8 @@
16
16
  "type": "module",
17
17
  "main": "./dist/index.js",
18
18
  "module": "./dist/index.js",
19
- "browser": "./dist/index.global.js",
20
19
  "unpkg": "./dist/index.global.js",
20
+ "jsdelivr": "./dist/index.global.js",
21
21
  "types": "./src/ts/types/index.d.ts",
22
22
  "style": "./src/css/index.css",
23
23
  "sideEffects": [
@@ -27,19 +27,21 @@
27
27
  ".": {
28
28
  "types": "./src/ts/types/index.d.ts",
29
29
  "import": "./dist/index.js",
30
- "default": "./dist/index.global.js"
30
+ "default": "./dist/index.js"
31
31
  },
32
+ "./standalone": "./dist/standalone.js",
33
+ "./global": "./dist/index.global.js",
32
34
  "./style.css": "./src/css/index.css"
33
35
  },
34
36
  "scripts": {
35
- "build": "tsup src/index.js --format esm,iife --clean"
37
+ "build": "tsup --config ../../tsup.config.ts"
36
38
  },
37
39
  "files": [
38
40
  "dist",
39
41
  "./src/ts/types/index.d.ts",
40
42
  "./src/css/index.css"
41
43
  ],
42
- "keywords": [
44
+ "keywords": [
43
45
  "t007",
44
46
  "dialog",
45
47
  "modal",
@@ -24,11 +24,6 @@ declare global {
24
24
  }
25
25
 
26
26
  interface Window {
27
- t007: T007Namespace;
28
-
29
- T007_DIALOG_CSS_SRC?: string;
30
- T007_INPUT_JS_SRC?: string;
31
-
32
27
  Alert?: T007Namespace["alert"];
33
28
  Confirm?: T007Namespace["confirm"];
34
29
  Prompt?: T007Namespace["prompt"];