@zag-js/element-rect
Advanced tools
Comparing version 0.53.0 to 0.54.0
@@ -1,2 +0,93 @@ | ||
"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);var src_exports={};__export(src_exports,{trackElementRect:()=>trackElementRect});module.exports=__toCommonJS(src_exports);var rafId;var observedElements=new Map;var getRectFn=el=>el.getBoundingClientRect();function trackElementRect(el,options){const{scope="rect",getRect=getRectFn,onChange}=options;const loop=getLoopFn({scope,getRect});const data=observedElements.get(el);if(!data){observedElements.set(el,{rect:{},callbacks:[onChange]});if(observedElements.size===1){rafId=requestAnimationFrame(loop)}}else{data.callbacks.push(onChange);onChange(getRect(el))}return function unobserve(){const data2=observedElements.get(el);if(!data2)return;const index=data2.callbacks.indexOf(onChange);if(index>-1){data2.callbacks.splice(index,1)}if(data2.callbacks.length===0){observedElements.delete(el);if(observedElements.size===0){cancelAnimationFrame(rafId)}}}}function getLoopFn(options){const{scope,getRect}=options;const isEqual=getEqualityFn(scope);return function loop(){const changedRectsData=[];observedElements.forEach((data,element)=>{const newRect=getRect(element);if(!isEqual(data.rect,newRect)){data.rect=newRect;changedRectsData.push(data)}});changedRectsData.forEach(data=>{data.callbacks.forEach(callback=>callback(data.rect))});rafId=requestAnimationFrame(loop)}}var isEqualSize=(a,b)=>a.width===b.width&&a.height===b.height;var isEqualPosition=(a,b)=>a.top===b.top&&a.left===b.left;var isEqualRect=(a,b)=>isEqualSize(a,b)&&isEqualPosition(a,b);function getEqualityFn(scope){if(scope==="size")return isEqualSize;if(scope==="position")return isEqualPosition;return isEqualRect}0&&(module.exports={trackElementRect}); | ||
"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/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
trackElementRect: () => trackElementRect | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var rafId; | ||
var observedElements = /* @__PURE__ */ new Map(); | ||
var getRectFn = (el) => el.getBoundingClientRect(); | ||
function trackElementRect(el, options) { | ||
const { scope = "rect", getRect = getRectFn, onChange } = options; | ||
const loop = getLoopFn({ scope, getRect }); | ||
const data = observedElements.get(el); | ||
if (!data) { | ||
observedElements.set(el, { | ||
rect: {}, | ||
callbacks: [onChange] | ||
}); | ||
if (observedElements.size === 1) { | ||
rafId = requestAnimationFrame(loop); | ||
} | ||
} else { | ||
data.callbacks.push(onChange); | ||
onChange(getRect(el)); | ||
} | ||
return function unobserve() { | ||
const data2 = observedElements.get(el); | ||
if (!data2) | ||
return; | ||
const index = data2.callbacks.indexOf(onChange); | ||
if (index > -1) { | ||
data2.callbacks.splice(index, 1); | ||
} | ||
if (data2.callbacks.length === 0) { | ||
observedElements.delete(el); | ||
if (observedElements.size === 0) { | ||
cancelAnimationFrame(rafId); | ||
} | ||
} | ||
}; | ||
} | ||
function getLoopFn(options) { | ||
const { scope, getRect } = options; | ||
const isEqual = getEqualityFn(scope); | ||
return function loop() { | ||
const changedRectsData = []; | ||
observedElements.forEach((data, element) => { | ||
const newRect = getRect(element); | ||
if (!isEqual(data.rect, newRect)) { | ||
data.rect = newRect; | ||
changedRectsData.push(data); | ||
} | ||
}); | ||
changedRectsData.forEach((data) => { | ||
data.callbacks.forEach((callback) => callback(data.rect)); | ||
}); | ||
rafId = requestAnimationFrame(loop); | ||
}; | ||
} | ||
var isEqualSize = (a, b) => a.width === b.width && a.height === b.height; | ||
var isEqualPosition = (a, b) => a.top === b.top && a.left === b.left; | ||
var isEqualRect = (a, b) => isEqualSize(a, b) && isEqualPosition(a, b); | ||
function getEqualityFn(scope) { | ||
if (scope === "size") | ||
return isEqualSize; | ||
if (scope === "position") | ||
return isEqualPosition; | ||
return isEqualRect; | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
trackElementRect | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@zag-js/element-rect", | ||
"version": "0.53.0", | ||
"version": "0.54.0", | ||
"description": "observe element's rect over time", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
19091
249