@radix-ui/rect 0.1.1-rc.3 → 0.1.2-rc.2

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.
@@ -1 +1 @@
1
- {"mappings":"AAAA,yBAAkB;IAAE,qBAAqB,IAAI,UAAU,CAAA;CAAE,CAAC;AAE1D;A;A;A;GAIG;AACH;AACE,wCAAwC;AACxC,gBAAgB,EAAE,UAAU;AAC5B,8DAA8D;AAC9D,QAAQ,EAAE,UAAU,cAwCrB;AAKD,kBAAkB,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC","sources":["./packages/core/rect/src/packages/core/rect/src/observeElementRect.ts","./packages/core/rect/src/packages/core/rect/src/index.ts"],"sourcesContent":[null,null],"names":[],"version":3,"file":"index.d.ts.map"}
1
+ {"mappings":"AAAA,yBAAkB;IAAE,qBAAqB,IAAI,UAAU,CAAA;CAAE,CAAC;AAE1D;;;;GAIG;AACH;AACE,wCAAwC;AACxC,gBAAgB,EAAE,UAAU;AAC5B,8DAA8D;AAC9D,QAAQ,EAAE,UAAU,cAwCrB;AAKD,kBAAkB,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC","sources":["packages/core/rect/src/packages/core/rect/src/observeElementRect.ts","packages/core/rect/src/packages/core/rect/src/index.ts","packages/core/rect/src/index.ts"],"sourcesContent":[null,null,"export { observeElementRect } from './observeElementRect';\nexport type { Measurable } from './observeElementRect';\n"],"names":[],"version":3,"file":"index.d.ts.map"}
package/dist/index.js CHANGED
@@ -1,2 +1,70 @@
1
- let t;exports.observeElementRect=function(n,o){const i=e.get(n);return void 0===i?(e.set(n,{rect:{},callbacks:[o]}),1===e.size&&(t=requestAnimationFrame(c))):(i.callbacks.push(o),o(n.getBoundingClientRect())),()=>{const c=e.get(n);if(void 0===c)return;const i=c.callbacks.indexOf(o);i>-1&&c.callbacks.splice(i,1),0===c.callbacks.length&&(e.delete(n),0===e.size&&cancelAnimationFrame(t))}};const e=new Map;function c(){const n=[];e.forEach(((t,e)=>{const c=e.getBoundingClientRect();var o,i;o=t.rect,i=c,(o.width!==i.width||o.height!==i.height||o.top!==i.top||o.right!==i.right||o.bottom!==i.bottom||o.left!==i.left)&&(t.rect=c,n.push(t))})),n.forEach((t=>{t.callbacks.forEach((e=>e(t.rect)))})),t=requestAnimationFrame(c)}
1
+ function $parcel$export(e, n, v, s) {
2
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
3
+ }
4
+
5
+ $parcel$export(module.exports, "observeElementRect", () => $f98399f7d3345a24$export$5a50ff2cde8c3802);
6
+ /**
7
+ * Observes an element's rectangle on screen (getBoundingClientRect)
8
+ * This is useful to track elements on the screen and attach other elements
9
+ * that might be in different layers, etc.
10
+ */ function $f98399f7d3345a24$export$5a50ff2cde8c3802(/** The element whose rect to observe */ elementToObserve, /** The callback which will be called when the rect changes */ callback) {
11
+ const observedData1 = $f98399f7d3345a24$var$observedElements.get(elementToObserve);
12
+ if (observedData1 === undefined) {
13
+ // add the element to the map of observed elements with its first callback
14
+ // because this is the first time this element is observed
15
+ $f98399f7d3345a24$var$observedElements.set(elementToObserve, {
16
+ rect: {},
17
+ callbacks: [
18
+ callback
19
+ ]
20
+ });
21
+ if ($f98399f7d3345a24$var$observedElements.size === 1) // start the internal loop once at least 1 element is observed
22
+ $f98399f7d3345a24$var$rafId = requestAnimationFrame($f98399f7d3345a24$var$runLoop);
23
+ } else {
24
+ // only add a callback for this element as it's already observed
25
+ observedData1.callbacks.push(callback);
26
+ callback(elementToObserve.getBoundingClientRect());
27
+ }
28
+ return ()=>{
29
+ const observedData = $f98399f7d3345a24$var$observedElements.get(elementToObserve);
30
+ if (observedData === undefined) return; // start by removing the callback
31
+ const index = observedData.callbacks.indexOf(callback);
32
+ if (index > -1) observedData.callbacks.splice(index, 1);
33
+ if (observedData.callbacks.length === 0) {
34
+ // stop observing this element because there are no
35
+ // callbacks registered for it anymore
36
+ $f98399f7d3345a24$var$observedElements.delete(elementToObserve);
37
+ if ($f98399f7d3345a24$var$observedElements.size === 0) // stop the internal loop once no elements are observed anymore
38
+ cancelAnimationFrame($f98399f7d3345a24$var$rafId);
39
+ }
40
+ };
41
+ } // ========================================================================
42
+ // module internals
43
+ let $f98399f7d3345a24$var$rafId;
44
+ const $f98399f7d3345a24$var$observedElements = new Map();
45
+ function $f98399f7d3345a24$var$runLoop() {
46
+ const changedRectsData = []; // process all DOM reads first (getBoundingClientRect)
47
+ $f98399f7d3345a24$var$observedElements.forEach((data, element)=>{
48
+ const newRect = element.getBoundingClientRect(); // gather all the data for elements whose rects have changed
49
+ if (!$f98399f7d3345a24$var$rectEquals(data.rect, newRect)) {
50
+ data.rect = newRect;
51
+ changedRectsData.push(data);
52
+ }
53
+ }); // group DOM writes here after the DOM reads (getBoundingClientRect)
54
+ // as DOM writes will most likely happen with the callbacks
55
+ changedRectsData.forEach((data)=>{
56
+ data.callbacks.forEach((callback)=>callback(data.rect)
57
+ );
58
+ });
59
+ $f98399f7d3345a24$var$rafId = requestAnimationFrame($f98399f7d3345a24$var$runLoop);
60
+ } // ========================================================================
61
+ /**
62
+ * Returns whether 2 rects are equal in values
63
+ */ function $f98399f7d3345a24$var$rectEquals(rect1, rect2) {
64
+ return rect1.width === rect2.width && rect1.height === rect2.height && rect1.top === rect2.top && rect1.right === rect2.right && rect1.bottom === rect2.bottom && rect1.left === rect2.left;
65
+ }
66
+
67
+
68
+
69
+
2
70
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":"AA+DA,IAAIA,6BAxDJ,SAEEC,EAEAC,GAEA,MAAMC,EAAeC,EAAiBC,IAAIJ,GAiB1C,YAfqBK,IAAjBH,GAGFC,EAAiBG,IAAIN,EAAkB,CAAEO,KAAM,GAAkBC,UAAW,CAACP,KAE/C,IAA1BE,EAAiBM,OAEnBV,EAAQW,sBAAsBC,MAIhCT,EAAaM,UAAUI,KAAKX,GAC5BA,EAASD,EAAiBa,0BAGrB,KACL,MAAMX,EAAeC,EAAiBC,IAAIJ,GAC1C,QAAqBK,IAAjBH,EAA4B,OAGhC,MAAMY,EAAQZ,EAAaM,UAAUO,QAAQd,GACzCa,GAAS,GACXZ,EAAaM,UAAUQ,OAAOF,EAAO,GAGD,IAAlCZ,EAAaM,UAAUS,SAGzBd,EAAiBe,OAAOlB,GAEM,IAA1BG,EAAiBM,MAEnBU,qBAAqBpB,MAiB7B,MAAMI,EAAkD,IAAIiB,IAE5D,SAAST,IACP,MAAMU,EAAwC,GAG9ClB,EAAiBmB,SAAQ,CAACC,EAAMC,KAC9B,MAAMC,EAAUD,EAAQX,wBAsB5B,IAAoBa,EAAmBC,EAAnBD,EAnBAH,EAAKhB,KAmBcoB,EAnBRF,GAqB3BC,EAAME,QAAUD,EAAMC,OACtBF,EAAMG,SAAWF,EAAME,QACvBH,EAAMI,MAAQH,EAAMG,KACpBJ,EAAMK,QAAUJ,EAAMI,OACtBL,EAAMM,SAAWL,EAAMK,QACvBN,EAAMO,OAASN,EAAMM,QAzBnBV,EAAKhB,KAAOkB,EACZJ,EAAiBT,KAAKW,OAM1BF,EAAiBC,SAASC,IACxBA,EAAKf,UAAUc,SAASrB,GAAaA,EAASsB,EAAKhB,WAGrDR,EAAQW,sBAAsBC","sources":["./packages/core/rect/src/observeElementRect.ts"],"sourcesContent":["type Measurable = { getBoundingClientRect(): ClientRect };\n\n/**\n * Observes an element's rectangle on screen (getBoundingClientRect)\n * This is useful to track elements on the screen and attach other elements\n * that might be in different layers, etc.\n */\nfunction observeElementRect(\n /** The element whose rect to observe */\n elementToObserve: Measurable,\n /** The callback which will be called when the rect changes */\n callback: CallbackFn\n) {\n const observedData = observedElements.get(elementToObserve);\n\n if (observedData === undefined) {\n // add the element to the map of observed elements with its first callback\n // because this is the first time this element is observed\n observedElements.set(elementToObserve, { rect: {} as ClientRect, callbacks: [callback] });\n\n if (observedElements.size === 1) {\n // start the internal loop once at least 1 element is observed\n rafId = requestAnimationFrame(runLoop);\n }\n } else {\n // only add a callback for this element as it's already observed\n observedData.callbacks.push(callback);\n callback(elementToObserve.getBoundingClientRect());\n }\n\n return () => {\n const observedData = observedElements.get(elementToObserve);\n if (observedData === undefined) return;\n\n // start by removing the callback\n const index = observedData.callbacks.indexOf(callback);\n if (index > -1) {\n observedData.callbacks.splice(index, 1);\n }\n\n if (observedData.callbacks.length === 0) {\n // stop observing this element because there are no\n // callbacks registered for it anymore\n observedElements.delete(elementToObserve);\n\n if (observedElements.size === 0) {\n // stop the internal loop once no elements are observed anymore\n cancelAnimationFrame(rafId);\n }\n }\n };\n}\n\n// ========================================================================\n// module internals\n\ntype CallbackFn = (rect: ClientRect) => void;\n\ntype ObservedData = {\n rect: ClientRect;\n callbacks: Array<CallbackFn>;\n};\n\nlet rafId: number;\nconst observedElements: Map<Measurable, ObservedData> = new Map();\n\nfunction runLoop() {\n const changedRectsData: Array<ObservedData> = [];\n\n // process all DOM reads first (getBoundingClientRect)\n observedElements.forEach((data, element) => {\n const newRect = element.getBoundingClientRect();\n\n // gather all the data for elements whose rects have changed\n if (!rectEquals(data.rect, newRect)) {\n data.rect = newRect;\n changedRectsData.push(data);\n }\n });\n\n // group DOM writes here after the DOM reads (getBoundingClientRect)\n // as DOM writes will most likely happen with the callbacks\n changedRectsData.forEach((data) => {\n data.callbacks.forEach((callback) => callback(data.rect));\n });\n\n rafId = requestAnimationFrame(runLoop);\n}\n// ========================================================================\n\n/**\n * Returns whether 2 rects are equal in values\n */\nfunction rectEquals(rect1: ClientRect, rect2: ClientRect) {\n return (\n rect1.width === rect2.width &&\n rect1.height === rect2.height &&\n rect1.top === rect2.top &&\n rect1.right === rect2.right &&\n rect1.bottom === rect2.bottom &&\n rect1.left === rect2.left\n );\n}\n\nexport { observeElementRect };\nexport type { Measurable };\n"],"names":["rafId","elementToObserve","callback","observedData","observedElements","get","undefined","set","rect","callbacks","size","requestAnimationFrame","runLoop","push","getBoundingClientRect","index","indexOf","splice","length","delete","cancelAnimationFrame","Map","changedRectsData","forEach","data","element","newRect","rect1","rect2","width","height","top","right","bottom","left"],"version":3,"file":"index.js.map"}
1
+ {"mappings":";;;;;ACEA;;;;GAIA,CACA,SAASA,yCAAT,CACE,wCAAA,CACAC,gBAFF,EAGE,8DAAA,CACAC,QAJF,EAKE;IACA,MAAMC,aAAY,GAAGC,sCAAgB,CAACC,GAAjB,CAAqBJ,gBAArB,CAArB,AAAA;IAEA,IAAIE,aAAY,KAAKG,SAArB,EAAgC;QAC9B,0EAAA;QACA,0DAAA;QACAF,sCAAgB,CAACG,GAAjB,CAAqBN,gBAArB,EAAuC;YAAEO,IAAI,EAAE,EAAR;YAA0BC,SAAS,EAAE;gBAACP,QAAD;aAAXO;SAAjE,CAAuC,CAAA;QAEvC,IAAIL,sCAAgB,CAACM,IAAjB,KAA0B,CAA9B,EACE,8DAAA;QACAC,2BAAK,GAAGC,qBAAqB,CAACC,6BAAD,CAA7B,CAAAF;KAPJ,MASO;QACL,gEAAA;QACAR,aAAY,CAACM,SAAb,CAAuBK,IAAvB,CAA4BZ,QAA5B,CAAAC,CAAAA;QACAD,QAAQ,CAACD,gBAAgB,CAACc,qBAAjB,EAAD,CAAR,CAAAb;KACD;IAED,OAAO,IAAM;QACX,MAAMC,YAAY,GAAGC,sCAAgB,CAACC,GAAjB,CAAqBJ,gBAArB,CAArB,AAAA;QACA,IAAIE,YAAY,KAAKG,SAArB,EAAgC,OAFrB,CAIX,iCAFA;QAGA,MAAMU,KAAK,GAAGb,YAAY,CAACM,SAAb,CAAuBQ,OAAvB,CAA+Bf,QAA/B,CAAd,AAAA;QACA,IAAIc,KAAK,GAAG,EAAZ,EACEb,YAAY,CAACM,SAAb,CAAuBS,MAAvB,CAA8BF,KAA9B,EAAqC,CAArC,CAAAb,CAAAA;QAGF,IAAIA,YAAY,CAACM,SAAb,CAAuBU,MAAvB,KAAkC,CAAtC,EAAyC;YACvC,mDAAA;YACA,sCAAA;YACAf,sCAAgB,CAACgB,MAAjB,CAAwBnB,gBAAxB,CAA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ACK,KAAN,KAAgBJ,KAAK,CAACI,KAHtB,IAIAL,KAAK,CAACM,MAAN,KAAiBL,KAAK,CAACK,MAJvB,IAKAN,KAAK,CAACO,IAAN,KAAeN,KAAK,CAACM,IANvB,CAAA;CAQD;;ADtGD","sources":["packages/core/rect/src/index.ts","packages/core/rect/src/observeElementRect.ts"],"sourcesContent":["export { observeElementRect } from './observeElementRect';\nexport type { Measurable } from './observeElementRect';\n","type Measurable = { getBoundingClientRect(): ClientRect };\n\n/**\n * Observes an element's rectangle on screen (getBoundingClientRect)\n * This is useful to track elements on the screen and attach other elements\n * that might be in different layers, etc.\n */\nfunction observeElementRect(\n /** The element whose rect to observe */\n elementToObserve: Measurable,\n /** The callback which will be called when the rect changes */\n callback: CallbackFn\n) {\n const observedData = observedElements.get(elementToObserve);\n\n if (observedData === undefined) {\n // add the element to the map of observed elements with its first callback\n // because this is the first time this element is observed\n observedElements.set(elementToObserve, { rect: {} as ClientRect, callbacks: [callback] });\n\n if (observedElements.size === 1) {\n // start the internal loop once at least 1 element is observed\n rafId = requestAnimationFrame(runLoop);\n }\n } else {\n // only add a callback for this element as it's already observed\n observedData.callbacks.push(callback);\n callback(elementToObserve.getBoundingClientRect());\n }\n\n return () => {\n const observedData = observedElements.get(elementToObserve);\n if (observedData === undefined) return;\n\n // start by removing the callback\n const index = observedData.callbacks.indexOf(callback);\n if (index > -1) {\n observedData.callbacks.splice(index, 1);\n }\n\n if (observedData.callbacks.length === 0) {\n // stop observing this element because there are no\n // callbacks registered for it anymore\n observedElements.delete(elementToObserve);\n\n if (observedElements.size === 0) {\n // stop the internal loop once no elements are observed anymore\n cancelAnimationFrame(rafId);\n }\n }\n };\n}\n\n// ========================================================================\n// module internals\n\ntype CallbackFn = (rect: ClientRect) => void;\n\ntype ObservedData = {\n rect: ClientRect;\n callbacks: Array<CallbackFn>;\n};\n\nlet rafId: number;\nconst observedElements: Map<Measurable, ObservedData> = new Map();\n\nfunction runLoop() {\n const changedRectsData: Array<ObservedData> = [];\n\n // process all DOM reads first (getBoundingClientRect)\n observedElements.forEach((data, element) => {\n const newRect = element.getBoundingClientRect();\n\n // gather all the data for elements whose rects have changed\n if (!rectEquals(data.rect, newRect)) {\n data.rect = newRect;\n changedRectsData.push(data);\n }\n });\n\n // group DOM writes here after the DOM reads (getBoundingClientRect)\n // as DOM writes will most likely happen with the callbacks\n changedRectsData.forEach((data) => {\n data.callbacks.forEach((callback) => callback(data.rect));\n });\n\n rafId = requestAnimationFrame(runLoop);\n}\n// ========================================================================\n\n/**\n * Returns whether 2 rects are equal in values\n */\nfunction rectEquals(rect1: ClientRect, rect2: ClientRect) {\n return (\n rect1.width === rect2.width &&\n rect1.height === rect2.height &&\n rect1.top === rect2.top &&\n rect1.right === rect2.right &&\n rect1.bottom === rect2.bottom &&\n rect1.left === rect2.left\n );\n}\n\nexport { observeElementRect };\nexport type { Measurable };\n"],"names":["observeElementRect","elementToObserve","callback","observedData","observedElements","get","undefined","set","rect","callbacks","size","rafId","requestAnimationFrame","runLoop","push","getBoundingClientRect","index","indexOf","splice","length","delete","cancelAnimationFrame","Map","changedRectsData","forEach","data","element","newRect","rectEquals","rect1","rect2","width","height","top","right","bottom","left"],"version":3,"file":"index.js.map"}
@@ -1,2 +1,66 @@
1
- export function observeElementRect(n,o){const i=e.get(n);return void 0===i?(e.set(n,{rect:{},callbacks:[o]}),1===e.size&&(t=requestAnimationFrame(c))):(i.callbacks.push(o),o(n.getBoundingClientRect())),()=>{const c=e.get(n);if(void 0===c)return;const i=c.callbacks.indexOf(o);i>-1&&c.callbacks.splice(i,1),0===c.callbacks.length&&(e.delete(n),0===e.size&&cancelAnimationFrame(t))}}let t;const e=new Map;function c(){const n=[];e.forEach(((t,e)=>{const c=e.getBoundingClientRect();var o,i;o=t.rect,i=c,(o.width!==i.width||o.height!==i.height||o.top!==i.top||o.right!==i.right||o.bottom!==i.bottom||o.left!==i.left)&&(t.rect=c,n.push(t))})),n.forEach((t=>{t.callbacks.forEach((e=>e(t.rect)))})),t=requestAnimationFrame(c)}
1
+ /**
2
+ * Observes an element's rectangle on screen (getBoundingClientRect)
3
+ * This is useful to track elements on the screen and attach other elements
4
+ * that might be in different layers, etc.
5
+ */ function $0f3bbd680c63c15c$export$5a50ff2cde8c3802(/** The element whose rect to observe */ elementToObserve, /** The callback which will be called when the rect changes */ callback) {
6
+ const observedData1 = $0f3bbd680c63c15c$var$observedElements.get(elementToObserve);
7
+ if (observedData1 === undefined) {
8
+ // add the element to the map of observed elements with its first callback
9
+ // because this is the first time this element is observed
10
+ $0f3bbd680c63c15c$var$observedElements.set(elementToObserve, {
11
+ rect: {},
12
+ callbacks: [
13
+ callback
14
+ ]
15
+ });
16
+ if ($0f3bbd680c63c15c$var$observedElements.size === 1) // start the internal loop once at least 1 element is observed
17
+ $0f3bbd680c63c15c$var$rafId = requestAnimationFrame($0f3bbd680c63c15c$var$runLoop);
18
+ } else {
19
+ // only add a callback for this element as it's already observed
20
+ observedData1.callbacks.push(callback);
21
+ callback(elementToObserve.getBoundingClientRect());
22
+ }
23
+ return ()=>{
24
+ const observedData = $0f3bbd680c63c15c$var$observedElements.get(elementToObserve);
25
+ if (observedData === undefined) return; // start by removing the callback
26
+ const index = observedData.callbacks.indexOf(callback);
27
+ if (index > -1) observedData.callbacks.splice(index, 1);
28
+ if (observedData.callbacks.length === 0) {
29
+ // stop observing this element because there are no
30
+ // callbacks registered for it anymore
31
+ $0f3bbd680c63c15c$var$observedElements.delete(elementToObserve);
32
+ if ($0f3bbd680c63c15c$var$observedElements.size === 0) // stop the internal loop once no elements are observed anymore
33
+ cancelAnimationFrame($0f3bbd680c63c15c$var$rafId);
34
+ }
35
+ };
36
+ } // ========================================================================
37
+ // module internals
38
+ let $0f3bbd680c63c15c$var$rafId;
39
+ const $0f3bbd680c63c15c$var$observedElements = new Map();
40
+ function $0f3bbd680c63c15c$var$runLoop() {
41
+ const changedRectsData = []; // process all DOM reads first (getBoundingClientRect)
42
+ $0f3bbd680c63c15c$var$observedElements.forEach((data, element)=>{
43
+ const newRect = element.getBoundingClientRect(); // gather all the data for elements whose rects have changed
44
+ if (!$0f3bbd680c63c15c$var$rectEquals(data.rect, newRect)) {
45
+ data.rect = newRect;
46
+ changedRectsData.push(data);
47
+ }
48
+ }); // group DOM writes here after the DOM reads (getBoundingClientRect)
49
+ // as DOM writes will most likely happen with the callbacks
50
+ changedRectsData.forEach((data)=>{
51
+ data.callbacks.forEach((callback)=>callback(data.rect)
52
+ );
53
+ });
54
+ $0f3bbd680c63c15c$var$rafId = requestAnimationFrame($0f3bbd680c63c15c$var$runLoop);
55
+ } // ========================================================================
56
+ /**
57
+ * Returns whether 2 rects are equal in values
58
+ */ function $0f3bbd680c63c15c$var$rectEquals(rect1, rect2) {
59
+ return rect1.width === rect2.width && rect1.height === rect2.height && rect1.top === rect2.top && rect1.right === rect2.right && rect1.bottom === rect2.bottom && rect1.left === rect2.left;
60
+ }
61
+
62
+
63
+
64
+
65
+ export {$0f3bbd680c63c15c$export$5a50ff2cde8c3802 as observeElementRect};
2
66
  //# sourceMappingURL=index.module.js.map
@@ -1 +1 @@
1
- {"mappings":"OAOA,SAASA,mBAEPC,EAEAC,GAEA,MAAMC,EAAeC,EAAiBC,IAAIJ,GAiB1C,YAfqBK,IAAjBH,GAGFC,EAAiBG,IAAIN,EAAkB,CAAEO,KAAM,GAAkBC,UAAW,CAACP,KAE/C,IAA1BE,EAAiBM,OAEnBC,EAAQC,sBAAsBC,MAIhCV,EAAaM,UAAUK,KAAKZ,GAC5BA,EAASD,EAAiBc,0BAGrB,KACL,MAAMZ,EAAeC,EAAiBC,IAAIJ,GAC1C,QAAqBK,IAAjBH,EAA4B,OAGhC,MAAMa,EAAQb,EAAaM,UAAUQ,QAAQf,GACzCc,GAAS,GACXb,EAAaM,UAAUS,OAAOF,EAAO,GAGD,IAAlCb,EAAaM,UAAUU,SAGzBf,EAAiBgB,OAAOnB,GAEM,IAA1BG,EAAiBM,MAEnBW,qBAAqBV,KAgB7B,IAAIA,EACJ,MAAMP,EAAkD,IAAIkB,IAE5D,SAAST,IACP,MAAMU,EAAwC,GAG9CnB,EAAiBoB,SAAQ,CAACC,EAAMC,KAC9B,MAAMC,EAAUD,EAAQX,wBAsB5B,IAAoBa,EAAmBC,EAAnBD,EAnBAH,EAAKjB,KAmBcqB,EAnBRF,GAqB3BC,EAAME,QAAUD,EAAMC,OACtBF,EAAMG,SAAWF,EAAME,QACvBH,EAAMI,MAAQH,EAAMG,KACpBJ,EAAMK,QAAUJ,EAAMI,OACtBL,EAAMM,SAAWL,EAAMK,QACvBN,EAAMO,OAASN,EAAMM,QAzBnBV,EAAKjB,KAAOmB,EACZJ,EAAiBT,KAAKW,OAM1BF,EAAiBC,SAASC,IACxBA,EAAKhB,UAAUe,SAAStB,GAAaA,EAASuB,EAAKjB,WAGrDG,EAAQC,sBAAsBC","sources":["./packages/core/rect/src/observeElementRect.ts"],"sourcesContent":["type Measurable = { getBoundingClientRect(): ClientRect };\n\n/**\n * Observes an element's rectangle on screen (getBoundingClientRect)\n * This is useful to track elements on the screen and attach other elements\n * that might be in different layers, etc.\n */\nfunction observeElementRect(\n /** The element whose rect to observe */\n elementToObserve: Measurable,\n /** The callback which will be called when the rect changes */\n callback: CallbackFn\n) {\n const observedData = observedElements.get(elementToObserve);\n\n if (observedData === undefined) {\n // add the element to the map of observed elements with its first callback\n // because this is the first time this element is observed\n observedElements.set(elementToObserve, { rect: {} as ClientRect, callbacks: [callback] });\n\n if (observedElements.size === 1) {\n // start the internal loop once at least 1 element is observed\n rafId = requestAnimationFrame(runLoop);\n }\n } else {\n // only add a callback for this element as it's already observed\n observedData.callbacks.push(callback);\n callback(elementToObserve.getBoundingClientRect());\n }\n\n return () => {\n const observedData = observedElements.get(elementToObserve);\n if (observedData === undefined) return;\n\n // start by removing the callback\n const index = observedData.callbacks.indexOf(callback);\n if (index > -1) {\n observedData.callbacks.splice(index, 1);\n }\n\n if (observedData.callbacks.length === 0) {\n // stop observing this element because there are no\n // callbacks registered for it anymore\n observedElements.delete(elementToObserve);\n\n if (observedElements.size === 0) {\n // stop the internal loop once no elements are observed anymore\n cancelAnimationFrame(rafId);\n }\n }\n };\n}\n\n// ========================================================================\n// module internals\n\ntype CallbackFn = (rect: ClientRect) => void;\n\ntype ObservedData = {\n rect: ClientRect;\n callbacks: Array<CallbackFn>;\n};\n\nlet rafId: number;\nconst observedElements: Map<Measurable, ObservedData> = new Map();\n\nfunction runLoop() {\n const changedRectsData: Array<ObservedData> = [];\n\n // process all DOM reads first (getBoundingClientRect)\n observedElements.forEach((data, element) => {\n const newRect = element.getBoundingClientRect();\n\n // gather all the data for elements whose rects have changed\n if (!rectEquals(data.rect, newRect)) {\n data.rect = newRect;\n changedRectsData.push(data);\n }\n });\n\n // group DOM writes here after the DOM reads (getBoundingClientRect)\n // as DOM writes will most likely happen with the callbacks\n changedRectsData.forEach((data) => {\n data.callbacks.forEach((callback) => callback(data.rect));\n });\n\n rafId = requestAnimationFrame(runLoop);\n}\n// ========================================================================\n\n/**\n * Returns whether 2 rects are equal in values\n */\nfunction rectEquals(rect1: ClientRect, rect2: ClientRect) {\n return (\n rect1.width === rect2.width &&\n rect1.height === rect2.height &&\n rect1.top === rect2.top &&\n rect1.right === rect2.right &&\n rect1.bottom === rect2.bottom &&\n rect1.left === rect2.left\n );\n}\n\nexport { observeElementRect };\nexport type { Measurable };\n"],"names":["observeElementRect","elementToObserve","callback","observedData","observedElements","get","undefined","set","rect","callbacks","size","rafId","requestAnimationFrame","runLoop","push","getBoundingClientRect","index","indexOf","splice","length","delete","cancelAnimationFrame","Map","changedRectsData","forEach","data","element","newRect","rect1","rect2","width","height","top","right","bottom","left"],"version":3,"file":"index.module.js.map"}
1
+ {"mappings":"ACEA;;;;GAIA,CACA,SAASA,yCAAT,CACE,wCAAA,CACAC,gBAFF,EAGE,8DAAA,CACAC,QAJF,EAKE;IACA,MAAMC,aAAY,GAAGC,sCAAgB,CAACC,GAAjB,CAAqBJ,gBAArB,CAArB,AAAA;IAEA,IAA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BAAT,GAAmB;IACjB,MAAMU,gBAAqC,GAAG,EAA9C,AADiB,EAGjB,sDAFA;IAGAnB,sCAAgB,CAACoB,OAAjB,CAAyB,CAACC,IAAD,EAAOC,OAAP,GAAmB;QAC1C,MAAMC,OAAO,GAAGD,OAAO,CAACX,qBAAR,EAAhB,AAD0C,EAG1C,4DAFA;QAGA,IAAI,CAACa,gCAAU,CAACH,IAAI,CAACjB,IAAN,EAAYmB,OAAZ,CAAf,EAAqC;YACnCF,IAAI,CAACjB,IAAL,GAAYmB,OAAZ,CAAAF;YACAF,gBAAgB,CAACT,IAAjB,CAAsBW,IAAtB,CAAAF,CAAAA;SACD;KAPH,CAAA,CAJiB,CAcjB,oEAFC;IAGD,2DAAA;IACAA,gBAAgB,CAACC,OAAjB,CAA0BC,CAAAA,IAAD,GAAU;QACjCA,IAAI,CAAChB,SAAL,CAAee,OAAf,CAAwBtB,CAAAA,QAAD,GAAcA,QAAQ,CAACuB,IAAI,CAACjB,IAAN,CAA7C;QAAA,CAAAiB,CAAAA;KADF,CAEC,CAAA;IAEDd,2BAAK,GAAGC,qBAAqB,CAACC,6BAAD,CAA7B,CAAAF;C,CAEF,2EADC;AAGD;;GAEA,CACA,SAASiB,gCAAT,CAAoBC,KAApB,EAAuCC,KAAvC,EAA0D;IACxD,OACED,KAAK,CAACE,KAAN,KAAgBD,KAAK,CAACC,KAAtB,IACAF,KAAK,CAACG,MAAN,KAAiBF,KAAK,CAACE,MADvB,IAEAH,KAAK,CAACI,GAAN,KAAcH,KAAK,CAACG,GAFpB,IAGAJ,KAAK,CAACK,KAAN,KAAgBJ,KAAK,CAACI,KAHtB,IAIAL,KAAK,CAACM,MAAN,KAAiBL,KAAK,CAACK,MAJvB,IAKAN,KAAK,CAACO,IAAN,KAAeN,KAAK,CAACM,IANvB,CAAA;CAQD;;ADtGD","sources":["packages/core/rect/src/index.ts","packages/core/rect/src/observeElementRect.ts"],"sourcesContent":["export { observeElementRect } from './observeElementRect';\nexport type { Measurable } from './observeElementRect';\n","type Measurable = { getBoundingClientRect(): ClientRect };\n\n/**\n * Observes an element's rectangle on screen (getBoundingClientRect)\n * This is useful to track elements on the screen and attach other elements\n * that might be in different layers, etc.\n */\nfunction observeElementRect(\n /** The element whose rect to observe */\n elementToObserve: Measurable,\n /** The callback which will be called when the rect changes */\n callback: CallbackFn\n) {\n const observedData = observedElements.get(elementToObserve);\n\n if (observedData === undefined) {\n // add the element to the map of observed elements with its first callback\n // because this is the first time this element is observed\n observedElements.set(elementToObserve, { rect: {} as ClientRect, callbacks: [callback] });\n\n if (observedElements.size === 1) {\n // start the internal loop once at least 1 element is observed\n rafId = requestAnimationFrame(runLoop);\n }\n } else {\n // only add a callback for this element as it's already observed\n observedData.callbacks.push(callback);\n callback(elementToObserve.getBoundingClientRect());\n }\n\n return () => {\n const observedData = observedElements.get(elementToObserve);\n if (observedData === undefined) return;\n\n // start by removing the callback\n const index = observedData.callbacks.indexOf(callback);\n if (index > -1) {\n observedData.callbacks.splice(index, 1);\n }\n\n if (observedData.callbacks.length === 0) {\n // stop observing this element because there are no\n // callbacks registered for it anymore\n observedElements.delete(elementToObserve);\n\n if (observedElements.size === 0) {\n // stop the internal loop once no elements are observed anymore\n cancelAnimationFrame(rafId);\n }\n }\n };\n}\n\n// ========================================================================\n// module internals\n\ntype CallbackFn = (rect: ClientRect) => void;\n\ntype ObservedData = {\n rect: ClientRect;\n callbacks: Array<CallbackFn>;\n};\n\nlet rafId: number;\nconst observedElements: Map<Measurable, ObservedData> = new Map();\n\nfunction runLoop() {\n const changedRectsData: Array<ObservedData> = [];\n\n // process all DOM reads first (getBoundingClientRect)\n observedElements.forEach((data, element) => {\n const newRect = element.getBoundingClientRect();\n\n // gather all the data for elements whose rects have changed\n if (!rectEquals(data.rect, newRect)) {\n data.rect = newRect;\n changedRectsData.push(data);\n }\n });\n\n // group DOM writes here after the DOM reads (getBoundingClientRect)\n // as DOM writes will most likely happen with the callbacks\n changedRectsData.forEach((data) => {\n data.callbacks.forEach((callback) => callback(data.rect));\n });\n\n rafId = requestAnimationFrame(runLoop);\n}\n// ========================================================================\n\n/**\n * Returns whether 2 rects are equal in values\n */\nfunction rectEquals(rect1: ClientRect, rect2: ClientRect) {\n return (\n rect1.width === rect2.width &&\n rect1.height === rect2.height &&\n rect1.top === rect2.top &&\n rect1.right === rect2.right &&\n rect1.bottom === rect2.bottom &&\n rect1.left === rect2.left\n );\n}\n\nexport { observeElementRect };\nexport type { Measurable };\n"],"names":["observeElementRect","elementToObserve","callback","observedData","observedElements","get","undefined","set","rect","callbacks","size","rafId","requestAnimationFrame","runLoop","push","getBoundingClientRect","index","indexOf","splice","length","delete","cancelAnimationFrame","Map","changedRectsData","forEach","data","element","newRect","rectEquals","rect1","rect2","width","height","top","right","bottom","left"],"version":3,"file":"index.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radix-ui/rect",
3
- "version": "0.1.1-rc.3",
3
+ "version": "0.1.2-rc.2",
4
4
  "license": "MIT",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.js",
@@ -26,5 +26,5 @@
26
26
  "dependencies": {
27
27
  "@babel/runtime": "^7.13.10"
28
28
  },
29
- "stableVersion": "0.1.0"
29
+ "stableVersion": "0.1.1"
30
30
  }