@zag-js/element-size
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -8,2 +8,9 @@ declare type ElementSize = { | ||
export { ElementSize, ElementSizeCallback, trackElementSize }; | ||
declare type TrackElementsSizeOptions<T extends HTMLElement | null> = { | ||
getNodes: () => T[]; | ||
observeMutation?: boolean; | ||
callback: (size: ElementSize | undefined, index: number) => void; | ||
}; | ||
declare function trackElementsSize<T extends HTMLElement | null>(options: TrackElementsSizeOptions<T>): () => void; | ||
export { ElementSize, ElementSizeCallback, trackElementSize, trackElementsSize }; |
@@ -23,5 +23,8 @@ "use strict"; | ||
__export(src_exports, { | ||
trackElementSize: () => trackElementSize | ||
trackElementSize: () => trackElementSize, | ||
trackElementsSize: () => trackElementsSize | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
// src/track-size.ts | ||
function trackElementSize(element, callback) { | ||
@@ -54,5 +57,46 @@ if (!element) { | ||
} | ||
// src/track-sizes.ts | ||
function trackElementsSize(options) { | ||
const { getNodes, observeMutation = true, callback } = options; | ||
const cleanups = []; | ||
let firstNode = null; | ||
function trigger() { | ||
const elements = getNodes(); | ||
firstNode = elements[0]; | ||
const fns = elements.map( | ||
(element, index) => trackElementSize(element, (size) => { | ||
callback(size, index); | ||
}) | ||
); | ||
cleanups.push(...fns); | ||
} | ||
trigger(); | ||
if (observeMutation) { | ||
const fn = trackMutation(firstNode, trigger); | ||
cleanups.push(fn); | ||
} | ||
return () => { | ||
cleanups.forEach((cleanup) => { | ||
cleanup == null ? void 0 : cleanup(); | ||
}); | ||
}; | ||
} | ||
function trackMutation(el, cb) { | ||
var _a; | ||
if (!el || !el.parentElement) | ||
return; | ||
const win = ((_a = el.ownerDocument) == null ? void 0 : _a.defaultView) ?? window; | ||
const observer = new win.MutationObserver(() => { | ||
cb(); | ||
}); | ||
observer.observe(el.parentElement, { childList: true }); | ||
return () => { | ||
observer.disconnect(); | ||
}; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
trackElementSize | ||
trackElementSize, | ||
trackElementsSize | ||
}); |
{ | ||
"name": "@zag-js/element-size", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Observer the size of an element over time", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8306
181