@marianmeres/stuic 2.1.26 → 2.1.27
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/utils/index.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export * from "./is-nullish.js";
|
|
|
13
13
|
export * from "./maybe-json-parse.js";
|
|
14
14
|
export * from "./maybe-json-stringify.js";
|
|
15
15
|
export * from "./nl2br.js";
|
|
16
|
+
export * from "./observe-exists.svelte.js";
|
|
16
17
|
export * from "./omit-pick.js";
|
|
17
18
|
export * from "./oscillate.js";
|
|
18
19
|
export * from "./paint.js";
|
package/dist/utils/index.js
CHANGED
|
@@ -13,6 +13,7 @@ export * from "./is-nullish.js";
|
|
|
13
13
|
export * from "./maybe-json-parse.js";
|
|
14
14
|
export * from "./maybe-json-stringify.js";
|
|
15
15
|
export * from "./nl2br.js";
|
|
16
|
+
export * from "./observe-exists.svelte.js";
|
|
16
17
|
export * from "./omit-pick.js";
|
|
17
18
|
export * from "./oscillate.js";
|
|
18
19
|
export * from "./paint.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Will observe DOM changes and triggers callback when element presence changes */
|
|
2
|
+
export declare function observeExists(selector: string, options?: Partial<{
|
|
3
|
+
rootElement: HTMLElement;
|
|
4
|
+
}>): {
|
|
5
|
+
readonly current: boolean;
|
|
6
|
+
disconnect(): void;
|
|
7
|
+
forceCheck(): void;
|
|
8
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Will observe DOM changes and triggers callback when element presence changes */
|
|
2
|
+
export function observeExists(selector, options = {}) {
|
|
3
|
+
if (!selector)
|
|
4
|
+
throw new TypeError("Expecting non empty selector");
|
|
5
|
+
const { rootElement = document.body } = options;
|
|
6
|
+
const ns = `[observeExists] [${selector}]`;
|
|
7
|
+
const clog = (...args) => console.debug(ns, ...args);
|
|
8
|
+
const check = () => rootElement.querySelector(selector) !== null;
|
|
9
|
+
let current = $state(check());
|
|
10
|
+
//
|
|
11
|
+
const observer = new MutationObserver((mutations) => {
|
|
12
|
+
let shouldCheck = false;
|
|
13
|
+
for (const mutation of mutations) {
|
|
14
|
+
if (mutation.type === "childList") {
|
|
15
|
+
// was something added or removed?
|
|
16
|
+
if (mutation.addedNodes.length > 0 || mutation.removedNodes.length > 0) {
|
|
17
|
+
shouldCheck = true;
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (shouldCheck)
|
|
23
|
+
current = check();
|
|
24
|
+
});
|
|
25
|
+
// start observing now
|
|
26
|
+
clog(`connecting...`);
|
|
27
|
+
observer.observe(rootElement, { childList: true, subtree: true });
|
|
28
|
+
return {
|
|
29
|
+
get current() {
|
|
30
|
+
return current;
|
|
31
|
+
},
|
|
32
|
+
disconnect() {
|
|
33
|
+
clog(`disconnecting...`);
|
|
34
|
+
observer.disconnect();
|
|
35
|
+
},
|
|
36
|
+
forceCheck() {
|
|
37
|
+
check();
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|