@radix-ui/rect 1.0.1 → 1.1.0-rc.1

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.
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- export type Measurable = {
1
+ declare type Measurable = {
2
2
  getBoundingClientRect(): ClientRect;
3
3
  };
4
4
  /**
@@ -6,11 +6,11 @@ export type Measurable = {
6
6
  * This is useful to track elements on the screen and attach other elements
7
7
  * that might be in different layers, etc.
8
8
  */
9
- export function observeElementRect(
9
+ declare function observeElementRect(
10
10
  /** The element whose rect to observe */
11
11
  elementToObserve: Measurable,
12
12
  /** The callback which will be called when the rect changes */
13
13
  callback: CallbackFn): () => void;
14
- type CallbackFn = (rect: ClientRect) => void;
14
+ declare type CallbackFn = (rect: ClientRect) => void;
15
15
 
16
- //# sourceMappingURL=index.d.ts.map
16
+ export { type Measurable, observeElementRect };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type Measurable = {
1
+ declare type Measurable = {
2
2
  getBoundingClientRect(): ClientRect;
3
3
  };
4
4
  /**
@@ -6,11 +6,11 @@ export type Measurable = {
6
6
  * This is useful to track elements on the screen and attach other elements
7
7
  * that might be in different layers, etc.
8
8
  */
9
- export function observeElementRect(
9
+ declare function observeElementRect(
10
10
  /** The element whose rect to observe */
11
11
  elementToObserve: Measurable,
12
12
  /** The callback which will be called when the rect changes */
13
13
  callback: CallbackFn): () => void;
14
- type CallbackFn = (rect: ClientRect) => void;
14
+ declare type CallbackFn = (rect: ClientRect) => void;
15
15
 
16
- //# sourceMappingURL=index.d.ts.map
16
+ export { type Measurable, observeElementRect };
package/dist/index.js CHANGED
@@ -1,70 +1,50 @@
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);
1
+ "use strict";
2
+ (() => {
3
+ // packages/core/rect/src/observeElementRect.ts
4
+ function observeElementRect(elementToObserve, callback) {
5
+ const observedData = observedElements.get(elementToObserve);
6
+ if (observedData === void 0) {
7
+ observedElements.set(elementToObserve, { rect: {}, callbacks: [callback] });
8
+ if (observedElements.size === 1) {
9
+ rafId = requestAnimationFrame(runLoop);
10
+ }
23
11
  } else {
24
- // only add a callback for this element as it's already observed
25
- observedData1.callbacks.push(callback);
26
- callback(elementToObserve.getBoundingClientRect());
12
+ observedData.callbacks.push(callback);
13
+ callback(elementToObserve.getBoundingClientRect());
27
14
  }
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);
15
+ return () => {
16
+ const observedData2 = observedElements.get(elementToObserve);
17
+ if (observedData2 === void 0) return;
18
+ const index = observedData2.callbacks.indexOf(callback);
19
+ if (index > -1) {
20
+ observedData2.callbacks.splice(index, 1);
21
+ }
22
+ if (observedData2.callbacks.length === 0) {
23
+ observedElements.delete(elementToObserve);
24
+ if (observedElements.size === 0) {
25
+ cancelAnimationFrame(rafId);
39
26
  }
27
+ }
40
28
  };
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
- );
29
+ }
30
+ var rafId;
31
+ var observedElements = /* @__PURE__ */ new Map();
32
+ function runLoop() {
33
+ const changedRectsData = [];
34
+ observedElements.forEach((data, element) => {
35
+ const newRect = element.getBoundingClientRect();
36
+ if (!rectEquals(data.rect, newRect)) {
37
+ data.rect = newRect;
38
+ changedRectsData.push(data);
39
+ }
40
+ });
41
+ changedRectsData.forEach((data) => {
42
+ data.callbacks.forEach((callback) => callback(data.rect));
58
43
  });
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) {
44
+ rafId = requestAnimationFrame(runLoop);
45
+ }
46
+ function rectEquals(rect1, rect2) {
64
47
  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
-
48
+ }
49
+ })();
70
50
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1,7 @@
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,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BF,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.js.map"}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/observeElementRect.ts"],
4
+ "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"],
5
+ "mappings": ";;;AAOA,WAAS,mBAEP,kBA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WAAW,MAAM,UACvB,MAAM,QAAQ,MAAM,OACpB,MAAM,UAAU,MAAM,SACtB,MAAM,WAAW,MAAM,UACvB,MAAM,SAAS,MAAM;AAAA,EAEzB;",
6
+ "names": ["observedData"]
7
+ }
package/dist/index.mjs CHANGED
@@ -1,66 +1,50 @@
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());
1
+ // packages/core/rect/src/observeElementRect.ts
2
+ function observeElementRect(elementToObserve, callback) {
3
+ const observedData = observedElements.get(elementToObserve);
4
+ if (observedData === void 0) {
5
+ observedElements.set(elementToObserve, { rect: {}, callbacks: [callback] });
6
+ if (observedElements.size === 1) {
7
+ rafId = requestAnimationFrame(runLoop);
22
8
  }
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;
9
+ } else {
10
+ observedData.callbacks.push(callback);
11
+ callback(elementToObserve.getBoundingClientRect());
12
+ }
13
+ return () => {
14
+ const observedData2 = observedElements.get(elementToObserve);
15
+ if (observedData2 === void 0) return;
16
+ const index = observedData2.callbacks.indexOf(callback);
17
+ if (index > -1) {
18
+ observedData2.callbacks.splice(index, 1);
19
+ }
20
+ if (observedData2.callbacks.length === 0) {
21
+ observedElements.delete(elementToObserve);
22
+ if (observedElements.size === 0) {
23
+ cancelAnimationFrame(rafId);
24
+ }
25
+ }
26
+ };
27
+ }
28
+ var rafId;
29
+ var observedElements = /* @__PURE__ */ new Map();
30
+ function runLoop() {
31
+ const changedRectsData = [];
32
+ observedElements.forEach((data, element) => {
33
+ const newRect = element.getBoundingClientRect();
34
+ if (!rectEquals(data.rect, newRect)) {
35
+ data.rect = newRect;
36
+ changedRectsData.push(data);
37
+ }
38
+ });
39
+ changedRectsData.forEach((data) => {
40
+ data.callbacks.forEach((callback) => callback(data.rect));
41
+ });
42
+ rafId = requestAnimationFrame(runLoop);
43
+ }
44
+ function rectEquals(rect1, rect2) {
45
+ 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
46
  }
61
-
62
-
63
-
64
-
65
- export {$0f3bbd680c63c15c$export$5a50ff2cde8c3802 as observeElementRect};
47
+ export {
48
+ observeElementRect
49
+ };
66
50
  //# sourceMappingURL=index.mjs.map
@@ -1 +1,7 @@
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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A,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.mjs.map"}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/observeElementRect.ts"],
4
+ "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"],
5
+ "mappings": ";AAOA,SAAS,mBAEP,kBAEA,UACA;AACA,QAAM,eAAe,iBAAiB,IAAI,gBAAgB;AAE1D,MAAI,iBAAiB,QAAW;AAG9B,qBAAiB,IAAI,kBAAkB,EAAE,MAAM,CAAC,GAAiB,WAAW,CAAC,QAAQ,EAAE,CAAC;AAExF,QAAI,iBAAiB,SAAS,GAAG;AAE/B,cAAQ,sBAAsB,OAAO;AAAA,IACvC;AAAA,EACF,OAAO;AAEL,iBAAa,UAAU,KAAK,QAAQ;AACpC,aAAS,iBAAiB,sBAAsB,CAAC;AAAA,EACnD;AAEA,SAAO,MAAM;AACX,UAAMA,gBAAe,iBAAiB,IAAI,gBAAgB;AAC1D,QAAIA,kBAAiB,OAAW;AAGhC,UAAM,QAAQA,cAAa,UAAU,QAAQ,QAAQ;AACrD,QAAI,QAAQ,IAAI;AACd,MAAAA,cAAa,UAAU,OAAO,OAAO,CAAC;AAAA,IACxC;AAEA,QAAIA,cAAa,UAAU,WAAW,GAAG;AAGvC,uBAAiB,OAAO,gBAAgB;AAExC,UAAI,iBAAiB,SAAS,GAAG;AAE/B,6BAAqB,KAAK;AAAA,MAC5B;AAAA,IACF;AAAA,EACF;AACF;AAYA,IAAI;AACJ,IAAM,mBAAkD,oBAAI,IAAI;AAEhE,SAAS,UAAU;AACjB,QAAM,mBAAwC,CAAC;AAG/C,mBAAiB,QAAQ,CAAC,MAAM,YAAY;AAC1C,UAAM,UAAU,QAAQ,sBAAsB;AAG9C,QAAI,CAAC,WAAW,KAAK,MAAM,OAAO,GAAG;AACnC,WAAK,OAAO;AACZ,uBAAiB,KAAK,IAAI;AAAA,IAC5B;AAAA,EACF,CAAC;AAID,mBAAiB,QAAQ,CAAC,SAAS;AACjC,SAAK,UAAU,QAAQ,CAAC,aAAa,SAAS,KAAK,IAAI,CAAC;AAAA,EAC1D,CAAC;AAED,UAAQ,sBAAsB,OAAO;AACvC;AAMA,SAAS,WAAW,OAAmB,OAAmB;AACxD,SACE,MAAM,UAAU,MAAM,SACtB,MAAM,WAAW,MAAM,UACvB,MAAM,QAAQ,MAAM,OACpB,MAAM,UAAU,MAAM,SACtB,MAAM,WAAW,MAAM,UACvB,MAAM,SAAS,MAAM;AAEzB;",
6
+ "names": ["observedData"]
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radix-ui/rect",
3
- "version": "1.0.1",
3
+ "version": "1.1.0-rc.1",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": {
@@ -35,7 +35,5 @@
35
35
  "bugs": {
36
36
  "url": "https://github.com/radix-ui/primitives/issues"
37
37
  },
38
- "dependencies": {
39
- "@babel/runtime": "^7.13.10"
40
- }
38
+ "stableVersion": "1.0.1"
41
39
  }
@@ -1 +0,0 @@
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"}