UNPKG

@zag-js/dom-query

Version:

The dom helper library for zag.js machines

83 lines (81 loc) 2.82 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/mutation-observer.ts var mutation_observer_exports = {}; __export(mutation_observer_exports, { observeAttributes: () => observeAttributes, observeChildren: () => observeChildren }); module.exports = __toCommonJS(mutation_observer_exports); var import_raf = require("./raf.js"); 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 ? import_raf.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 ? import_raf.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?.()); }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { observeAttributes, observeChildren });