@zag-js/dom-query
Version:
The dom helper library for zag.js machines
59 lines (57 loc) • 1.72 kB
JavaScript
import "./chunk-QZ7TP4HQ.mjs";
// src/mutation-observer.ts
import { raf } from "./raf.mjs";
function observeAttributesImpl(node, options) {
if (!node) return;
const { attributes, callback: fn } = options;
const win = node.ownerDocument.defaultView || window;
const obs = new win.MutationObserver((changes) => {
for (const change of changes) {
if (change.type === "attributes" && change.attributeName && attributes.includes(change.attributeName)) {
fn(change);
}
}
});
obs.observe(node, { attributes: true, attributeFilter: attributes });
return () => obs.disconnect();
}
function observeAttributes(nodeOrFn, options) {
const { defer } = options;
const func = defer ? raf : (v) => v();
const cleanups = [];
cleanups.push(
func(() => {
const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
cleanups.push(observeAttributesImpl(node, options));
})
);
return () => {
cleanups.forEach((fn) => fn?.());
};
}
function observeChildrenImpl(node, options) {
const { callback: fn } = options;
if (!node) return;
const win = node.ownerDocument.defaultView || window;
const obs = new win.MutationObserver(fn);
obs.observe(node, { childList: true, subtree: true });
return () => obs.disconnect();
}
function observeChildren(nodeOrFn, options) {
const { defer } = options;
const func = defer ? raf : (v) => v();
const cleanups = [];
cleanups.push(
func(() => {
const node = typeof nodeOrFn === "function" ? nodeOrFn() : nodeOrFn;
cleanups.push(observeChildrenImpl(node, options));
})
);
return () => {
cleanups.forEach((fn) => fn?.());
};
}
export {
observeAttributes,
observeChildren
};