@zero-dependency/dom 1.0.1 → 1.1.0

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/html.d.ts CHANGED
@@ -2,9 +2,33 @@ 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
- export declare function el<T extends keyof HTMLElementTagNameMap>(tag: T, attributes?: Children | Attributes<T>, ...children: Children): HTMLElementTagNameMap[T];
6
- export declare function text(str: string): Text;
5
+ /**
6
+ * Create an element
7
+ * @param tag The tag name of the element to create
8
+ * @param attributes The attributes or children to set on the element
9
+ * @param children The children to append to the element
10
+ * @returns The created element
11
+ * @example
12
+ * el('div', { id: 'foo' }, 'Hello world')
13
+ * el('div', 'Hello world')
14
+ * el('div', [el('span', 'Hello'), el('span', 'world')])
15
+ * el('div', el('span', 'Hello world'))
16
+ * el('div', el('span', 'Hello'), el('span', 'world'))
17
+ * el('div', el('span', 'Hello world'), 'world')
18
+ */
19
+ export declare function el<T extends keyof HTMLElementTagNameMap>(tag: T, attributes?: Attributes<T> | Children, ...children: Children): HTMLElementTagNameMap[T];
20
+ /**
21
+ * Create a text node
22
+ * @param text The string to create a text node from
23
+ */
24
+ export declare function text(text: string): Text;
25
+ /**
26
+ * A non-breaking space
27
+ */
7
28
  export declare function nbsp(): Text;
29
+ /**
30
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event
31
+ * @returns A promise that resolves when the DOM is ready
32
+ */
8
33
  export declare function domReady(): Promise<void>;
9
- export declare function isDisabled(element: HTMLElement): boolean;
10
34
  export {};
package/dist/index.cjs.js CHANGED
@@ -1,2 +1,2 @@
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;
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()})}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 p 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 c(s,e,n){const t=new MutationObserver((o,r)=>{for(const u of o)e(u,r)});return t.observe(s,{childList:!0,subtree:!0,...n}),()=>t.disconnect()}function m(s,e=document.documentElement){return new Promise(n=>{c(e,(t,o)=>{const r=e.querySelector(s);r&&(o.disconnect(),n(r))})})}exports.LocationObserver=p;exports.domReady=d;exports.el=l;exports.nbsp=a;exports.observeElement=c;exports.text=i;exports.waitElement=m;
2
2
  //# sourceMappingURL=index.cjs.js.map
@@ -1 +1 @@
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,SAASI,EA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AS,EAAQ,CAACE,EAAGN,IAAa,CACvBK,IACfL,EAAS,WAAW,CAAA,CACrB,CAAA,CACF,CACH"}
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\n/**\n * Create an element\n * @param tag The tag name of the element to create\n * @param attributes The attributes or children to set on the element\n * @param children The children to append to the element\n * @returns The created element\n * @example\n * el('div', { id: 'foo' }, 'Hello world')\n * el('div', 'Hello world')\n * el('div', [el('span', 'Hello'), el('span', 'world')])\n * el('div', el('span', 'Hello world'))\n * el('div', el('span', 'Hello'), el('span', 'world'))\n * el('div', el('span', 'Hello world'), 'world')\n */\nexport function el<T extends keyof HTMLElementTagNameMap>(\n tag: T,\n attributes?: Attributes<T> | Children,\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\n/**\n * Create a text node\n * @param text The string to create a text node from\n */\nexport function text(text: string): Text {\n return document.createTextNode(text)\n}\n\n/**\n * A non-breaking space\n */\nexport function nbsp(): Text {\n return text('\\u00a0')\n}\n\n/**\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event\n * @returns A promise that resolves when the DOM is ready\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","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\n/**\n * Observe changes to the location\n * @example\n * const observer = new LocationObserver<{ id: string }>()\n * observer.on('pushState', (location, state) => {\n * console.log(state.id)\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\n/**\n * Observe mutations on an element\n * @param el The element to observe\n * @param callback The callback to call when a mutation occurs\n * @param options The options to pass to the `MutationObserver`\n * @returns A function to disconnect the observer\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\n/**\n * Wait for an element to appear in the DOM\n * @param selector The selector to wait for\n * @param target The element to search in\n * @returns A promise that resolves when the element is found\n */\nexport function waitElement<T extends Element = Element>(\n selector: string,\n target = document.documentElement\n): Promise<T> {\n return new Promise((resolve) => {\n observeElement(target, (_, observer) => {\n const el = target.querySelector<T>(selector)\n if (el) {\n observer.disconnect()\n resolve(el)\n }\n })\n })\n}\n"],"names":["el","tag","attributes","children","text","nbsp","domReady","resolve","n","#t","t","e","s","i","LocationObserver","Emitter","history","location","pushState","replaceState","args","event","observeElement","callback","options","observe","mutations","observer","mutation","waitElement","selector","target","_"],"mappings":"gFAqBgB,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,CAMO,SAASI,EAAKA,EAAoB,CAChC,OAAA,SAAS,eAAeA,CAAI,CACrC,CAKO,SAASC,GAAa,CAC3B,OAAOD,EAAK,GAAQ,CACtB,CAMA,eAAsBE,GAA0B,CACvC,OAAA,IAAI,QAASC,GAAY,CAC1B,SAAS,YAAc,UACzB,SAAS,iBAAiB,mBAAoB,IAAMA,EAAA,EAAW,CAC7D,KAAM,EAAA,CACP,EAEOA,GACV,CACD,CACH,CCzEA,MAAMC,CAAE,CACNC,GAAK,CAAA,EACL,GAAGC,EAAGC,EAAG,CACP,M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5BA,EAAA,UAAY,IAAII,IAAS,CACrBF,EAAA,MAAMF,EAASI,CAAI,EAC7B,KAAK,KAAK,YAAaH,EAAUG,EAAK,CAAC,CAAC,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,CChCgB,SAAAC,EACdtB,EACAuB,EACAC,EACY,CACZ,MAAMC,EAAU,IAAI,iBAAiB,CAACC,EAAWC,IAAa,CAC5D,UAAWC,KAAYF,EACrBH,EAASK,EAAUD,CAAQ,CAC7B,CACD,EAED,OAAAF,EAAQ,QAAQzB,EAAI,CAClB,UAAW,GACX,QAAS,GACT,GAAGwB,CAAA,CACJ,EAEM,IAAMC,EAAQ,YACvB,CAQO,SAASI,EACdC,EACAC,EAAS,SAAS,gBACN,CACL,OAAA,IAAI,QAASxB,GAAY,CACfe,EAAAS,EAAQ,CAACC,EAAGL,IAAa,CAChC,MAAA3B,EAAK+B,EAAO,cAAiBD,CAAQ,EACvC9B,IACF2B,EAAS,WAAW,EACpBpB,EAAQP,CAAE,EACZ,CACD,CAAA,CACF,CACH"}
package/dist/index.es.js CHANGED
@@ -5,19 +5,16 @@ function a(s, e, ...n) {
5
5
  function i(s) {
6
6
  return document.createTextNode(s);
7
7
  }
8
- function d() {
8
+ function h() {
9
9
  return i(" ");
10
10
  }
11
- async function h() {
11
+ async function d() {
12
12
  return new Promise((s) => {
13
13
  document.readyState == "loading" ? document.addEventListener("DOMContentLoaded", () => s(), {
14
14
  once: !0
15
15
  }) : s();
16
16
  });
17
17
  }
18
- function f(s) {
19
- return !!s.getAttribute("disabled") || !!s.getAttribute("aria-disabled");
20
- }
21
18
  class u {
22
19
  #e = {};
23
20
  on(e, n) {
@@ -28,15 +25,15 @@ class u {
28
25
  return this.on(e, n);
29
26
  }
30
27
  once(e, n) {
31
- const t = (...r) => {
32
- this.off(e, t), n(...r);
28
+ const t = (...o) => {
29
+ this.off(e, t), n(...o);
33
30
  };
34
31
  return this.on(e, t), this;
35
32
  }
36
33
  emit(e, ...n) {
37
34
  const t = this.#e[e] || [];
38
- for (let r = 0; r < t.length; r++)
39
- t[r](...n);
35
+ for (let o = 0; o < t.length; o++)
36
+ t[o](...n);
40
37
  return !!t.length;
41
38
  }
42
39
  off(e, n) {
@@ -61,20 +58,20 @@ class u {
61
58
  class p extends u {
62
59
  constructor() {
63
60
  super();
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);
61
+ const { history: e, location: n } = window, { pushState: t, replaceState: o } = e;
62
+ e.pushState = (...r) => {
63
+ t.apply(e, r), this.emit("pushState", n, r[0]);
64
+ }, e.replaceState = (...r) => {
65
+ o.apply(e, r), this.emit("replaceState", n, r[0]);
66
+ }, window.addEventListener("popstate", (r) => {
67
+ this.emit("popState", n, r);
71
68
  });
72
69
  }
73
70
  }
74
71
  function l(s, e, n) {
75
- const t = new MutationObserver((r, o) => {
76
- for (const c of r)
77
- e(c, o);
72
+ const t = new MutationObserver((o, r) => {
73
+ for (const c of o)
74
+ e(c, r);
78
75
  });
79
76
  return t.observe(s, {
80
77
  childList: !0,
@@ -82,25 +79,21 @@ function l(s, e, n) {
82
79
  ...n
83
80
  }), () => t.disconnect();
84
81
  }
85
- function m(s, e = document.documentElement) {
82
+ function f(s, e = document.documentElement) {
86
83
  return new Promise((n) => {
87
- function t() {
84
+ l(e, (t, o) => {
88
85
  const r = e.querySelector(s);
89
- r && n(r);
90
- }
91
- l(e, (r, o) => {
92
- t(), o.disconnect();
86
+ r && (o.disconnect(), n(r));
93
87
  });
94
88
  });
95
89
  }
96
90
  export {
97
91
  p as LocationObserver,
98
- h as domReady,
92
+ d as domReady,
99
93
  a as el,
100
- f as isDisabled,
101
- d as nbsp,
94
+ h as nbsp,
102
95
  l as observeElement,
103
96
  i as text,
104
- m as waitElement
97
+ f as waitElement
105
98
  };
106
99
  //# sourceMappingURL=index.es.js.map
@@ -1 +1 @@
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,CAAC,IACjB,MAAM,QAAQA,CAAU,IACjCF,EAAG,OAAO,GAAGE,CAAU,KAEhB,OAAA,OAAOF,GAAIE,CAAU,GAC5B,OAAO,OAAOF,EAAG,OAAOE,GAAY,KAAK,IAGvCC,EAAS,UACXH,EAAG,OAAO,GAAGG,CAAQ,GAGhBH;AACT;AAEO,SAASI,EAAKC,GAAmB;AAC/B,SAAA,SAAS,eAAeA,CAAG;AACpC;AAEO,SAASC,IAAa;AAC3B,SAAOF,EAAK,GAAQ;AACtB;AAEA,eAAsBG,IAA0B;AACvC,SAAA,IAAI,QAAQ,CAACC,MAAY;AAC1B,IAAA,SAAS,cAAc,YACzB,SAAS,iBAAiB,oBAAoB,MAAMA,EAAA,GAAW;AAAA,MAC7D,MAAM;AAAA,IAAA,CACP,IAEOA;EACV,CACD;AACH;AAEO,SAASC,EAAWC,GAA+B;AACxD,SACE,EAAQA,EAAQ,aAAa,UAAU,KACvC,EAAQA,EAAQ,aAAa,eAAe;AAEhD;ACvDA,MAAMC,EAAE;AAAA,EACNC,KAAK,CAAA;AAAA,EACL,GAAGC,GAAGC,GAAG;AACP,UAAMC,IAAI,KAAKH,GAAGC,CAAC;AACnB,WAAOE,IAAIA,EAAE,KAAKD,CAAC,IAAI,KAAKF,GAAGC,CAAC,IAAI,CAACC,CAAC,GAAG;AAAA,EAC1C;AAAA,EACD,YAAYD,GAAGC,GAAG;AAChB,WAAO,KAAK,GAAGD,GAAGC,CAAC;AAAA,EACpB;AAAA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,KAAK,aAAaH,GAAUG,EAAK,CAAC,CAAC;AAAA,IAAA,GAGlCJ,EAAA,eAAe,IAAII,MAAS;AACrB,MAAAD,EAAA,MAAMH,GAASI,CAAI,GAChC,KAAK,KAAK,gBAAgBH,GAAUG,EAAK,CAAC,CAAC;AAAA,IAAA,GAGtC,OAAA,iBAAiB,YAAY,CAACC,MAAU;AACxC,WAAA,KAAK,YAAYJ,GAAUI,CAAK;AAAA,IAAA,CACtC;AAAA,EACH;AACF;AC/BgB,SAAAC,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;"}
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\n/**\n * Create an element\n * @param tag The tag name of the element to create\n * @param attributes The attributes or children to set on the element\n * @param children The children to append to the element\n * @returns The created element\n * @example\n * el('div', { id: 'foo' }, 'Hello world')\n * el('div', 'Hello world')\n * el('div', [el('span', 'Hello'), el('span', 'world')])\n * el('div', el('span', 'Hello world'))\n * el('div', el('span', 'Hello'), el('span', 'world'))\n * el('div', el('span', 'Hello world'), 'world')\n */\nexport function el<T extends keyof HTMLElementTagNameMap>(\n tag: T,\n attributes?: Attributes<T> | Children,\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\n/**\n * Create a text node\n * @param text The string to create a text node from\n */\nexport function text(text: string): Text {\n return document.createTextNode(text)\n}\n\n/**\n * A non-breaking space\n */\nexport function nbsp(): Text {\n return text('\\u00a0')\n}\n\n/**\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event\n * @returns A promise that resolves when the DOM is ready\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","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\n/**\n * Observe changes to the location\n * @example\n * const observer = new LocationObserver<{ id: string }>()\n * observer.on('pushState', (location, state) => {\n * console.log(state.id)\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\n/**\n * Observe mutations on an element\n * @param el The element to observe\n * @param callback The callback to call when a mutation occurs\n * @param options The options to pass to the `MutationObserver`\n * @returns A function to disconnect the observer\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\n/**\n * Wait for an element to appear in the DOM\n * @param selector The selector to wait for\n * @param target The element to search in\n * @returns A promise that resolves when the element is found\n */\nexport function waitElement<T extends Element = Element>(\n selector: string,\n target = document.documentElement\n): Promise<T> {\n return new Promise((resolve) => {\n observeElement(target, (_, observer) => {\n const el = target.querySelector<T>(selector)\n if (el) {\n observer.disconnect()\n resolve(el)\n }\n })\n })\n}\n"],"names":["el","tag","attributes","children","text","nbsp","domReady","resolve","n","#t","t","e","s","i","LocationObserver","Emitter","history","location","pushState","replaceState","args","event","observeElement","callback","options","observe","mutations","observer","mutation","waitElement","selector","target","_"],"mappings":"AAqBgB,SAAAA,EACdC,GACAC,MACGC,GACuB;AACpBH,QAAAA,IAAK,SAAS,cAAcC,CAAG;AAEjC,SAAA,OAAOC,KAAe,WACxBF,EAAG,OAAOI,EAAKF,CAAU,CAAC,IACjB,MAAM,QAAQA,CAAU,IACjCF,EAAG,OAAO,GAAGE,CAAU,KAEhB,OAAA,OAAOF,GAAIE,CAAU,GAC5B,OAAO,OAAOF,EAAG,OAAOE,GAAY,KAAK,IAGvCC,EAAS,UACXH,EAAG,OAAO,GAAGG,CAAQ,GAGhBH;AACT;AAMO,SAASI,EAAKA,GAAoB;AAChC,SAAA,SAAS,eAAeA,CAAI;AACrC;AAKO,SAASC,IAAa;AAC3B,SAAOD,EAAK,GAAQ;AACtB;AAMA,eAAsBE,IAA0B;AACvC,SAAA,IAAI,QAAQ,CAACC,MAAY;AAC1B,IAAA,SAAS,cAAc,YACzB,SAAS,iBAAiB,oBAAoB,MAAMA,EAAA,GAAW;AAAA,MAC7D,MAAM;AAAA,IAAA,CACP,IAEOA;EACV,CACD;AACH;ACzEA,MAAMC,EAAE;AAAA,EACNC,KAAK,CAAA;AAAA,EACL,GAAGC,GAAGC,GAAG;AACP,UAAMC,IAAI,KAAKH,GAAGC,CAAC;AACnB,WAAOE,IAAIA,EAAE,KAAKD,CAAC,IAAI,KAAKF,GAAGC,CAAC,IAAI,CAACC,CAAC,GAAG;AAAA,EAC1C;AAAA,EACD,YAAYD,GAAGC,GAAG;AAChB,WAAO,KAAK,GAAGD,GAAGC,CAAC;AAAA,EACpB;AAAA,EACD,KAAKD,GAAGC,GAAG;AACT,UAAMC,IAAI,IAAIC,MAAM;AAClB,WAAK,IAAIH,GAAGE,CAAC,GAAGD,EAAE,GAAGE,CAAC;AAAA,IAC5B;AACI,WAAO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K,gBAAgBH,GAAUG,EAAK,CAAC,CAAC;AAAA,IAAA,GAGtC,OAAA,iBAAiB,YAAY,CAACC,MAAU;AACxC,WAAA,KAAK,YAAYJ,GAAUI,CAAK;AAAA,IAAA,CACtC;AAAA,EACH;AACF;AChCgB,SAAAC,EACdtB,GACAuB,GACAC,GACY;AACZ,QAAMC,IAAU,IAAI,iBAAiB,CAACC,GAAWC,MAAa;AAC5D,eAAWC,KAAYF;AACrB,MAAAH,EAASK,GAAUD,CAAQ;AAAA,EAC7B,CACD;AAED,SAAAF,EAAQ,QAAQzB,GAAI;AAAA,IAClB,WAAW;AAAA,IACX,SAAS;AAAA,IACT,GAAGwB;AAAA,EAAA,CACJ,GAEM,MAAMC,EAAQ;AACvB;AAQO,SAASI,EACdC,GACAC,IAAS,SAAS,iBACN;AACL,SAAA,IAAI,QAAQ,CAACxB,MAAY;AACf,IAAAe,EAAAS,GAAQ,CAACC,GAAGL,MAAa;AAChC,YAAA3B,IAAK+B,EAAO,cAAiBD,CAAQ;AAC3C,MAAI9B,MACF2B,EAAS,WAAW,GACpBpB,EAAQP,CAAE;AAAA,IACZ,CACD;AAAA,EAAA,CACF;AACH;"}
package/dist/index.umd.js CHANGED
@@ -1,2 +1,2 @@
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"})});
1
+ (function(s,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(s=typeof globalThis<"u"?globalThis:s||self,c(s["@zero-dependency/dom"]={}))})(this,function(s){"use strict";function c(i,e,...n){const t=document.createElement(i);return typeof e=="string"?t.append(u(e)):Array.isArray(e)?t.append(...e):(Object.assign(t,e),Object.assign(t.style,e?.style)),n.length&&t.append(...n),t}function u(i){return document.createTextNode(i)}function l(){return u(" ")}async function a(){return new Promise(i=>{document.readyState=="loading"?document.addEventListener("DOMContentLoaded",()=>i(),{once:!0}):i()})}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 h 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 d(i,e,n){const t=new MutationObserver((r,o)=>{for(const p of r)e(p,o)});return t.observe(i,{childList:!0,subtree:!0,...n}),()=>t.disconnect()}function m(i,e=document.documentElement){return new Promise(n=>{d(e,(t,r)=>{const o=e.querySelector(i);o&&(r.disconnect(),n(o))})})}s.LocationObserver=h,s.domReady=a,s.el=c,s.nbsp=l,s.observeElement=d,s.text=u,s.waitElement=m,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
2
2
  //# sourceMappingURL=index.umd.js.map
@@ -1 +1 @@
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,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","../../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\n/**\n * Create an element\n * @param tag The tag name of the element to create\n * @param attributes The attributes or children to set on the element\n * @param children The children to append to the element\n * @returns The created element\n * @example\n * el('div', { id: 'foo' }, 'Hello world')\n * el('div', 'Hello world')\n * el('div', [el('span', 'Hello'), el('span', 'world')])\n * el('div', el('span', 'Hello world'))\n * el('div', el('span', 'Hello'), el('span', 'world'))\n * el('div', el('span', 'Hello world'), 'world')\n */\nexport function el<T extends keyof HTMLElementTagNameMap>(\n tag: T,\n attributes?: Attributes<T> | Children,\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\n/**\n * Create a text node\n * @param text The string to create a text node from\n */\nexport function text(text: string): Text {\n return document.createTextNode(text)\n}\n\n/**\n * A non-breaking space\n */\nexport function nbsp(): Text {\n return text('\\u00a0')\n}\n\n/**\n * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event\n * @returns A promise that resolves when the DOM is ready\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","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\n/**\n * Observe changes to the location\n * @example\n * const observer = new LocationObserver<{ id: string }>()\n * observer.on('pushState', (location, state) => {\n * console.log(state.id)\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\n/**\n * Observe mutations on an element\n * @param el The element to observe\n * @param callback The callback to call when a mutation occurs\n * @param options The options to pass to the `MutationObserver`\n * @returns A function to disconnect the observer\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\n/**\n * Wait for an element to appear in the DOM\n * @param selector The selector to wait for\n * @param target The element to search in\n * @returns A promise that resolves when the element is found\n */\nexport function waitElement<T extends Element = Element>(\n selector: string,\n target = document.documentElement\n): Promise<T> {\n return new Promise((resolve) => {\n observeElement(target, (_, observer) => {\n const el = target.querySelector<T>(selector)\n if (el) {\n observer.disconnect()\n resolve(el)\n }\n })\n })\n}\n"],"names":["el","tag","attributes","children","text","nbsp","domReady","resolve","n","#t","t","e","s","i","LocationObserver","Emitter","history","location","pushState","replaceState","args","event","observeElement","callback","options","observe","mutations","observer","mutation","waitElement","selector","target","_"],"mappings":"+OAqBgB,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,CAMO,SAASI,EAAKA,EAAoB,CAChC,OAAA,SAAS,eAAeA,CAAI,CACrC,CAKO,SAASC,GAAa,CAC3B,OAAOD,EAAK,GAAQ,CACtB,CAMA,eAAsBE,GAA0B,CACvC,OAAA,IAAI,QAASC,GAAY,CAC1B,SAAS,YAAc,UACzB,SAAS,iBAAiB,mBAAoB,IAAMA,EAAA,EAAW,CAC7D,KAAM,EAAA,CACP,EAEOA,GACV,CACD,CACH,CCzEA,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,CAAC,EAAG,IA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J,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,CChCgB,SAAAC,EACdtB,EACAuB,EACAC,EACY,CACZ,MAAMC,EAAU,IAAI,iBAAiB,CAACC,EAAWC,IAAa,CAC5D,UAAWC,KAAYF,EACrBH,EAASK,EAAUD,CAAQ,CAC7B,CACD,EAED,OAAAF,EAAQ,QAAQzB,EAAI,CAClB,UAAW,GACX,QAAS,GACT,GAAGwB,CAAA,CACJ,EAEM,IAAMC,EAAQ,YACvB,CAQO,SAASI,EACdC,EACAC,EAAS,SAAS,gBACN,CACL,OAAA,IAAI,QAASxB,GAAY,CACfe,EAAAS,EAAQ,CAACC,EAAGL,IAAa,CAChC,MAAA3B,EAAK+B,EAAO,cAAiBD,CAAQ,EACvC9B,IACF2B,EAAS,WAAW,EACpBpB,EAAQP,CAAE,EACZ,CACD,CAAA,CACF,CACH"}
@@ -7,6 +7,14 @@ type Events<T> = {
7
7
  readonly state: T;
8
8
  }>;
9
9
  };
10
+ /**
11
+ * Observe changes to the location
12
+ * @example
13
+ * const observer = new LocationObserver<{ id: string }>()
14
+ * observer.on('pushState', (location, state) => {
15
+ * console.log(state.id)
16
+ * })
17
+ */
10
18
  export declare class LocationObserver<T> extends Emitter<Events<T>> {
11
19
  constructor();
12
20
  }
@@ -1,4 +1,17 @@
1
1
  type Disconnect = () => void;
2
+ /**
3
+ * Observe mutations on an element
4
+ * @param el The element to observe
5
+ * @param callback The callback to call when a mutation occurs
6
+ * @param options The options to pass to the `MutationObserver`
7
+ * @returns A function to disconnect the observer
8
+ */
2
9
  export declare function observeElement<T extends Element = Element>(el: T, callback: (mutation: MutationRecord, observer: MutationObserver) => void, options?: MutationObserverInit): Disconnect;
10
+ /**
11
+ * Wait for an element to appear in the DOM
12
+ * @param selector The selector to wait for
13
+ * @param target The element to search in
14
+ * @returns A promise that resolves when the element is found
15
+ */
3
16
  export declare function waitElement<T extends Element = Element>(selector: string, target?: HTMLElement): Promise<T>;
4
17
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zero-dependency/dom",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "main": "./dist/index.umd.js",