@zero-dependency/dom 0.12.0 → 1.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.
- package/README.md +20 -0
- package/dist/html.d.ts +3 -2
- package/dist/index.cjs.js +1 -133
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.es.js +81 -99
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -139
- package/dist/index.umd.js.map +1 -1
- package/dist/location-observer.d.ts +13 -0
- package/dist/mutation-observers.d.ts +4 -0
- package/package.json +12 -12
- package/dist/locationObserver.d.ts +0 -11
- package/dist/observeElement.d.ts +0 -1
- package/dist/waitElement.d.ts +0 -1
package/README.md
CHANGED
|
@@ -19,3 +19,23 @@ pnpm add @zero-dependency/dom
|
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
## Usage
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
import { el, observeElement, waitElement } from '@zero-dependency/dom'
|
|
25
|
+
|
|
26
|
+
// createElement
|
|
27
|
+
const element = el('div', { class: 'foo' }, 'Hello World')
|
|
28
|
+
document.body.appendChild(element)
|
|
29
|
+
|
|
30
|
+
// observeElement
|
|
31
|
+
observeElement(element, (mutation, observer) => {
|
|
32
|
+
console.log(mutation.target.textContent) // 'hello world'
|
|
33
|
+
observer.disconnect()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
element.textContent = element.textContent.toLowerCase() // 'hello world'
|
|
37
|
+
|
|
38
|
+
// waitElement
|
|
39
|
+
const el = await waitElement('div.card')
|
|
40
|
+
console.log(el) // <div class="card">...</div>
|
|
41
|
+
```
|
package/dist/html.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
type Attributes<T extends keyof HTMLElementTagNameMap> = Partial<{
|
|
2
2
|
style: Partial<CSSStyleDeclaration>;
|
|
3
3
|
} & Omit<HTMLElementTagNameMap[T], 'style'>>;
|
|
4
|
-
|
|
4
|
+
type Children = (string | Node | HTMLElement)[];
|
|
5
5
|
export declare function el<T extends keyof HTMLElementTagNameMap>(tag: T, attributes?: Children | Attributes<T>, ...children: Children): HTMLElementTagNameMap[T];
|
|
6
6
|
export declare function text(str: string): Text;
|
|
7
7
|
export declare function nbsp(): Text;
|
|
8
8
|
export declare function domReady(): Promise<void>;
|
|
9
|
+
export declare function isDisabled(element: HTMLElement): boolean;
|
|
9
10
|
export {};
|
package/dist/index.cjs.js
CHANGED
|
@@ -1,134 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
4
|
-
|
|
5
|
-
function el(tag, attributes, ...children) {
|
|
6
|
-
const el2 = document.createElement(tag);
|
|
7
|
-
if (typeof attributes === "string") {
|
|
8
|
-
el2.append(text(attributes));
|
|
9
|
-
} else if (Array.isArray(attributes)) {
|
|
10
|
-
el2.append(...attributes);
|
|
11
|
-
} else {
|
|
12
|
-
Object.assign(el2, attributes);
|
|
13
|
-
Object.assign(el2.style, attributes?.style);
|
|
14
|
-
}
|
|
15
|
-
if (children.length) {
|
|
16
|
-
el2.append(...children);
|
|
17
|
-
}
|
|
18
|
-
return el2;
|
|
19
|
-
}
|
|
20
|
-
function text(str) {
|
|
21
|
-
return document.createTextNode(str);
|
|
22
|
-
}
|
|
23
|
-
function nbsp() {
|
|
24
|
-
return text("\xA0");
|
|
25
|
-
}
|
|
26
|
-
async function domReady() {
|
|
27
|
-
return new Promise((resolve) => {
|
|
28
|
-
if (document.readyState == "loading") {
|
|
29
|
-
document.addEventListener("DOMContentLoaded", () => resolve(), {
|
|
30
|
-
once: true
|
|
31
|
-
});
|
|
32
|
-
} else {
|
|
33
|
-
resolve();
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function observeElement(el, callback, options) {
|
|
39
|
-
const observe = new MutationObserver((mutations, observer) => {
|
|
40
|
-
for (const mutation of mutations) {
|
|
41
|
-
callback(mutation, observer);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
observe.observe(el, {
|
|
45
|
-
childList: true,
|
|
46
|
-
subtree: true,
|
|
47
|
-
...options
|
|
48
|
-
});
|
|
49
|
-
return observe;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function waitElement(selector, target = document.documentElement) {
|
|
53
|
-
return new Promise((resolve) => {
|
|
54
|
-
function resolveElement() {
|
|
55
|
-
const el = target.querySelector(selector);
|
|
56
|
-
if (el) {
|
|
57
|
-
resolve(el);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
observeElement(target, (_, observer) => {
|
|
61
|
-
resolveElement();
|
|
62
|
-
observer.disconnect();
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
class n {
|
|
68
|
-
#t = {};
|
|
69
|
-
on(t, e) {
|
|
70
|
-
const s = this.#t[t];
|
|
71
|
-
return s ? s.push(e) : this.#t[t] = [e], this;
|
|
72
|
-
}
|
|
73
|
-
addListener(t, e) {
|
|
74
|
-
return this.on(t, e);
|
|
75
|
-
}
|
|
76
|
-
once(t, e) {
|
|
77
|
-
const s = (...i) => {
|
|
78
|
-
this.off(t, s), e(...i);
|
|
79
|
-
};
|
|
80
|
-
return this.on(t, s), this;
|
|
81
|
-
}
|
|
82
|
-
emit(t, ...e) {
|
|
83
|
-
const s = this.#t[t] || [];
|
|
84
|
-
for (let i = 0; i < s.length; i++)
|
|
85
|
-
s[i](...e);
|
|
86
|
-
return Boolean(s.length);
|
|
87
|
-
}
|
|
88
|
-
off(t, e) {
|
|
89
|
-
return this.#t[t] && (this.#t[t] = this.#t[t].filter((s) => s !== e)), this;
|
|
90
|
-
}
|
|
91
|
-
removeListener(t, e) {
|
|
92
|
-
return this.off(t, e);
|
|
93
|
-
}
|
|
94
|
-
removeAllListeners(t) {
|
|
95
|
-
return t ? delete this.#t[t] : this.#t = {}, this;
|
|
96
|
-
}
|
|
97
|
-
eventNames() {
|
|
98
|
-
return Reflect.ownKeys(this.#t);
|
|
99
|
-
}
|
|
100
|
-
listeners(t) {
|
|
101
|
-
return this.#t[t];
|
|
102
|
-
}
|
|
103
|
-
listenerCount(t) {
|
|
104
|
-
return this.#t[t]?.length ?? 0;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
class LocationObserver extends n {
|
|
109
|
-
constructor() {
|
|
110
|
-
super();
|
|
111
|
-
const { history, location } = window;
|
|
112
|
-
const { pushState, replaceState } = history;
|
|
113
|
-
history.pushState = (...args) => {
|
|
114
|
-
pushState.apply(history, args);
|
|
115
|
-
this.emit("pushState", location, args[0]);
|
|
116
|
-
};
|
|
117
|
-
history.replaceState = (...args) => {
|
|
118
|
-
replaceState.apply(history, args);
|
|
119
|
-
this.emit("replaceState", location, args[0]);
|
|
120
|
-
};
|
|
121
|
-
window.addEventListener("popstate", ({ state }) => {
|
|
122
|
-
this.emit("popState", location, state);
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
exports.LocationObserver = LocationObserver;
|
|
128
|
-
exports.domReady = domReady;
|
|
129
|
-
exports.el = el;
|
|
130
|
-
exports.nbsp = nbsp;
|
|
131
|
-
exports.observeElement = observeElement;
|
|
132
|
-
exports.text = text;
|
|
133
|
-
exports.waitElement = waitElement;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function l(s,e,...n){const t=document.createElement(s);return typeof e=="string"?t.append(i(e)):Array.isArray(e)?t.append(...e):(Object.assign(t,e),Object.assign(t.style,e?.style)),n.length&&t.append(...n),t}function i(s){return document.createTextNode(s)}function a(){return i(" ")}async function d(){return new Promise(s=>{document.readyState=="loading"?document.addEventListener("DOMContentLoaded",()=>s(),{once:!0}):s()})}function h(s){return!!s.getAttribute("disabled")||!!s.getAttribute("aria-disabled")}class f{#e={};on(e,n){const t=this.#e[e];return t?t.push(n):this.#e[e]=[n],this}addListener(e,n){return this.on(e,n)}once(e,n){const t=(...r)=>{this.off(e,t),n(...r)};return this.on(e,t),this}emit(e,...n){const t=this.#e[e]||[];for(let r=0;r<t.length;r++)t[r](...n);return!!t.length}off(e,n){return this.#e[e]&&(this.#e[e]=this.#e[e].filter(t=>t!==n)),this}removeListener(e,n){return this.off(e,n)}removeAllListeners(e){return e?delete this.#e[e]:this.#e={},this}eventNames(){return Reflect.ownKeys(this.#e)}listeners(e){return this.#e[e]}listenerCount(e){return this.#e[e]?.length??0}}class p extends f{constructor(){super();const{history:e,location:n}=window,{pushState:t,replaceState:r}=e;e.pushState=(...o)=>{t.apply(e,o),this.emit("pushState",n,o[0])},e.replaceState=(...o)=>{r.apply(e,o),this.emit("replaceState",n,o[0])},window.addEventListener("popstate",o=>{this.emit("popState",n,o)})}}function c(s,e,n){const t=new MutationObserver((r,o)=>{for(const u of r)e(u,o)});return t.observe(s,{childList:!0,subtree:!0,...n}),()=>t.disconnect()}function m(s,e=document.documentElement){return new Promise(n=>{function t(){const r=e.querySelector(s);r&&n(r)}c(e,(r,o)=>{t(),o.disconnect()})})}exports.LocationObserver=p;exports.domReady=d;exports.el=l;exports.isDisabled=h;exports.nbsp=a;exports.observeElement=c;exports.text=i;exports.waitElement=m;
|
|
134
2
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/html.ts","
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/html.ts","../../emitter/dist/index.es.js","../src/location-observer.ts","../src/mutation-observers.ts"],"sourcesContent":["// prettier-ignore\ntype Attributes<T extends keyof HTMLElementTagNameMap> = Partial<{\n style: Partial<CSSStyleDeclaration>\n} & Omit<HTMLElementTagNameMap[T], 'style'>>\n\ntype Children = (string | Node | HTMLElement)[]\n\nexport function el<T extends keyof HTMLElementTagNameMap>(\n tag: T,\n attributes?: Children | Attributes<T>,\n ...children: Children\n): HTMLElementTagNameMap[T] {\n const el = document.createElement(tag)\n\n if (typeof attributes === 'string') {\n el.append(text(attributes))\n } else if (Array.isArray(attributes)) {\n el.append(...attributes)\n } else {\n Object.assign(el, attributes)\n Object.assign(el.style, attributes?.style)\n }\n\n if (children.length) {\n el.append(...children)\n }\n\n return el\n}\n\nexport function text(str: string): Text {\n return document.createTextNode(str)\n}\n\nexport function nbsp(): Text {\n return text('\\u00a0')\n}\n\nexport async function domReady(): Promise<void> {\n return new Promise((resolve) => {\n if (document.readyState == 'loading') {\n document.addEventListener('DOMContentLoaded', () => resolve(), {\n once: true\n })\n } else {\n resolve()\n }\n })\n}\n\nexport function isDisabled(element: HTMLElement): boolean {\n return (\n Boolean(element.getAttribute('disabled')) === true ||\n Boolean(element.getAttribute('aria-disabled')) === true\n )\n}\n","class n {\n #t = {};\n on(t, e) {\n const s = this.#t[t];\n return s ? s.push(e) : this.#t[t] = [e], this;\n }\n addListener(t, e) {\n return this.on(t, e);\n }\n once(t, e) {\n const s = (...i) => {\n this.off(t, s), e(...i);\n };\n return this.on(t, s), this;\n }\n emit(t, ...e) {\n const s = this.#t[t] || [];\n for (let i = 0; i < s.length; i++)\n s[i](...e);\n return !!s.length;\n }\n off(t, e) {\n return this.#t[t] && (this.#t[t] = this.#t[t].filter((s) => s !== e)), this;\n }\n removeListener(t, e) {\n return this.off(t, e);\n }\n removeAllListeners(t) {\n return t ? delete this.#t[t] : this.#t = {}, this;\n }\n eventNames() {\n return Reflect.ownKeys(this.#t);\n }\n listeners(t) {\n return this.#t[t];\n }\n listenerCount(t) {\n return this.#t[t]?.length ?? 0;\n }\n}\nexport {\n n as Emitter\n};\n//# sourceMappingURL=index.es.js.map\n","import { Emitter } from '@zero-dependency/emitter'\n\ntype LocationCallback<T = any> = (location: Location, args: T) => void\n\ntype Events<T> = {\n pushState: LocationCallback<T>\n replaceState: LocationCallback<T>\n popState: LocationCallback<\n Omit<PopStateEvent, 'state'> & { readonly state: T }\n >\n}\n\nexport class LocationObserver<T> extends Emitter<Events<T>> {\n constructor() {\n super()\n\n const { history, location } = window\n const { pushState, replaceState } = history\n\n history.pushState = (...args) => {\n pushState.apply(history, args)\n this.emit('pushState', location, args[0])\n }\n\n history.replaceState = (...args) => {\n replaceState.apply(history, args)\n this.emit('replaceState', location, args[0])\n }\n\n window.addEventListener('popstate', (event) => {\n this.emit('popState', location, event)\n })\n }\n}\n","type Disconnect = () => void\n\nexport function observeElement<T extends Element = Element>(\n el: T,\n callback: (mutation: MutationRecord, observer: MutationObserver) => void,\n options?: MutationObserverInit\n): Disconnect {\n const observe = new MutationObserver((mutations, observer) => {\n for (const mutation of mutations) {\n callback(mutation, observer)\n }\n })\n\n observe.observe(el, {\n childList: true,\n subtree: true,\n ...options\n })\n\n return () => observe.disconnect()\n}\n\nexport function waitElement<T extends Element = Element>(\n selector: string,\n target = document.documentElement\n): Promise<T> {\n return new Promise((resolve) => {\n function resolveElement() {\n const el = target.querySelector<T>(selector)\n if (el) {\n resolve(el)\n }\n }\n\n observeElement(target, (_, observer) => {\n resolveElement()\n observer.disconnect()\n })\n })\n}\n"],"names":["el","tag","attributes","children","text","str","nbsp","domReady","resolve","isDisabled","element","n","#t","t","e","s","i","LocationObserver","Emitter","history","location","pushState","replaceState","args","event","observeElement","callback","options","observe","mutations","observer","mutation","waitElement","selector","target","resolveElement","_"],"mappings":"gFAOgB,SAAAA,EACdC,EACAC,KACGC,EACuB,CACpBH,MAAAA,EAAK,SAAS,cAAcC,CAAG,EAEjC,OAAA,OAAOC,GAAe,SACxBF,EAAG,OAAOI,EAAKF,CAAU,CAAC,EACjB,MAAM,QAAQA,CAAU,EACjCF,EAAG,OAAO,GAAGE,CAAU,GAEhB,OAAA,OAAOF,EAAIE,CAAU,EAC5B,OAAO,OAAOF,EAAG,MAAOE,GAAY,KAAK,GAGvCC,EAAS,QACXH,EAAG,OAAO,GAAGG,CAAQ,EAGhBH,CACT,CAEO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EeyB,EAAAS,EAAQ,CAACE,EAAGN,IAAa,CACvBK,IACfL,EAAS,WAAW,CAAA,CACrB,CAAA,CACF,CACH"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export { observeElement } from './observeElement.js';
|
|
1
|
+
export * from './html.js';
|
|
2
|
+
export * from './location-observer.js';
|
|
3
|
+
export * from './mutation-observers.js';
|
package/dist/index.es.js
CHANGED
|
@@ -1,124 +1,106 @@
|
|
|
1
|
-
function
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
el2.append(text(attributes));
|
|
5
|
-
} else if (Array.isArray(attributes)) {
|
|
6
|
-
el2.append(...attributes);
|
|
7
|
-
} else {
|
|
8
|
-
Object.assign(el2, attributes);
|
|
9
|
-
Object.assign(el2.style, attributes?.style);
|
|
10
|
-
}
|
|
11
|
-
if (children.length) {
|
|
12
|
-
el2.append(...children);
|
|
13
|
-
}
|
|
14
|
-
return el2;
|
|
1
|
+
function a(s, e, ...n) {
|
|
2
|
+
const t = document.createElement(s);
|
|
3
|
+
return typeof e == "string" ? t.append(i(e)) : Array.isArray(e) ? t.append(...e) : (Object.assign(t, e), Object.assign(t.style, e?.style)), n.length && t.append(...n), t;
|
|
15
4
|
}
|
|
16
|
-
function
|
|
17
|
-
return document.createTextNode(
|
|
5
|
+
function i(s) {
|
|
6
|
+
return document.createTextNode(s);
|
|
18
7
|
}
|
|
19
|
-
function
|
|
20
|
-
return
|
|
8
|
+
function d() {
|
|
9
|
+
return i(" ");
|
|
21
10
|
}
|
|
22
|
-
async function
|
|
23
|
-
return new Promise((
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
});
|
|
28
|
-
} else {
|
|
29
|
-
resolve();
|
|
30
|
-
}
|
|
11
|
+
async function h() {
|
|
12
|
+
return new Promise((s) => {
|
|
13
|
+
document.readyState == "loading" ? document.addEventListener("DOMContentLoaded", () => s(), {
|
|
14
|
+
once: !0
|
|
15
|
+
}) : s();
|
|
31
16
|
});
|
|
32
17
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const observe = new MutationObserver((mutations, observer) => {
|
|
36
|
-
for (const mutation of mutations) {
|
|
37
|
-
callback(mutation, observer);
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
observe.observe(el, {
|
|
41
|
-
childList: true,
|
|
42
|
-
subtree: true,
|
|
43
|
-
...options
|
|
44
|
-
});
|
|
45
|
-
return observe;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function waitElement(selector, target = document.documentElement) {
|
|
49
|
-
return new Promise((resolve) => {
|
|
50
|
-
function resolveElement() {
|
|
51
|
-
const el = target.querySelector(selector);
|
|
52
|
-
if (el) {
|
|
53
|
-
resolve(el);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
observeElement(target, (_, observer) => {
|
|
57
|
-
resolveElement();
|
|
58
|
-
observer.disconnect();
|
|
59
|
-
});
|
|
60
|
-
});
|
|
18
|
+
function f(s) {
|
|
19
|
+
return !!s.getAttribute("disabled") || !!s.getAttribute("aria-disabled");
|
|
61
20
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return s ? s.push(e) : this.#t[t] = [e], this;
|
|
21
|
+
class u {
|
|
22
|
+
#e = {};
|
|
23
|
+
on(e, n) {
|
|
24
|
+
const t = this.#e[e];
|
|
25
|
+
return t ? t.push(n) : this.#e[e] = [n], this;
|
|
68
26
|
}
|
|
69
|
-
addListener(
|
|
70
|
-
return this.on(
|
|
27
|
+
addListener(e, n) {
|
|
28
|
+
return this.on(e, n);
|
|
71
29
|
}
|
|
72
|
-
once(
|
|
73
|
-
const
|
|
74
|
-
this.off(
|
|
30
|
+
once(e, n) {
|
|
31
|
+
const t = (...r) => {
|
|
32
|
+
this.off(e, t), n(...r);
|
|
75
33
|
};
|
|
76
|
-
return this.on(
|
|
34
|
+
return this.on(e, t), this;
|
|
77
35
|
}
|
|
78
|
-
emit(
|
|
79
|
-
const
|
|
80
|
-
for (let
|
|
81
|
-
|
|
82
|
-
return
|
|
36
|
+
emit(e, ...n) {
|
|
37
|
+
const t = this.#e[e] || [];
|
|
38
|
+
for (let r = 0; r < t.length; r++)
|
|
39
|
+
t[r](...n);
|
|
40
|
+
return !!t.length;
|
|
83
41
|
}
|
|
84
|
-
off(
|
|
85
|
-
return this.#
|
|
42
|
+
off(e, n) {
|
|
43
|
+
return this.#e[e] && (this.#e[e] = this.#e[e].filter((t) => t !== n)), this;
|
|
86
44
|
}
|
|
87
|
-
removeListener(
|
|
88
|
-
return this.off(
|
|
45
|
+
removeListener(e, n) {
|
|
46
|
+
return this.off(e, n);
|
|
89
47
|
}
|
|
90
|
-
removeAllListeners(
|
|
91
|
-
return
|
|
48
|
+
removeAllListeners(e) {
|
|
49
|
+
return e ? delete this.#e[e] : this.#e = {}, this;
|
|
92
50
|
}
|
|
93
51
|
eventNames() {
|
|
94
|
-
return Reflect.ownKeys(this.#
|
|
52
|
+
return Reflect.ownKeys(this.#e);
|
|
95
53
|
}
|
|
96
|
-
listeners(
|
|
97
|
-
return this.#
|
|
54
|
+
listeners(e) {
|
|
55
|
+
return this.#e[e];
|
|
98
56
|
}
|
|
99
|
-
listenerCount(
|
|
100
|
-
return this.#
|
|
57
|
+
listenerCount(e) {
|
|
58
|
+
return this.#e[e]?.length ?? 0;
|
|
101
59
|
}
|
|
102
60
|
}
|
|
103
|
-
|
|
104
|
-
class LocationObserver extends n {
|
|
61
|
+
class p extends u {
|
|
105
62
|
constructor() {
|
|
106
63
|
super();
|
|
107
|
-
const { history, location } = window;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
this.emit("
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
replaceState.apply(history, args);
|
|
115
|
-
this.emit("replaceState", location, args[0]);
|
|
116
|
-
};
|
|
117
|
-
window.addEventListener("popstate", ({ state }) => {
|
|
118
|
-
this.emit("popState", location, state);
|
|
64
|
+
const { history: e, location: n } = window, { pushState: t, replaceState: r } = e;
|
|
65
|
+
e.pushState = (...o) => {
|
|
66
|
+
t.apply(e, o), this.emit("pushState", n, o[0]);
|
|
67
|
+
}, e.replaceState = (...o) => {
|
|
68
|
+
r.apply(e, o), this.emit("replaceState", n, o[0]);
|
|
69
|
+
}, window.addEventListener("popstate", (o) => {
|
|
70
|
+
this.emit("popState", n, o);
|
|
119
71
|
});
|
|
120
72
|
}
|
|
121
73
|
}
|
|
122
|
-
|
|
123
|
-
|
|
74
|
+
function l(s, e, n) {
|
|
75
|
+
const t = new MutationObserver((r, o) => {
|
|
76
|
+
for (const c of r)
|
|
77
|
+
e(c, o);
|
|
78
|
+
});
|
|
79
|
+
return t.observe(s, {
|
|
80
|
+
childList: !0,
|
|
81
|
+
subtree: !0,
|
|
82
|
+
...n
|
|
83
|
+
}), () => t.disconnect();
|
|
84
|
+
}
|
|
85
|
+
function m(s, e = document.documentElement) {
|
|
86
|
+
return new Promise((n) => {
|
|
87
|
+
function t() {
|
|
88
|
+
const r = e.querySelector(s);
|
|
89
|
+
r && n(r);
|
|
90
|
+
}
|
|
91
|
+
l(e, (r, o) => {
|
|
92
|
+
t(), o.disconnect();
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
p as LocationObserver,
|
|
98
|
+
h as domReady,
|
|
99
|
+
a as el,
|
|
100
|
+
f as isDisabled,
|
|
101
|
+
d as nbsp,
|
|
102
|
+
l as observeElement,
|
|
103
|
+
i as text,
|
|
104
|
+
m as waitElement
|
|
105
|
+
};
|
|
124
106
|
//# sourceMappingURL=index.es.js.map
|
package/dist/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/html.ts","
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/html.ts","../../emitter/dist/index.es.js","../src/location-observer.ts","../src/mutation-observers.ts"],"sourcesContent":["// prettier-ignore\ntype Attributes<T extends keyof HTMLElementTagNameMap> = Partial<{\n style: Partial<CSSStyleDeclaration>\n} & Omit<HTMLElementTagNameMap[T], 'style'>>\n\ntype Children = (string | Node | HTMLElement)[]\n\nexport function el<T extends keyof HTMLElementTagNameMap>(\n tag: T,\n attributes?: Children | Attributes<T>,\n ...children: Children\n): HTMLElementTagNameMap[T] {\n const el = document.createElement(tag)\n\n if (typeof attributes === 'string') {\n el.append(text(attributes))\n } else if (Array.isArray(attributes)) {\n el.append(...attributes)\n } else {\n Object.assign(el, attributes)\n Object.assign(el.style, attributes?.style)\n }\n\n if (children.length) {\n el.append(...children)\n }\n\n return el\n}\n\nexport function text(str: string): Text {\n return document.createTextNode(str)\n}\n\nexport function nbsp(): Text {\n return text('\\u00a0')\n}\n\nexport async function domReady(): Promise<void> {\n return new Promise((resolve) => {\n if (document.readyState == 'loading') {\n document.addEventListener('DOMContentLoaded', () => resolve(), {\n once: true\n })\n } else {\n resolve()\n }\n })\n}\n\nexport function isDisabled(element: HTMLElement): boolean {\n return (\n Boolean(element.getAttribute('disabled')) === true ||\n Boolean(element.getAttribute('aria-disabled')) === true\n )\n}\n","class n {\n #t = {};\n on(t, e) {\n const s = this.#t[t];\n return s ? s.push(e) : this.#t[t] = [e], this;\n }\n addListener(t, e) {\n return this.on(t, e);\n }\n once(t, e) {\n const s = (...i) => {\n this.off(t, s), e(...i);\n };\n return this.on(t, s), this;\n }\n emit(t, ...e) {\n const s = this.#t[t] || [];\n for (let i = 0; i < s.length; i++)\n s[i](...e);\n return !!s.length;\n }\n off(t, e) {\n return this.#t[t] && (this.#t[t] = this.#t[t].filter((s) => s !== e)), this;\n }\n removeListener(t, e) {\n return this.off(t, e);\n }\n removeAllListeners(t) {\n return t ? delete this.#t[t] : this.#t = {}, this;\n }\n eventNames() {\n return Reflect.ownKeys(this.#t);\n }\n listeners(t) {\n return this.#t[t];\n }\n listenerCount(t) {\n return this.#t[t]?.length ?? 0;\n }\n}\nexport {\n n as Emitter\n};\n//# sourceMappingURL=index.es.js.map\n","import { Emitter } from '@zero-dependency/emitter'\n\ntype LocationCallback<T = any> = (location: Location, args: T) => void\n\ntype Events<T> = {\n pushState: LocationCallback<T>\n replaceState: LocationCallback<T>\n popState: LocationCallback<\n Omit<PopStateEvent, 'state'> & { readonly state: T }\n >\n}\n\nexport class LocationObserver<T> extends Emitter<Events<T>> {\n constructor() {\n super()\n\n const { history, location } = window\n const { pushState, replaceState } = history\n\n history.pushState = (...args) => {\n pushState.apply(history, args)\n this.emit('pushState', location, args[0])\n }\n\n history.replaceState = (...args) => {\n replaceState.apply(history, args)\n this.emit('replaceState', location, args[0])\n }\n\n window.addEventListener('popstate', (event) => {\n this.emit('popState', location, event)\n })\n }\n}\n","type Disconnect = () => void\n\nexport function observeElement<T extends Element = Element>(\n el: T,\n callback: (mutation: MutationRecord, observer: MutationObserver) => void,\n options?: MutationObserverInit\n): Disconnect {\n const observe = new MutationObserver((mutations, observer) => {\n for (const mutation of mutations) {\n callback(mutation, observer)\n }\n })\n\n observe.observe(el, {\n childList: true,\n subtree: true,\n ...options\n })\n\n return () => observe.disconnect()\n}\n\nexport function waitElement<T extends Element = Element>(\n selector: string,\n target = document.documentElement\n): Promise<T> {\n return new Promise((resolve) => {\n function resolveElement() {\n const el = target.querySelector<T>(selector)\n if (el) {\n resolve(el)\n }\n }\n\n observeElement(target, (_, observer) => {\n resolveElement()\n observer.disconnect()\n })\n })\n}\n"],"names":["el","tag","attributes","children","text","str","nbsp","domReady","resolve","isDisabled","element","n","#t","t","e","s","i","LocationObserver","Emitter","history","location","pushState","replaceState","args","event","observeElement","callback","options","observe","mutations","observer","mutation","waitElement","selector","target","resolveElement","_"],"mappings":"AAOgB,SAAAA,EACdC,GACAC,MACGC,GACuB;AACpBH,QAAAA,IAAK,SAAS,cAAcC,CAAG;AAEjC,SAAA,OAAOC,KAAe,WACxBF,EAAG,OAAOI,EAAKF,CAAU,CA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,EACdzB,GACA0B,GACAC,GACY;AACZ,QAAMC,IAAU,IAAI,iBAAiB,CAACC,GAAWC,MAAa;AAC5D,eAAWC,KAAYF;AACrB,MAAAH,EAASK,GAAUD,CAAQ;AAAA,EAC7B,CACD;AAED,SAAAF,EAAQ,QAAQ5B,GAAI;AAAA,IAClB,WAAW;AAAA,IACX,SAAS;AAAA,IACT,GAAG2B;AAAA,EAAA,CACJ,GAEM,MAAMC,EAAQ;AACvB;AAEO,SAASI,EACdC,GACAC,IAAS,SAAS,iBACN;AACL,SAAA,IAAI,QAAQ,CAAC1B,MAAY;AAC9B,aAAS2B,IAAiB;AAClB,YAAAnC,IAAKkC,EAAO,cAAiBD,CAAQ;AAC3C,MAAIjC,KACFQ,EAAQR,CAAE;AAAA,IAEd;AAEe,IAAAyB,EAAAS,GAAQ,CAACE,GAAGN,MAAa;AACvB,MAAAK,KACfL,EAAS,WAAW;AAAA,IAAA,CACrB;AAAA,EAAA,CACF;AACH;"}
|
package/dist/index.umd.js
CHANGED
|
@@ -1,140 +1,2 @@
|
|
|
1
|
-
(function (
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@zero-dependency/dom"] = {}));
|
|
5
|
-
})(this, (function (exports) { 'use strict';
|
|
6
|
-
|
|
7
|
-
function el(tag, attributes, ...children) {
|
|
8
|
-
const el2 = document.createElement(tag);
|
|
9
|
-
if (typeof attributes === "string") {
|
|
10
|
-
el2.append(text(attributes));
|
|
11
|
-
} else if (Array.isArray(attributes)) {
|
|
12
|
-
el2.append(...attributes);
|
|
13
|
-
} else {
|
|
14
|
-
Object.assign(el2, attributes);
|
|
15
|
-
Object.assign(el2.style, attributes?.style);
|
|
16
|
-
}
|
|
17
|
-
if (children.length) {
|
|
18
|
-
el2.append(...children);
|
|
19
|
-
}
|
|
20
|
-
return el2;
|
|
21
|
-
}
|
|
22
|
-
function text(str) {
|
|
23
|
-
return document.createTextNode(str);
|
|
24
|
-
}
|
|
25
|
-
function nbsp() {
|
|
26
|
-
return text("\xA0");
|
|
27
|
-
}
|
|
28
|
-
async function domReady() {
|
|
29
|
-
return new Promise((resolve) => {
|
|
30
|
-
if (document.readyState == "loading") {
|
|
31
|
-
document.addEventListener("DOMContentLoaded", () => resolve(), {
|
|
32
|
-
once: true
|
|
33
|
-
});
|
|
34
|
-
} else {
|
|
35
|
-
resolve();
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function observeElement(el, callback, options) {
|
|
41
|
-
const observe = new MutationObserver((mutations, observer) => {
|
|
42
|
-
for (const mutation of mutations) {
|
|
43
|
-
callback(mutation, observer);
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
observe.observe(el, {
|
|
47
|
-
childList: true,
|
|
48
|
-
subtree: true,
|
|
49
|
-
...options
|
|
50
|
-
});
|
|
51
|
-
return observe;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function waitElement(selector, target = document.documentElement) {
|
|
55
|
-
return new Promise((resolve) => {
|
|
56
|
-
function resolveElement() {
|
|
57
|
-
const el = target.querySelector(selector);
|
|
58
|
-
if (el) {
|
|
59
|
-
resolve(el);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
observeElement(target, (_, observer) => {
|
|
63
|
-
resolveElement();
|
|
64
|
-
observer.disconnect();
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
class n {
|
|
70
|
-
#t = {};
|
|
71
|
-
on(t, e) {
|
|
72
|
-
const s = this.#t[t];
|
|
73
|
-
return s ? s.push(e) : this.#t[t] = [e], this;
|
|
74
|
-
}
|
|
75
|
-
addListener(t, e) {
|
|
76
|
-
return this.on(t, e);
|
|
77
|
-
}
|
|
78
|
-
once(t, e) {
|
|
79
|
-
const s = (...i) => {
|
|
80
|
-
this.off(t, s), e(...i);
|
|
81
|
-
};
|
|
82
|
-
return this.on(t, s), this;
|
|
83
|
-
}
|
|
84
|
-
emit(t, ...e) {
|
|
85
|
-
const s = this.#t[t] || [];
|
|
86
|
-
for (let i = 0; i < s.length; i++)
|
|
87
|
-
s[i](...e);
|
|
88
|
-
return Boolean(s.length);
|
|
89
|
-
}
|
|
90
|
-
off(t, e) {
|
|
91
|
-
return this.#t[t] && (this.#t[t] = this.#t[t].filter((s) => s !== e)), this;
|
|
92
|
-
}
|
|
93
|
-
removeListener(t, e) {
|
|
94
|
-
return this.off(t, e);
|
|
95
|
-
}
|
|
96
|
-
removeAllListeners(t) {
|
|
97
|
-
return t ? delete this.#t[t] : this.#t = {}, this;
|
|
98
|
-
}
|
|
99
|
-
eventNames() {
|
|
100
|
-
return Reflect.ownKeys(this.#t);
|
|
101
|
-
}
|
|
102
|
-
listeners(t) {
|
|
103
|
-
return this.#t[t];
|
|
104
|
-
}
|
|
105
|
-
listenerCount(t) {
|
|
106
|
-
return this.#t[t]?.length ?? 0;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
class LocationObserver extends n {
|
|
111
|
-
constructor() {
|
|
112
|
-
super();
|
|
113
|
-
const { history, location } = window;
|
|
114
|
-
const { pushState, replaceState } = history;
|
|
115
|
-
history.pushState = (...args) => {
|
|
116
|
-
pushState.apply(history, args);
|
|
117
|
-
this.emit("pushState", location, args[0]);
|
|
118
|
-
};
|
|
119
|
-
history.replaceState = (...args) => {
|
|
120
|
-
replaceState.apply(history, args);
|
|
121
|
-
this.emit("replaceState", location, args[0]);
|
|
122
|
-
};
|
|
123
|
-
window.addEventListener("popstate", ({ state }) => {
|
|
124
|
-
this.emit("popState", location, state);
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
exports.LocationObserver = LocationObserver;
|
|
130
|
-
exports.domReady = domReady;
|
|
131
|
-
exports.el = el;
|
|
132
|
-
exports.nbsp = nbsp;
|
|
133
|
-
exports.observeElement = observeElement;
|
|
134
|
-
exports.text = text;
|
|
135
|
-
exports.waitElement = waitElement;
|
|
136
|
-
|
|
137
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
138
|
-
|
|
139
|
-
}));
|
|
1
|
+
(function(s,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(s=typeof globalThis<"u"?globalThis:s||self,u(s["@zero-dependency/dom"]={}))})(this,function(s){"use strict";function u(i,e,...n){const t=document.createElement(i);return typeof e=="string"?t.append(c(e)):Array.isArray(e)?t.append(...e):(Object.assign(t,e),Object.assign(t.style,e?.style)),n.length&&t.append(...n),t}function c(i){return document.createTextNode(i)}function l(){return c(" ")}async function a(){return new Promise(i=>{document.readyState=="loading"?document.addEventListener("DOMContentLoaded",()=>i(),{once:!0}):i()})}function f(i){return!!i.getAttribute("disabled")||!!i.getAttribute("aria-disabled")}class h{#e={};on(e,n){const t=this.#e[e];return t?t.push(n):this.#e[e]=[n],this}addListener(e,n){return this.on(e,n)}once(e,n){const t=(...o)=>{this.off(e,t),n(...o)};return this.on(e,t),this}emit(e,...n){const t=this.#e[e]||[];for(let o=0;o<t.length;o++)t[o](...n);return!!t.length}off(e,n){return this.#e[e]&&(this.#e[e]=this.#e[e].filter(t=>t!==n)),this}removeListener(e,n){return this.off(e,n)}removeAllListeners(e){return e?delete this.#e[e]:this.#e={},this}eventNames(){return Reflect.ownKeys(this.#e)}listeners(e){return this.#e[e]}listenerCount(e){return this.#e[e]?.length??0}}class m extends h{constructor(){super();const{history:e,location:n}=window,{pushState:t,replaceState:o}=e;e.pushState=(...r)=>{t.apply(e,r),this.emit("pushState",n,r[0])},e.replaceState=(...r)=>{o.apply(e,r),this.emit("replaceState",n,r[0])},window.addEventListener("popstate",r=>{this.emit("popState",n,r)})}}function d(i,e,n){const t=new MutationObserver((o,r)=>{for(const y of o)e(y,r)});return t.observe(i,{childList:!0,subtree:!0,...n}),()=>t.disconnect()}function p(i,e=document.documentElement){return new Promise(n=>{function t(){const o=e.querySelector(i);o&&n(o)}d(e,(o,r)=>{t(),r.disconnect()})})}s.LocationObserver=m,s.domReady=a,s.el=u,s.isDisabled=f,s.nbsp=l,s.observeElement=d,s.text=c,s.waitElement=p,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
|
|
140
2
|
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/html.ts","
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/html.ts","../../emitter/dist/index.es.js","../src/location-observer.ts","../src/mutation-observers.ts"],"sourcesContent":["// prettier-ignore\ntype Attributes<T extends keyof HTMLElementTagNameMap> = Partial<{\n style: Partial<CSSStyleDeclaration>\n} & Omit<HTMLElementTagNameMap[T], 'style'>>\n\ntype Children = (string | Node | HTMLElement)[]\n\nexport function el<T extends keyof HTMLElementTagNameMap>(\n tag: T,\n attributes?: Children | Attributes<T>,\n ...children: Children\n): HTMLElementTagNameMap[T] {\n const el = document.createElement(tag)\n\n if (typeof attributes === 'string') {\n el.append(text(attributes))\n } else if (Array.isArray(attributes)) {\n el.append(...attributes)\n } else {\n Object.assign(el, attributes)\n Object.assign(el.style, attributes?.style)\n }\n\n if (children.length) {\n el.append(...children)\n }\n\n return el\n}\n\nexport function text(str: string): Text {\n return document.createTextNode(str)\n}\n\nexport function nbsp(): Text {\n return text('\\u00a0')\n}\n\nexport async function domReady(): Promise<void> {\n return new Promise((resolve) => {\n if (document.readyState == 'loading') {\n document.addEventListener('DOMContentLoaded', () => resolve(), {\n once: true\n })\n } else {\n resolve()\n }\n })\n}\n\nexport function isDisabled(element: HTMLElement): boolean {\n return (\n Boolean(element.getAttribute('disabled')) === true ||\n Boolean(element.getAttribute('aria-disabled')) === true\n )\n}\n","class n {\n #t = {};\n on(t, e) {\n const s = this.#t[t];\n return s ? s.push(e) : this.#t[t] = [e], this;\n }\n addListener(t, e) {\n return this.on(t, e);\n }\n once(t, e) {\n const s = (...i) => {\n this.off(t, s), e(...i);\n };\n return this.on(t, s), this;\n }\n emit(t, ...e) {\n const s = this.#t[t] || [];\n for (let i = 0; i < s.length; i++)\n s[i](...e);\n return !!s.length;\n }\n off(t, e) {\n return this.#t[t] && (this.#t[t] = this.#t[t].filter((s) => s !== e)), this;\n }\n removeListener(t, e) {\n return this.off(t, e);\n }\n removeAllListeners(t) {\n return t ? delete this.#t[t] : this.#t = {}, this;\n }\n eventNames() {\n return Reflect.ownKeys(this.#t);\n }\n listeners(t) {\n return this.#t[t];\n }\n listenerCount(t) {\n return this.#t[t]?.length ?? 0;\n }\n}\nexport {\n n as Emitter\n};\n//# sourceMappingURL=index.es.js.map\n","import { Emitter } from '@zero-dependency/emitter'\n\ntype LocationCallback<T = any> = (location: Location, args: T) => void\n\ntype Events<T> = {\n pushState: LocationCallback<T>\n replaceState: LocationCallback<T>\n popState: LocationCallback<\n Omit<PopStateEvent, 'state'> & { readonly state: T }\n >\n}\n\nexport class LocationObserver<T> extends Emitter<Events<T>> {\n constructor() {\n super()\n\n const { history, location } = window\n const { pushState, replaceState } = history\n\n history.pushState = (...args) => {\n pushState.apply(history, args)\n this.emit('pushState', location, args[0])\n }\n\n history.replaceState = (...args) => {\n replaceState.apply(history, args)\n this.emit('replaceState', location, args[0])\n }\n\n window.addEventListener('popstate', (event) => {\n this.emit('popState', location, event)\n })\n }\n}\n","type Disconnect = () => void\n\nexport function observeElement<T extends Element = Element>(\n el: T,\n callback: (mutation: MutationRecord, observer: MutationObserver) => void,\n options?: MutationObserverInit\n): Disconnect {\n const observe = new MutationObserver((mutations, observer) => {\n for (const mutation of mutations) {\n callback(mutation, observer)\n }\n })\n\n observe.observe(el, {\n childList: true,\n subtree: true,\n ...options\n })\n\n return () => observe.disconnect()\n}\n\nexport function waitElement<T extends Element = Element>(\n selector: string,\n target = document.documentElement\n): Promise<T> {\n return new Promise((resolve) => {\n function resolveElement() {\n const el = target.querySelector<T>(selector)\n if (el) {\n resolve(el)\n }\n }\n\n observeElement(target, (_, observer) => {\n resolveElement()\n observer.disconnect()\n })\n })\n}\n"],"names":["el","tag","attributes","children","text","str","nbsp","domReady","resolve","isDisabled","element","n","#t","t","e","s","i","LocationObserver","Emitter","history","location","pushState","replaceState","args","event","observeElement","callback","options","observe","mutations","observer","mutation","waitElement","selector","target","resolveElement","_"],"mappings":"+OAOgB,SAAAA,EACdC,EACAC,KACGC,EACuB,CACpBH,MAAAA,EAAK,SAAS,cAAcC,CAAG,EAEjC,OAAA,OAAOC,GAAe,SACxBF,EAAG,OAAOI,EAAKF,CAAU,CAAC,EACjB,MAAM,QAAQA,CAAU,EACjCF,EAAG,OAAO,GAAGE,CAAU,GAEhB,OAAA,OAAOF,EAAIE,CAAU,EAC5B,OAAO,OAAOF,EAAG,MAAOE,GAAY,KAAK,GAGvCC,EAAS,QACXH,EAAG,OAAO,GAAGG,CAAQ,EAGhBH,CACT,CAEO,SAASI,EAAKC,EAAmB,CAC/B,OAAA,SAAS,eAAeA,CAAG,CACpC,CAEO,SAASC,GAAa,CAC3B,OAAOF,EAAK,GAAQ,CACtB,CAEA,eAAsBG,GAA0B,CACvC,OAAA,IAAI,QAASC,GAAY,CAC1B,SAAS,YAAc,UACzB,SAAS,iBAAiB,mBAAoB,IAAMA,EAAA,EAAW,CAC7D,KAAM,EAAA,CACP,EAEOA,GACV,CACD,CACH,CAEO,SAASC,EAAWC,EAA+B,CACxD,MACE,EAAQA,EAAQ,aAAa,UAAU,GACvC,EAAQA,EAAQ,aAAa,eAAe,CAEhD,CCvDA,MAAMC,CAAE,CACNC,GAAK,CAAA,EACL,GAAGC,EAAGC,EAAG,CACP,MAAMC,EAAI,KAAKH,GAAGC,CAAC,EACnB,OAAOE,EAAIA,EAAE,KAAKD,CAAC,EAAI,KAAKF,GAAGC,CAAC,EAAI,CAACC,CA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CAAA,EAGlCJ,EAAA,aAAe,IAAII,IAAS,CACrBD,EAAA,MAAMH,EAASI,CAAI,EAChC,KAAK,KAAK,eAAgBH,EAAUG,EAAK,CAAC,CAAC,CAAA,EAGtC,OAAA,iBAAiB,WAAaC,GAAU,CACxC,KAAA,KAAK,WAAYJ,EAAUI,CAAK,CAAA,CACtC,CACH,CACF,CC/BgB,SAAAC,EACdzB,EACA0B,EACAC,EACY,CACZ,MAAMC,EAAU,IAAI,iBAAiB,CAACC,EAAWC,IAAa,CAC5D,UAAWC,KAAYF,EACrBH,EAASK,EAAUD,CAAQ,CAC7B,CACD,EAED,OAAAF,EAAQ,QAAQ5B,EAAI,CAClB,UAAW,GACX,QAAS,GACT,GAAG2B,CAAA,CACJ,EAEM,IAAMC,EAAQ,YACvB,CAEO,SAASI,EACdC,EACAC,EAAS,SAAS,gBACN,CACL,OAAA,IAAI,QAAS1B,GAAY,CAC9B,SAAS2B,GAAiB,CAClB,MAAAnC,EAAKkC,EAAO,cAAiBD,CAAQ,EACvCjC,GACFQ,EAAQR,CAAE,CAEd,CAEeyB,EAAAS,EAAQ,CAACE,EAAGN,IAAa,CACvBK,IACfL,EAAS,WAAW,CAAA,CACrB,CAAA,CACF,CACH"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Emitter } from '@zero-dependency/emitter';
|
|
2
|
+
type LocationCallback<T = any> = (location: Location, args: T) => void;
|
|
3
|
+
type Events<T> = {
|
|
4
|
+
pushState: LocationCallback<T>;
|
|
5
|
+
replaceState: LocationCallback<T>;
|
|
6
|
+
popState: LocationCallback<Omit<PopStateEvent, 'state'> & {
|
|
7
|
+
readonly state: T;
|
|
8
|
+
}>;
|
|
9
|
+
};
|
|
10
|
+
export declare class LocationObserver<T> extends Emitter<Events<T>> {
|
|
11
|
+
constructor();
|
|
12
|
+
}
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
type Disconnect = () => void;
|
|
2
|
+
export declare function observeElement<T extends Element = Element>(el: T, callback: (mutation: MutationRecord, observer: MutationObserver) => void, options?: MutationObserverInit): Disconnect;
|
|
3
|
+
export declare function waitElement<T extends Element = Element>(selector: string, target?: HTMLElement): Promise<T>;
|
|
4
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zero-dependency/dom",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"main": "./dist/index.umd.js",
|
|
@@ -22,19 +22,19 @@
|
|
|
22
22
|
"url": "https://crashmax.ru"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@crashmax/prettier-config": "^2.1
|
|
26
|
-
"@crashmax/tsconfig": "^
|
|
27
|
-
"@types/node": "^18.11
|
|
28
|
-
"@vitest/coverage-c8": "^0.
|
|
29
|
-
"@vitest/ui": "^0.
|
|
30
|
-
"jsdom": "^
|
|
31
|
-
"typescript": "^
|
|
32
|
-
"vite": "^
|
|
33
|
-
"vite-plugin-dts": "^1.
|
|
34
|
-
"vitest": "^0.
|
|
25
|
+
"@crashmax/prettier-config": "^3.2.1",
|
|
26
|
+
"@crashmax/tsconfig": "^2.0.1",
|
|
27
|
+
"@types/node": "^18.15.11",
|
|
28
|
+
"@vitest/coverage-c8": "^0.29.8",
|
|
29
|
+
"@vitest/ui": "^0.29.8",
|
|
30
|
+
"jsdom": "^21.1.1",
|
|
31
|
+
"typescript": "^5.0.2",
|
|
32
|
+
"vite": "^4.2.1",
|
|
33
|
+
"vite-plugin-dts": "^2.1.0",
|
|
34
|
+
"vitest": "^0.29.8"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@zero-dependency/emitter": "1.1.
|
|
37
|
+
"@zero-dependency/emitter": "1.1.3"
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"dev": "vite build --watch",
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { Emitter } from '@zero-dependency/emitter';
|
|
2
|
-
declare type LocationCallback = (location: Location, state: any) => void;
|
|
3
|
-
declare type Events = {
|
|
4
|
-
pushState: LocationCallback;
|
|
5
|
-
replaceState: LocationCallback;
|
|
6
|
-
popState: LocationCallback;
|
|
7
|
-
};
|
|
8
|
-
export declare class LocationObserver extends Emitter<Events> {
|
|
9
|
-
constructor();
|
|
10
|
-
}
|
|
11
|
-
export {};
|
package/dist/observeElement.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function observeElement<T extends Element = Element>(el: T, callback: (mutation: MutationRecord, observer: MutationObserver) => void, options?: MutationObserverInit): MutationObserver;
|
package/dist/waitElement.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function waitElement<T extends Element = Element>(selector: string, target?: HTMLElement): Promise<T>;
|