@radix-ui/rect 0.0.0-20250116175529

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/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # `rect`
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ yarn add @radix-ui/rect
7
+ # or
8
+ $ npm install @radix-ui/rect
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ This is an internal utility, not intended for public usage.
@@ -0,0 +1,16 @@
1
+ type Measurable = {
2
+ getBoundingClientRect(): DOMRect;
3
+ };
4
+ /**
5
+ * Observes an element's rectangle on screen (getBoundingClientRect)
6
+ * This is useful to track elements on the screen and attach other elements
7
+ * that might be in different layers, etc.
8
+ */
9
+ declare function observeElementRect(
10
+ /** The element whose rect to observe */
11
+ elementToObserve: Measurable,
12
+ /** The callback which will be called when the rect changes */
13
+ callback: CallbackFn): () => void;
14
+ type CallbackFn = (rect: DOMRect) => void;
15
+
16
+ export { type Measurable, observeElementRect };
@@ -0,0 +1,16 @@
1
+ type Measurable = {
2
+ getBoundingClientRect(): DOMRect;
3
+ };
4
+ /**
5
+ * Observes an element's rectangle on screen (getBoundingClientRect)
6
+ * This is useful to track elements on the screen and attach other elements
7
+ * that might be in different layers, etc.
8
+ */
9
+ declare function observeElementRect(
10
+ /** The element whose rect to observe */
11
+ elementToObserve: Measurable,
12
+ /** The callback which will be called when the rect changes */
13
+ callback: CallbackFn): () => void;
14
+ type CallbackFn = (rect: DOMRect) => void;
15
+
16
+ export { type Measurable, observeElementRect };
package/dist/index.js ADDED
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // packages/core/rect/src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ observeElementRect: () => observeElementRect
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // packages/core/rect/src/observeElementRect.ts
28
+ function observeElementRect(elementToObserve, callback) {
29
+ const observedData = observedElements.get(elementToObserve);
30
+ if (observedData === void 0) {
31
+ observedElements.set(elementToObserve, { rect: {}, callbacks: [callback] });
32
+ if (observedElements.size === 1) {
33
+ rafId = requestAnimationFrame(runLoop);
34
+ }
35
+ } else {
36
+ observedData.callbacks.push(callback);
37
+ callback(elementToObserve.getBoundingClientRect());
38
+ }
39
+ return () => {
40
+ const observedData2 = observedElements.get(elementToObserve);
41
+ if (observedData2 === void 0) return;
42
+ const index = observedData2.callbacks.indexOf(callback);
43
+ if (index > -1) {
44
+ observedData2.callbacks.splice(index, 1);
45
+ }
46
+ if (observedData2.callbacks.length === 0) {
47
+ observedElements.delete(elementToObserve);
48
+ if (observedElements.size === 0) {
49
+ cancelAnimationFrame(rafId);
50
+ }
51
+ }
52
+ };
53
+ }
54
+ var rafId;
55
+ var observedElements = /* @__PURE__ */ new Map();
56
+ function runLoop() {
57
+ const changedRectsData = [];
58
+ observedElements.forEach((data, element) => {
59
+ const newRect = element.getBoundingClientRect();
60
+ if (!rectEquals(data.rect, newRect)) {
61
+ data.rect = newRect;
62
+ changedRectsData.push(data);
63
+ }
64
+ });
65
+ changedRectsData.forEach((data) => {
66
+ data.callbacks.forEach((callback) => callback(data.rect));
67
+ });
68
+ rafId = requestAnimationFrame(runLoop);
69
+ }
70
+ function rectEquals(rect1, rect2) {
71
+ 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;
72
+ }
73
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts", "../src/observeElementRect.ts"],
4
+ "sourcesContent": ["export { observeElementRect } from './observeElementRect';\nexport type { Measurable } from './observeElementRect';\n", "type Measurable = { getBoundingClientRect(): DOMRect };\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 DOMRect, 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: DOMRect) => void;\n\ntype ObservedData = {\n rect: DOMRect;\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: DOMRect, rect2: DOMRect) {\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": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACOA,SAAS,mBAEP,kBAEA,UACA;AACA,QAAM,eAAe,iBAAiB,IAAI,gBAAgB;AAE1D,MAAI,iBAAiB,QAAW;AAG9B,qBAAiB,IAAI,kBAAkB,EAAE,MAAM,CAAC,GAAc,WAAW,CAAC,QAAQ,EAAE,CAAC;AAErF,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,OAAgB,OAAgB;AAClD,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/dist/index.mjs ADDED
@@ -0,0 +1,50 @@
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);
8
+ }
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;
46
+ }
47
+ export {
48
+ observeElementRect
49
+ };
50
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/observeElementRect.ts"],
4
+ "sourcesContent": ["type Measurable = { getBoundingClientRect(): DOMRect };\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 DOMRect, 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: DOMRect) => void;\n\ntype ObservedData = {\n rect: DOMRect;\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: DOMRect, rect2: DOMRect) {\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,GAAc,WAAW,CAAC,QAAQ,EAA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1,37 @@
1
+ {
2
+ "name": "@radix-ui/rect",
3
+ "version": "0.0.0-20250116175529",
4
+ "license": "MIT",
5
+ "exports": {
6
+ ".": {
7
+ "import": {
8
+ "types": "./dist/index.d.mts",
9
+ "default": "./dist/index.mjs"
10
+ },
11
+ "require": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ }
15
+ }
16
+ },
17
+ "source": "./src/index.ts",
18
+ "main": "./dist/index.js",
19
+ "module": "./dist/index.mjs",
20
+ "types": "./dist/index.d.ts",
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
25
+ "sideEffects": false,
26
+ "scripts": {
27
+ "clean": "rm -rf dist"
28
+ },
29
+ "homepage": "https://radix-ui.com/primitives",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/radix-ui/primitives.git"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/radix-ui/primitives/issues"
36
+ }
37
+ }