@t007/input 0.0.1 → 0.0.2
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/dist/index.global.js +32 -24
- package/dist/index.js +125 -3
- package/package.json +9 -5
- package/src/ts/types/index.d.ts +0 -3
package/dist/index.global.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
(function () {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
3
4
|
// ../utils/dist/index.js
|
|
4
5
|
function isSameURL(src1, src2) {
|
|
5
6
|
if (typeof src1 !== "string" || typeof src2 !== "string" || !src1 || !src2) return false;
|
|
@@ -20,7 +21,7 @@
|
|
|
20
21
|
for (const k of Object.keys(styles)) if (styles[k] !== void 0) el.style[k] = styles[k];
|
|
21
22
|
}
|
|
22
23
|
function loadResource(src, type = "style", { module, media, crossOrigin, integrity, referrerPolicy, nonce, fetchPriority, attempts = 3, retryKey = false } = {}, w = window) {
|
|
23
|
-
w.t007
|
|
24
|
+
w.t007._resourceCache ??= {};
|
|
24
25
|
if (w.t007._resourceCache[src]) return w.t007._resourceCache[src];
|
|
25
26
|
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
27
|
if (existing) return w.t007._resourceCache[src] = Promise.resolve(existing);
|
|
@@ -44,20 +45,20 @@
|
|
|
44
45
|
});
|
|
45
46
|
return w.t007._resourceCache[src];
|
|
46
47
|
}
|
|
47
|
-
var _SCROLLERS = /* @__PURE__ */ new WeakMap();
|
|
48
|
-
var _SCROLLER_R_OBSERVER = typeof window !== "undefined" ? new ResizeObserver((entries) => entries.forEach(({ target }) => _SCROLLERS.get(target)?.update())) : null;
|
|
49
|
-
var _SCROLLER_M_OBSERVER = typeof window !== "undefined" ? new MutationObserver((entries) => {
|
|
50
|
-
const els = /* @__PURE__ */ new Set();
|
|
51
|
-
for (const entry of entries) {
|
|
52
|
-
let node = entry.target instanceof Element ? entry.target : null;
|
|
53
|
-
while (node && !_SCROLLERS.has(node)) node = node.parentElement;
|
|
54
|
-
if (node) els.add(node);
|
|
55
|
-
}
|
|
56
|
-
for (const el of els) _SCROLLERS.get(el)?.update();
|
|
57
|
-
}) : null;
|
|
58
48
|
function initScrollAssist(el, { pxPerSecond = 80, assistClassName = "tmg-video-controls-scroll-assist", vertical = true, horizontal = true } = {}) {
|
|
49
|
+
t007._scrollers ??= /* @__PURE__ */ new WeakMap();
|
|
50
|
+
t007._scroller_r_observer ??= new ResizeObserver((entries) => entries.forEach(({ target }) => t007._scrollers.get(target)?.update()));
|
|
51
|
+
t007._scroller_m_observer ??= new MutationObserver((entries) => {
|
|
52
|
+
const els = /* @__PURE__ */ new Set();
|
|
53
|
+
for (const entry of entries) {
|
|
54
|
+
let node = entry.target instanceof Element ? entry.target : null;
|
|
55
|
+
while (node && !t007._scrollers.has(node)) node = node.parentElement;
|
|
56
|
+
if (node) els.add(node);
|
|
57
|
+
}
|
|
58
|
+
for (const el2 of els) t007._scrollers.get(el2)?.update();
|
|
59
|
+
});
|
|
59
60
|
const parent = el?.parentElement;
|
|
60
|
-
if (!parent ||
|
|
61
|
+
if (!parent || t007._scrollers.has(el)) return;
|
|
61
62
|
const assist = {};
|
|
62
63
|
let scrollId = null, last = performance.now(), assistWidth = 20, assistHeight = 20;
|
|
63
64
|
const update = () => {
|
|
@@ -101,20 +102,28 @@
|
|
|
101
102
|
if (horizontal) ["left", "right"].forEach(addAssist);
|
|
102
103
|
if (vertical) ["up", "down"].forEach(addAssist);
|
|
103
104
|
el.addEventListener("scroll", update);
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
105
|
+
t007._scroller_r_observer.observe(el);
|
|
106
|
+
t007._scroller_m_observer.observe(el, { childList: true, subtree: true, characterData: true });
|
|
107
|
+
t007._scrollers.set(el, {
|
|
107
108
|
update,
|
|
108
109
|
destroy() {
|
|
109
110
|
stop();
|
|
110
111
|
el.removeEventListener("scroll", update);
|
|
111
|
-
|
|
112
|
-
|
|
112
|
+
t007._scroller_r_observer.unobserve(el);
|
|
113
|
+
t007._scrollers.delete(el);
|
|
113
114
|
Object.values(assist).forEach((a) => a.remove());
|
|
114
115
|
}
|
|
115
116
|
});
|
|
116
|
-
update();
|
|
117
|
-
|
|
117
|
+
return update(), t007._scrollers.get(el);
|
|
118
|
+
}
|
|
119
|
+
if (typeof window !== "undefined") {
|
|
120
|
+
window.t007 ??= {};
|
|
121
|
+
window.T007_TOAST_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest`;
|
|
122
|
+
window.T007_INPUT_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/input@latest`;
|
|
123
|
+
window.T007_DIALOG_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/dialog@latest`;
|
|
124
|
+
window.T007_TOAST_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest/dist/style.css`;
|
|
125
|
+
window.T007_INPUT_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/input@latest/dist/style.css`;
|
|
126
|
+
window.T007_DIALOG_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/dialog@latest/dist/style.css`;
|
|
118
127
|
}
|
|
119
128
|
|
|
120
129
|
// src/index.js
|
|
@@ -412,15 +421,14 @@
|
|
|
412
421
|
}
|
|
413
422
|
};
|
|
414
423
|
if (typeof window !== "undefined") {
|
|
415
|
-
window.t007 ??= {};
|
|
416
424
|
t007.FM = T007_Form_Manager;
|
|
417
425
|
t007.field = t007.FM.field;
|
|
418
426
|
t007.handleFormValidation = t007.FM.handleFormValidation;
|
|
419
|
-
window.T007_INPUT_CSS_SRC ??= `https://unpkg.com/@t007/input@latest/style.css`;
|
|
420
427
|
window.field ??= t007.field;
|
|
421
428
|
window.handleFormValidation ??= t007.handleFormValidation;
|
|
422
429
|
console.log("%cT007 Input helpers attached to window!", "color: darkturquoise");
|
|
423
430
|
loadResource(T007_INPUT_CSS_SRC);
|
|
424
431
|
t007.FM.init();
|
|
425
432
|
}
|
|
433
|
+
|
|
426
434
|
})();
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,129 @@
|
|
|
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 initScrollAssist(el, { pxPerSecond = 80, assistClassName = "tmg-video-controls-scroll-assist", vertical = true, horizontal = true } = {}) {
|
|
46
|
+
t007._scrollers ??= /* @__PURE__ */ new WeakMap();
|
|
47
|
+
t007._scroller_r_observer ??= new ResizeObserver((entries) => entries.forEach(({ target }) => t007._scrollers.get(target)?.update()));
|
|
48
|
+
t007._scroller_m_observer ??= new MutationObserver((entries) => {
|
|
49
|
+
const els = /* @__PURE__ */ new Set();
|
|
50
|
+
for (const entry of entries) {
|
|
51
|
+
let node = entry.target instanceof Element ? entry.target : null;
|
|
52
|
+
while (node && !t007._scrollers.has(node)) node = node.parentElement;
|
|
53
|
+
if (node) els.add(node);
|
|
54
|
+
}
|
|
55
|
+
for (const el2 of els) t007._scrollers.get(el2)?.update();
|
|
56
|
+
});
|
|
57
|
+
const parent = el?.parentElement;
|
|
58
|
+
if (!parent || t007._scrollers.has(el)) return;
|
|
59
|
+
const assist = {};
|
|
60
|
+
let scrollId = null, last = performance.now(), assistWidth = 20, assistHeight = 20;
|
|
61
|
+
const update = () => {
|
|
62
|
+
const hasInteractive = !!parent.querySelector('button, a[href], input, select, textarea, [contenteditable="true"], [tabindex]:not([tabindex="-1"])');
|
|
63
|
+
if (horizontal) {
|
|
64
|
+
const w = assist.left?.offsetWidth || assistWidth, check = hasInteractive ? el.clientWidth < w * 2 : false;
|
|
65
|
+
assist.left.style.display = check ? "none" : el.scrollLeft > 0 ? "block" : "none";
|
|
66
|
+
assist.right.style.display = check ? "none" : el.scrollLeft + el.clientWidth < el.scrollWidth - 1 ? "block" : "none";
|
|
67
|
+
assistWidth = w;
|
|
68
|
+
}
|
|
69
|
+
if (vertical) {
|
|
70
|
+
const h = assist.up?.offsetHeight || assistHeight, check = hasInteractive ? el.clientHeight < h * 2 : false;
|
|
71
|
+
assist.up.style.display = check ? "none" : el.scrollTop > 0 ? "block" : "none";
|
|
72
|
+
assist.down.style.display = check ? "none" : el.scrollTop + el.clientHeight < el.scrollHeight - 1 ? "block" : "none";
|
|
73
|
+
assistHeight = h;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const scroll = (dir) => {
|
|
77
|
+
const frame = () => {
|
|
78
|
+
const now = performance.now(), dt = now - last;
|
|
79
|
+
last = now;
|
|
80
|
+
const d = pxPerSecond * dt / 1e3;
|
|
81
|
+
if (dir === "left") el.scrollLeft = Math.max(0, el.scrollLeft - d);
|
|
82
|
+
if (dir === "right") el.scrollLeft = Math.min(el.scrollWidth - el.clientWidth, el.scrollLeft + d);
|
|
83
|
+
if (dir === "up") el.scrollTop = Math.max(0, el.scrollTop - d);
|
|
84
|
+
if (dir === "down") el.scrollTop = Math.min(el.scrollHeight - el.clientHeight, el.scrollTop + d);
|
|
85
|
+
scrollId = requestAnimationFrame(frame);
|
|
86
|
+
};
|
|
87
|
+
last = performance.now();
|
|
88
|
+
frame();
|
|
89
|
+
};
|
|
90
|
+
const stop = () => (cancelAnimationFrame(scrollId ?? 0), scrollId = null);
|
|
91
|
+
const addAssist = (dir) => {
|
|
92
|
+
const div = createEl("div", { className: assistClassName }, { scrollDirection: dir }, { display: "none" });
|
|
93
|
+
if (!div) return;
|
|
94
|
+
["pointerenter", "dragenter"].forEach((evt) => div.addEventListener(evt, () => scroll(dir)));
|
|
95
|
+
["pointerleave", "pointerup", "pointercancel", "dragleave", "dragend"].forEach((evt) => div.addEventListener(evt, stop));
|
|
96
|
+
dir === "left" || dir === "up" ? parent.insertBefore(div, el) : parent.append(div);
|
|
97
|
+
assist[dir] = div;
|
|
98
|
+
};
|
|
99
|
+
if (horizontal) ["left", "right"].forEach(addAssist);
|
|
100
|
+
if (vertical) ["up", "down"].forEach(addAssist);
|
|
101
|
+
el.addEventListener("scroll", update);
|
|
102
|
+
t007._scroller_r_observer.observe(el);
|
|
103
|
+
t007._scroller_m_observer.observe(el, { childList: true, subtree: true, characterData: true });
|
|
104
|
+
t007._scrollers.set(el, {
|
|
105
|
+
update,
|
|
106
|
+
destroy() {
|
|
107
|
+
stop();
|
|
108
|
+
el.removeEventListener("scroll", update);
|
|
109
|
+
t007._scroller_r_observer.unobserve(el);
|
|
110
|
+
t007._scrollers.delete(el);
|
|
111
|
+
Object.values(assist).forEach((a) => a.remove());
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return update(), t007._scrollers.get(el);
|
|
115
|
+
}
|
|
116
|
+
if (typeof window !== "undefined") {
|
|
117
|
+
window.t007 ??= {};
|
|
118
|
+
window.T007_TOAST_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest`;
|
|
119
|
+
window.T007_INPUT_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/input@latest`;
|
|
120
|
+
window.T007_DIALOG_JS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/dialog@latest`;
|
|
121
|
+
window.T007_TOAST_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/toast@latest/dist/style.css`;
|
|
122
|
+
window.T007_INPUT_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/input@latest/dist/style.css`;
|
|
123
|
+
window.T007_DIALOG_CSS_SRC ??= `https://cdn.jsdelivr.net/npm/@t007/dialog@latest/dist/style.css`;
|
|
124
|
+
}
|
|
125
|
+
|
|
1
126
|
// src/index.js
|
|
2
|
-
import { createEl, loadResource, initScrollAssist } from "@t007/utils";
|
|
3
127
|
var T007_Form_Manager = {
|
|
4
128
|
forms: document.getElementsByClassName("t007-input-form"),
|
|
5
129
|
violationKeys: ["valueMissing", "typeMismatch", "patternMismatch", "stepMismatch", "tooShort", "tooLong", "rangeUnderflow", "rangeOverflow", "badInput", "customError"],
|
|
@@ -294,11 +418,9 @@ var T007_Form_Manager = {
|
|
|
294
418
|
}
|
|
295
419
|
};
|
|
296
420
|
if (typeof window !== "undefined") {
|
|
297
|
-
window.t007 ??= {};
|
|
298
421
|
t007.FM = T007_Form_Manager;
|
|
299
422
|
t007.field = t007.FM.field;
|
|
300
423
|
t007.handleFormValidation = t007.FM.handleFormValidation;
|
|
301
|
-
window.T007_INPUT_CSS_SRC ??= `https://unpkg.com/@t007/input@latest/style.css`;
|
|
302
424
|
window.field ??= t007.field;
|
|
303
425
|
window.handleFormValidation ??= t007.handleFormValidation;
|
|
304
426
|
console.log("%cT007 Input helpers attached to window!", "color: darkturquoise");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@t007/input",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "A lightweight, pure JS input system.",
|
|
5
5
|
"author": "Oketade Oluwatobiloba <tobioketade007@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,7 +17,6 @@
|
|
|
17
17
|
"main": "./dist/index.js",
|
|
18
18
|
"module": "./dist/index.js",
|
|
19
19
|
"browser": "./dist/index.global.js",
|
|
20
|
-
"unpkg": "./dist/index.global.js",
|
|
21
20
|
"types": "./src/ts/types/index.d.ts",
|
|
22
21
|
"style": "./src/css/index.css",
|
|
23
22
|
"sideEffects": [
|
|
@@ -32,14 +31,19 @@
|
|
|
32
31
|
"./style.css": "./src/css/index.css"
|
|
33
32
|
},
|
|
34
33
|
"scripts": {
|
|
35
|
-
"build": "tsup src/index.js --format esm,iife --clean"
|
|
34
|
+
"build": "tsup src/index.js --format esm,iife --clean --treeshake"
|
|
35
|
+
},
|
|
36
|
+
"tsup": {
|
|
37
|
+
"noExternal": [
|
|
38
|
+
"@t007/utils"
|
|
39
|
+
]
|
|
36
40
|
},
|
|
37
41
|
"files": [
|
|
38
42
|
"dist",
|
|
39
43
|
"./src/ts/types/index.d.ts",
|
|
40
44
|
"./src/css/index.css"
|
|
41
45
|
],
|
|
42
|
-
"keywords": [
|
|
46
|
+
"keywords": [
|
|
43
47
|
"input",
|
|
44
48
|
"form",
|
|
45
49
|
"form-validation",
|
|
@@ -54,4 +58,4 @@
|
|
|
54
58
|
"dependencies": {
|
|
55
59
|
"@t007/utils": "*"
|
|
56
60
|
}
|
|
57
|
-
}
|
|
61
|
+
}
|