@tamagui/core 1.126.18 → 1.127.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/cjs/helpers/getBoundingClientRect.cjs +16 -3
- package/dist/cjs/helpers/getBoundingClientRect.js +17 -2
- package/dist/cjs/helpers/getBoundingClientRect.js.map +1 -1
- package/dist/cjs/helpers/getBoundingClientRect.native.js +20 -2
- package/dist/cjs/helpers/getBoundingClientRect.native.js.map +2 -2
- package/dist/cjs/hooks/useElementLayout.cjs +90 -22
- package/dist/cjs/hooks/useElementLayout.js +72 -16
- package/dist/cjs/hooks/useElementLayout.js.map +1 -1
- package/dist/cjs/hooks/useElementLayout.native.js +82 -20
- package/dist/cjs/hooks/useElementLayout.native.js.map +2 -2
- package/dist/esm/helpers/getBoundingClientRect.js +17 -2
- package/dist/esm/helpers/getBoundingClientRect.js.map +1 -1
- package/dist/esm/helpers/getBoundingClientRect.mjs +15 -3
- package/dist/esm/helpers/getBoundingClientRect.mjs.map +1 -1
- package/dist/esm/helpers/getBoundingClientRect.native.js +22 -4
- package/dist/esm/helpers/getBoundingClientRect.native.js.map +1 -1
- package/dist/esm/hooks/useElementLayout.js +72 -16
- package/dist/esm/hooks/useElementLayout.js.map +1 -1
- package/dist/esm/hooks/useElementLayout.mjs +89 -23
- package/dist/esm/hooks/useElementLayout.mjs.map +1 -1
- package/dist/esm/hooks/useElementLayout.native.js +101 -36
- package/dist/esm/hooks/useElementLayout.native.js.map +1 -1
- package/dist/native.js +81 -22
- package/dist/native.js.map +2 -2
- package/dist/test.native.js +81 -22
- package/dist/test.native.js.map +2 -2
- package/package.json +7 -7
- package/src/helpers/getBoundingClientRect.tsx +26 -0
- package/src/hooks/useElementLayout.tsx +107 -8
- package/types/helpers/getBoundingClientRect.d.ts +1 -0
- package/types/helpers/getBoundingClientRect.d.ts.map +1 -1
- package/types/hooks/useElementLayout.d.ts +4 -2
- package/types/hooks/useElementLayout.d.ts.map +1 -1
|
@@ -20,9 +20,22 @@ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
|
20
20
|
}), mod);
|
|
21
21
|
var getBoundingClientRect_exports = {};
|
|
22
22
|
__export(getBoundingClientRect_exports, {
|
|
23
|
-
getBoundingClientRect: () => getBoundingClientRect
|
|
23
|
+
getBoundingClientRect: () => getBoundingClientRect,
|
|
24
|
+
getBoundingClientRectAsync: () => getBoundingClientRectAsync
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(getBoundingClientRect_exports);
|
|
26
27
|
const getBoundingClientRect = node => {
|
|
27
|
-
|
|
28
|
-
}
|
|
28
|
+
if (!(!node || node.nodeType !== 1)) return node.getBoundingClientRect?.();
|
|
29
|
+
},
|
|
30
|
+
getBoundingClientRectAsync = element => new Promise(resolve => {
|
|
31
|
+
let didFallback = !1;
|
|
32
|
+
function fallbackToSync() {
|
|
33
|
+
didFallback = !0, resolve(getBoundingClientRect(element));
|
|
34
|
+
}
|
|
35
|
+
const tm = setTimeout(fallbackToSync, 32);
|
|
36
|
+
new IntersectionObserver((entries, ob) => {
|
|
37
|
+
clearTimeout(tm), ob.disconnect(), didFallback || resolve(entries[0]?.boundingClientRect);
|
|
38
|
+
}, {
|
|
39
|
+
threshold: 1e-4
|
|
40
|
+
}).observe(element);
|
|
41
|
+
});
|
|
@@ -14,11 +14,26 @@ var __export = (target, all) => {
|
|
|
14
14
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
15
15
|
var getBoundingClientRect_exports = {};
|
|
16
16
|
__export(getBoundingClientRect_exports, {
|
|
17
|
-
getBoundingClientRect: () => getBoundingClientRect
|
|
17
|
+
getBoundingClientRect: () => getBoundingClientRect,
|
|
18
|
+
getBoundingClientRectAsync: () => getBoundingClientRectAsync
|
|
18
19
|
});
|
|
19
20
|
module.exports = __toCommonJS(getBoundingClientRect_exports);
|
|
20
21
|
const getBoundingClientRect = (node) => {
|
|
21
22
|
if (!(!node || node.nodeType !== 1))
|
|
22
23
|
return node.getBoundingClientRect?.();
|
|
23
|
-
}
|
|
24
|
+
}, getBoundingClientRectAsync = (element) => new Promise((resolve) => {
|
|
25
|
+
let didFallback = !1;
|
|
26
|
+
function fallbackToSync() {
|
|
27
|
+
didFallback = !0, resolve(getBoundingClientRect(element));
|
|
28
|
+
}
|
|
29
|
+
const tm = setTimeout(fallbackToSync, 32);
|
|
30
|
+
new IntersectionObserver(
|
|
31
|
+
(entries, ob) => {
|
|
32
|
+
clearTimeout(tm), ob.disconnect(), didFallback || resolve(entries[0]?.boundingClientRect);
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
threshold: 1e-4
|
|
36
|
+
}
|
|
37
|
+
).observe(element);
|
|
38
|
+
});
|
|
24
39
|
//# sourceMappingURL=getBoundingClientRect.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/helpers/getBoundingClientRect.tsx"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,MAAM,wBAAwB,CAAC,SAAkD;AACtF,MAAI,GAAC,QAAQ,KAAK,aAAa;AAC/B,WAAO,KAAK,wBAAwB;AACtC;",
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,MAAM,wBAAwB,CAAC,SAAkD;AACtF,MAAI,GAAC,QAAQ,KAAK,aAAa;AAC/B,WAAO,KAAK,wBAAwB;AACtC,GAEa,6BAA6B,CACxC,YAEO,IAAI,QAAQ,CAAC,YAAY;AAC9B,MAAI,cAAc;AAClB,WAAS,iBAAiB;AACxB,kBAAc,IACd,QAAQ,sBAAsB,OAAO,CAAC;AAAA,EACxC;AACA,QAAM,KAAK,WAAW,gBAAgB,EAAE;AAaxC,EAZiB,IAAI;AAAA,IACnB,CAAC,SAAS,OAAO;AACf,mBAAa,EAAE,GACf,GAAG,WAAW,GACT,eACH,QAAQ,QAAQ,CAAC,GAAG,kBAAkB;AAAA,IAE1C;AAAA,IACA;AAAA,MACE,WAAW;AAAA,IACb;AAAA,EACF,EACS,QAAQ,OAAO;AAC1B,CAAC;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -15,16 +15,34 @@ var __export = (target, all) => {
|
|
|
15
15
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
|
|
16
16
|
var getBoundingClientRect_exports = {};
|
|
17
17
|
__export(getBoundingClientRect_exports, {
|
|
18
|
-
getBoundingClientRect: () => getBoundingClientRect
|
|
18
|
+
getBoundingClientRect: () => getBoundingClientRect,
|
|
19
|
+
getBoundingClientRectAsync: () => getBoundingClientRectAsync
|
|
19
20
|
});
|
|
20
21
|
module.exports = __toCommonJS(getBoundingClientRect_exports);
|
|
21
22
|
var getBoundingClientRect = function(node) {
|
|
22
23
|
var _node_getBoundingClientRect;
|
|
23
24
|
if (!(!node || node.nodeType !== 1))
|
|
24
25
|
return (_node_getBoundingClientRect = node.getBoundingClientRect) === null || _node_getBoundingClientRect === void 0 ? void 0 : _node_getBoundingClientRect.call(node);
|
|
26
|
+
}, getBoundingClientRectAsync = function(element) {
|
|
27
|
+
return new Promise(function(resolve) {
|
|
28
|
+
var didFallback = !1;
|
|
29
|
+
function fallbackToSync() {
|
|
30
|
+
didFallback = !0, resolve(getBoundingClientRect(element));
|
|
31
|
+
}
|
|
32
|
+
var tm = setTimeout(fallbackToSync, 32), observer = new IntersectionObserver(function(entries, ob) {
|
|
33
|
+
if (clearTimeout(tm), ob.disconnect(), !didFallback) {
|
|
34
|
+
var _entries_;
|
|
35
|
+
resolve((_entries_ = entries[0]) === null || _entries_ === void 0 ? void 0 : _entries_.boundingClientRect);
|
|
36
|
+
}
|
|
37
|
+
}, {
|
|
38
|
+
threshold: 1e-4
|
|
39
|
+
});
|
|
40
|
+
observer.observe(element);
|
|
41
|
+
});
|
|
25
42
|
};
|
|
26
43
|
// Annotate the CommonJS export names for ESM import in node:
|
|
27
44
|
0 && (module.exports = {
|
|
28
|
-
getBoundingClientRect
|
|
45
|
+
getBoundingClientRect,
|
|
46
|
+
getBoundingClientRectAsync
|
|
29
47
|
});
|
|
30
48
|
//# sourceMappingURL=getBoundingClientRect.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/helpers/getBoundingClientRect.tsx"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;AAAA
|
|
5
|
-
"names": ["getBoundingClientRect", "node", "nodeType"]
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;AAAA;;;;;;AAAO,IAAMA,wBAAwB,SAACC,MAAAA;MAE7BA;AADP,MAAI,GAACA,QAAQA,KAAKC,aAAa;AAC/B,YAAOD,8BAAAA,KAAKD,2BAAqB,QAA1BC,gCAAAA,SAAAA,SAAAA,4BAAAA,KAAAA,IAAAA;AACT,GAEaE,6BAA6B,SACxCC,SAAAA;AAEA,SAAO,IAAIC,QAAQ,SAACC,SAAAA;AAClB,QAAIC,cAAc;AAClB,aAASC,iBAAAA;AACPD,oBAAc,IACdD,QAAQN,sBAAsBI,OAAAA,CAAAA;IAChC;AACA,QAAMK,KAAKC,WAAWF,gBAAgB,EAAA,GAChCG,WAAW,IAAIC,qBACnB,SAACC,SAASC,IAAAA;AAGR,UAFAC,aAAaN,EAAAA,GACbK,GAAGE,WAAU,GACT,CAACT,aAAa;YACRM;AAARP,iBAAQO,YAAAA,QAAQ,CAAA,OAAE,QAAVA,cAAAA,SAAAA,SAAAA,UAAYI,kBAAkB;MACxC;IACF,GACA;MACEC,WAAW;IACb,CAAA;AAEFP,aAASQ,QAAQf,OAAAA;EACnB,CAAA;AACF;",
|
|
5
|
+
"names": ["getBoundingClientRect", "node", "nodeType", "getBoundingClientRectAsync", "element", "Promise", "resolve", "didFallback", "fallbackToSync", "tm", "setTimeout", "observer", "IntersectionObserver", "entries", "ob", "clearTimeout", "disconnect", "boundingClientRect", "threshold", "observe"]
|
|
6
6
|
}
|
|
@@ -21,7 +21,9 @@ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
|
21
21
|
var useElementLayout_exports = {};
|
|
22
22
|
__export(useElementLayout_exports, {
|
|
23
23
|
getElementLayoutEvent: () => getElementLayoutEvent,
|
|
24
|
+
getElementLayoutEventAsync: () => getElementLayoutEventAsync,
|
|
24
25
|
measureLayout: () => measureLayout,
|
|
26
|
+
measureLayoutAsync: () => measureLayoutAsync,
|
|
25
27
|
setOnLayoutStrategy: () => setOnLayoutStrategy,
|
|
26
28
|
useElementLayout: () => useElementLayout
|
|
27
29
|
});
|
|
@@ -30,34 +32,21 @@ var import_constants = require("@tamagui/constants"),
|
|
|
30
32
|
import_web = require("@tamagui/web");
|
|
31
33
|
const LayoutHandlers = /* @__PURE__ */new WeakMap(),
|
|
32
34
|
Nodes = /* @__PURE__ */new Set();
|
|
33
|
-
let
|
|
35
|
+
let strategy = "async";
|
|
34
36
|
function setOnLayoutStrategy(state) {
|
|
35
|
-
|
|
37
|
+
strategy = state;
|
|
36
38
|
}
|
|
37
39
|
const NodeRectCache = /* @__PURE__ */new WeakMap(),
|
|
38
40
|
ParentRectCache = /* @__PURE__ */new WeakMap(),
|
|
39
|
-
|
|
41
|
+
DebounceTimers = /* @__PURE__ */new WeakMap(),
|
|
42
|
+
LastChangeTime = /* @__PURE__ */new WeakMap(),
|
|
43
|
+
rAF = typeof window < "u" ? window.requestAnimationFrame : void 0,
|
|
44
|
+
DEBOUNCE_DELAY = 32;
|
|
40
45
|
if (import_constants.isClient) if (rAF) {
|
|
41
|
-
let
|
|
42
|
-
const nodeRect = node.getBoundingClientRect(),
|
|
43
|
-
parentNode = node.parentElement,
|
|
44
|
-
parentRect = parentNode?.getBoundingClientRect(),
|
|
45
|
-
onLayout = LayoutHandlers.get(node);
|
|
46
|
-
if (typeof onLayout != "function") return;
|
|
47
|
-
const cachedRect = NodeRectCache.get(node),
|
|
48
|
-
cachedParentRect = parentNode ? NodeRectCache.get(parentNode) : null;
|
|
49
|
-
if (!cachedRect ||
|
|
50
|
-
// has changed one rect
|
|
51
|
-
!(0, import_web.isEqualShallow)(cachedRect, nodeRect) && (!cachedParentRect || !(0, import_web.isEqualShallow)(cachedParentRect, parentRect))) {
|
|
52
|
-
NodeRectCache.set(node, nodeRect), parentRect && parentNode && ParentRectCache.set(parentNode, parentRect);
|
|
53
|
-
const event = getElementLayoutEvent(node);
|
|
54
|
-
avoidUpdates ? queuedUpdates.set(node, () => onLayout(event)) : onLayout(event);
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
layoutOnAnimationFrame = function () {
|
|
46
|
+
let layoutOnAnimationFrame = function () {
|
|
58
47
|
const now = Date.now(),
|
|
59
48
|
timeSinceLastFrame = now - lastFrameAt;
|
|
60
|
-
lastFrameAt = now,
|
|
49
|
+
lastFrameAt = now, strategy !== "off" && (timeSinceLastFrame > 16.67 * numDroppedFramesUntilPause || Nodes.forEach(updateLayoutIfChanged)), rAF(layoutOnAnimationFrame);
|
|
61
50
|
},
|
|
62
51
|
avoidUpdates = !0;
|
|
63
52
|
const queuedUpdates = /* @__PURE__ */new Map();
|
|
@@ -65,7 +54,47 @@ if (import_constants.isClient) if (rAF) {
|
|
|
65
54
|
const numDroppedFramesUntilPause = 2;
|
|
66
55
|
(0, import_web.___onDidFinishClientRender)(() => {
|
|
67
56
|
avoidUpdates = !1, queuedUpdates && (queuedUpdates.forEach(cb => cb()), queuedUpdates.clear());
|
|
68
|
-
})
|
|
57
|
+
});
|
|
58
|
+
async function updateLayoutIfChanged(node) {
|
|
59
|
+
const nodeRect = node.getBoundingClientRect(),
|
|
60
|
+
parentNode = node.parentElement,
|
|
61
|
+
parentRect = parentNode?.getBoundingClientRect(),
|
|
62
|
+
onLayout = LayoutHandlers.get(node);
|
|
63
|
+
if (typeof onLayout != "function") return;
|
|
64
|
+
const cachedRect = NodeRectCache.get(node),
|
|
65
|
+
cachedParentRect = parentNode ? NodeRectCache.get(parentNode) : null;
|
|
66
|
+
if (!cachedRect ||
|
|
67
|
+
// has changed one rect
|
|
68
|
+
!(0, import_web.isEqualShallow)(cachedRect, nodeRect) && (!cachedParentRect || !(0, import_web.isEqualShallow)(cachedParentRect, parentRect))) if (NodeRectCache.set(node, nodeRect), parentRect && parentNode && ParentRectCache.set(parentNode, parentRect), avoidUpdates) {
|
|
69
|
+
const event = getElementLayoutEvent(node);
|
|
70
|
+
queuedUpdates.set(node, () => onLayout(event));
|
|
71
|
+
} else if (strategy === "async") {
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
LastChangeTime.set(node, now);
|
|
74
|
+
const existingTimer = DebounceTimers.get(node);
|
|
75
|
+
existingTimer && clearTimeout(existingTimer);
|
|
76
|
+
const timer = setTimeout(async () => {
|
|
77
|
+
const lastChange = LastChangeTime.get(node) || 0,
|
|
78
|
+
timeSinceChange = Date.now() - lastChange;
|
|
79
|
+
if (timeSinceChange >= DEBOUNCE_DELAY) {
|
|
80
|
+
const event = await getElementLayoutEventAsync(node);
|
|
81
|
+
onLayout(event), DebounceTimers.delete(node);
|
|
82
|
+
} else {
|
|
83
|
+
const remainingDelay = DEBOUNCE_DELAY - timeSinceChange,
|
|
84
|
+
newTimer = setTimeout(async () => {
|
|
85
|
+
const event = await getElementLayoutEventAsync(node);
|
|
86
|
+
onLayout(event), DebounceTimers.delete(node);
|
|
87
|
+
}, remainingDelay);
|
|
88
|
+
DebounceTimers.set(node, newTimer);
|
|
89
|
+
}
|
|
90
|
+
}, DEBOUNCE_DELAY);
|
|
91
|
+
DebounceTimers.set(node, timer);
|
|
92
|
+
} else {
|
|
93
|
+
const event = getElementLayoutEvent(node);
|
|
94
|
+
onLayout(event);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
rAF(layoutOnAnimationFrame);
|
|
69
98
|
} else process.env.NODE_ENV === "development" && console.warn("No requestAnimationFrame - please polyfill for onLayout to work correctly");
|
|
70
99
|
const getElementLayoutEvent = target => {
|
|
71
100
|
let res = null;
|
|
@@ -105,6 +134,43 @@ const getElementLayoutEvent = target => {
|
|
|
105
134
|
}
|
|
106
135
|
}
|
|
107
136
|
},
|
|
137
|
+
getElementLayoutEventAsync = async target => {
|
|
138
|
+
let res = null;
|
|
139
|
+
if (await measureLayoutAsync(target, null, (x, y, width, height, left, top) => {
|
|
140
|
+
res = {
|
|
141
|
+
nativeEvent: {
|
|
142
|
+
layout: {
|
|
143
|
+
x,
|
|
144
|
+
y,
|
|
145
|
+
width,
|
|
146
|
+
height,
|
|
147
|
+
left,
|
|
148
|
+
top
|
|
149
|
+
},
|
|
150
|
+
target
|
|
151
|
+
},
|
|
152
|
+
timeStamp: Date.now()
|
|
153
|
+
};
|
|
154
|
+
}), !res) throw new Error("\u203C\uFE0F");
|
|
155
|
+
return res;
|
|
156
|
+
},
|
|
157
|
+
measureLayoutAsync = async (node, relativeTo, callback) => {
|
|
158
|
+
const relativeNode = relativeTo || node?.parentElement;
|
|
159
|
+
if (relativeNode instanceof HTMLElement) {
|
|
160
|
+
const [nodeDim, relativeNodeDim] = await Promise.all([node.getBoundingClientRect(), relativeNode.getBoundingClientRect()]);
|
|
161
|
+
if (relativeNodeDim && nodeDim) {
|
|
162
|
+
const {
|
|
163
|
+
x,
|
|
164
|
+
y,
|
|
165
|
+
width,
|
|
166
|
+
height,
|
|
167
|
+
left,
|
|
168
|
+
top
|
|
169
|
+
} = getRelativeDimensions(nodeDim, relativeNodeDim);
|
|
170
|
+
callback(x, y, width, height, left, top);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
},
|
|
108
174
|
getRelativeDimensions = (a, b) => {
|
|
109
175
|
const {
|
|
110
176
|
height,
|
|
@@ -130,6 +196,8 @@ function useElementLayout(ref, onLayout) {
|
|
|
130
196
|
const node2 = ref.current?.host;
|
|
131
197
|
if (node2) return LayoutHandlers.set(node2, onLayout), Nodes.add(node2), onLayout(getElementLayoutEvent(node2)), () => {
|
|
132
198
|
Nodes.delete(node2), LayoutHandlers.delete(node2), NodeRectCache.delete(node2);
|
|
199
|
+
const timer = DebounceTimers.get(node2);
|
|
200
|
+
timer && (clearTimeout(timer), DebounceTimers.delete(node2)), LastChangeTime.delete(node2);
|
|
133
201
|
};
|
|
134
202
|
}, [ref, !!onLayout]);
|
|
135
203
|
}
|
|
@@ -15,40 +15,66 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0
|
|
|
15
15
|
var useElementLayout_exports = {};
|
|
16
16
|
__export(useElementLayout_exports, {
|
|
17
17
|
getElementLayoutEvent: () => getElementLayoutEvent,
|
|
18
|
+
getElementLayoutEventAsync: () => getElementLayoutEventAsync,
|
|
18
19
|
measureLayout: () => measureLayout,
|
|
20
|
+
measureLayoutAsync: () => measureLayoutAsync,
|
|
19
21
|
setOnLayoutStrategy: () => setOnLayoutStrategy,
|
|
20
22
|
useElementLayout: () => useElementLayout
|
|
21
23
|
});
|
|
22
24
|
module.exports = __toCommonJS(useElementLayout_exports);
|
|
23
25
|
var import_constants = require("@tamagui/constants"), import_web = require("@tamagui/web");
|
|
24
26
|
const LayoutHandlers = /* @__PURE__ */ new WeakMap(), Nodes = /* @__PURE__ */ new Set();
|
|
25
|
-
let
|
|
27
|
+
let strategy = "async";
|
|
26
28
|
function setOnLayoutStrategy(state) {
|
|
27
|
-
|
|
29
|
+
strategy = state;
|
|
28
30
|
}
|
|
29
|
-
const NodeRectCache = /* @__PURE__ */ new WeakMap(), ParentRectCache = /* @__PURE__ */ new WeakMap(), rAF = typeof window < "u" ? window.requestAnimationFrame : void 0;
|
|
31
|
+
const NodeRectCache = /* @__PURE__ */ new WeakMap(), ParentRectCache = /* @__PURE__ */ new WeakMap(), DebounceTimers = /* @__PURE__ */ new WeakMap(), LastChangeTime = /* @__PURE__ */ new WeakMap(), rAF = typeof window < "u" ? window.requestAnimationFrame : void 0, DEBOUNCE_DELAY = 32;
|
|
30
32
|
if (import_constants.isClient)
|
|
31
33
|
if (rAF) {
|
|
32
|
-
let
|
|
33
|
-
const nodeRect = node.getBoundingClientRect(), parentNode = node.parentElement, parentRect = parentNode?.getBoundingClientRect(), onLayout = LayoutHandlers.get(node);
|
|
34
|
-
if (typeof onLayout != "function") return;
|
|
35
|
-
const cachedRect = NodeRectCache.get(node), cachedParentRect = parentNode ? NodeRectCache.get(parentNode) : null;
|
|
36
|
-
if (!cachedRect || // has changed one rect
|
|
37
|
-
!(0, import_web.isEqualShallow)(cachedRect, nodeRect) && (!cachedParentRect || !(0, import_web.isEqualShallow)(cachedParentRect, parentRect))) {
|
|
38
|
-
NodeRectCache.set(node, nodeRect), parentRect && parentNode && ParentRectCache.set(parentNode, parentRect);
|
|
39
|
-
const event = getElementLayoutEvent(node);
|
|
40
|
-
avoidUpdates ? queuedUpdates.set(node, () => onLayout(event)) : onLayout(event);
|
|
41
|
-
}
|
|
42
|
-
}, layoutOnAnimationFrame = function() {
|
|
34
|
+
let layoutOnAnimationFrame = function() {
|
|
43
35
|
const now = Date.now(), timeSinceLastFrame = now - lastFrameAt;
|
|
44
|
-
lastFrameAt = now,
|
|
36
|
+
lastFrameAt = now, strategy !== "off" && (timeSinceLastFrame > 16.67 * numDroppedFramesUntilPause || Nodes.forEach(updateLayoutIfChanged)), rAF(layoutOnAnimationFrame);
|
|
45
37
|
}, avoidUpdates = !0;
|
|
46
38
|
const queuedUpdates = /* @__PURE__ */ new Map();
|
|
47
39
|
let lastFrameAt = Date.now();
|
|
48
40
|
const numDroppedFramesUntilPause = 2;
|
|
49
41
|
(0, import_web.___onDidFinishClientRender)(() => {
|
|
50
42
|
avoidUpdates = !1, queuedUpdates && (queuedUpdates.forEach((cb) => cb()), queuedUpdates.clear());
|
|
51
|
-
})
|
|
43
|
+
});
|
|
44
|
+
async function updateLayoutIfChanged(node) {
|
|
45
|
+
const nodeRect = node.getBoundingClientRect(), parentNode = node.parentElement, parentRect = parentNode?.getBoundingClientRect(), onLayout = LayoutHandlers.get(node);
|
|
46
|
+
if (typeof onLayout != "function") return;
|
|
47
|
+
const cachedRect = NodeRectCache.get(node), cachedParentRect = parentNode ? NodeRectCache.get(parentNode) : null;
|
|
48
|
+
if (!cachedRect || // has changed one rect
|
|
49
|
+
!(0, import_web.isEqualShallow)(cachedRect, nodeRect) && (!cachedParentRect || !(0, import_web.isEqualShallow)(cachedParentRect, parentRect)))
|
|
50
|
+
if (NodeRectCache.set(node, nodeRect), parentRect && parentNode && ParentRectCache.set(parentNode, parentRect), avoidUpdates) {
|
|
51
|
+
const event = getElementLayoutEvent(node);
|
|
52
|
+
queuedUpdates.set(node, () => onLayout(event));
|
|
53
|
+
} else if (strategy === "async") {
|
|
54
|
+
const now = Date.now();
|
|
55
|
+
LastChangeTime.set(node, now);
|
|
56
|
+
const existingTimer = DebounceTimers.get(node);
|
|
57
|
+
existingTimer && clearTimeout(existingTimer);
|
|
58
|
+
const timer = setTimeout(async () => {
|
|
59
|
+
const lastChange = LastChangeTime.get(node) || 0, timeSinceChange = Date.now() - lastChange;
|
|
60
|
+
if (timeSinceChange >= DEBOUNCE_DELAY) {
|
|
61
|
+
const event = await getElementLayoutEventAsync(node);
|
|
62
|
+
onLayout(event), DebounceTimers.delete(node);
|
|
63
|
+
} else {
|
|
64
|
+
const remainingDelay = DEBOUNCE_DELAY - timeSinceChange, newTimer = setTimeout(async () => {
|
|
65
|
+
const event = await getElementLayoutEventAsync(node);
|
|
66
|
+
onLayout(event), DebounceTimers.delete(node);
|
|
67
|
+
}, remainingDelay);
|
|
68
|
+
DebounceTimers.set(node, newTimer);
|
|
69
|
+
}
|
|
70
|
+
}, DEBOUNCE_DELAY);
|
|
71
|
+
DebounceTimers.set(node, timer);
|
|
72
|
+
} else {
|
|
73
|
+
const event = getElementLayoutEvent(node);
|
|
74
|
+
onLayout(event);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
rAF(layoutOnAnimationFrame);
|
|
52
78
|
} else
|
|
53
79
|
process.env.NODE_ENV === "development" && console.warn(
|
|
54
80
|
"No requestAnimationFrame - please polyfill for onLayout to work correctly"
|
|
@@ -78,6 +104,34 @@ const getElementLayoutEvent = (target) => {
|
|
|
78
104
|
callback(x, y, width, height, left, top);
|
|
79
105
|
}
|
|
80
106
|
}
|
|
107
|
+
}, getElementLayoutEventAsync = async (target) => {
|
|
108
|
+
let res = null;
|
|
109
|
+
if (await measureLayoutAsync(target, null, (x, y, width, height, left, top) => {
|
|
110
|
+
res = {
|
|
111
|
+
nativeEvent: {
|
|
112
|
+
layout: { x, y, width, height, left, top },
|
|
113
|
+
target
|
|
114
|
+
},
|
|
115
|
+
timeStamp: Date.now()
|
|
116
|
+
};
|
|
117
|
+
}), !res)
|
|
118
|
+
throw new Error("\u203C\uFE0F");
|
|
119
|
+
return res;
|
|
120
|
+
}, measureLayoutAsync = async (node, relativeTo, callback) => {
|
|
121
|
+
const relativeNode = relativeTo || node?.parentElement;
|
|
122
|
+
if (relativeNode instanceof HTMLElement) {
|
|
123
|
+
const [nodeDim, relativeNodeDim] = await Promise.all([
|
|
124
|
+
node.getBoundingClientRect(),
|
|
125
|
+
relativeNode.getBoundingClientRect()
|
|
126
|
+
]);
|
|
127
|
+
if (relativeNodeDim && nodeDim) {
|
|
128
|
+
const { x, y, width, height, left, top } = getRelativeDimensions(
|
|
129
|
+
nodeDim,
|
|
130
|
+
relativeNodeDim
|
|
131
|
+
);
|
|
132
|
+
callback(x, y, width, height, left, top);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
81
135
|
}, getRelativeDimensions = (a, b) => {
|
|
82
136
|
const { height, left, top, width } = a, x = left - b.left, y = top - b.top;
|
|
83
137
|
return { x, y, width, height, left, top };
|
|
@@ -90,6 +144,8 @@ function useElementLayout(ref, onLayout) {
|
|
|
90
144
|
if (node2)
|
|
91
145
|
return LayoutHandlers.set(node2, onLayout), Nodes.add(node2), onLayout(getElementLayoutEvent(node2)), () => {
|
|
92
146
|
Nodes.delete(node2), LayoutHandlers.delete(node2), NodeRectCache.delete(node2);
|
|
147
|
+
const timer = DebounceTimers.get(node2);
|
|
148
|
+
timer && (clearTimeout(timer), DebounceTimers.delete(node2)), LastChangeTime.delete(node2);
|
|
93
149
|
};
|
|
94
150
|
}, [ref, !!onLayout]);
|
|
95
151
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/useElementLayout.tsx"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAoD,+BACpD,aAIO;AAGP,MAAM,iBAAiB,oBAAI,QAA+B,GACpD,QAAQ,oBAAI,IAAiB;AAInC,IAAI,
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAoD,+BACpD,aAIO;AAGP,MAAM,iBAAiB,oBAAI,QAA+B,GACpD,QAAQ,oBAAI,IAAiB;AAInC,IAAI,WAAsC;AAEnC,SAAS,oBAAoB,OAAkC;AACpE,aAAW;AACb;AAmBA,MAAM,gBAAgB,oBAAI,QAA8B,GAClD,kBAAkB,oBAAI,QAA8B,GACpD,iBAAiB,oBAAI,QAAqC,GAC1D,iBAAiB,oBAAI,QAA6B,GAElD,MAAM,OAAO,SAAW,MAAc,OAAO,wBAAwB,QACrE,iBAAiB;AAEvB,IAAI;AACF,MAAI,KAAK;AAuFP,QAAS,yBAAT,WAAkC;AAChC,YAAM,MAAM,KAAK,IAAI,GACf,qBAAqB,MAAM;AACjC,oBAAc,KAEV,aAAa,UAIb,qBAAqB,QAAoB,8BAGzC,MAAM,QAAQ,qBAAqB,IAGvC,IAAK,sBAAsB;AAAA,IAC7B,GArGI,eAAe;AACnB,UAAM,gBAAgB,oBAAI,IAA2B;AAGrD,QAAI,cAAc,KAAK,IAAI;AAC3B,UAAM,6BAA6B;AAEnC,+CAA2B,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;AAAA,MAC5B;AAAA,EACF,GAAG,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC;AACtB;",
|
|
5
5
|
"names": ["node"]
|
|
6
6
|
}
|
|
@@ -16,45 +16,71 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0
|
|
|
16
16
|
var useElementLayout_exports = {};
|
|
17
17
|
__export(useElementLayout_exports, {
|
|
18
18
|
getElementLayoutEvent: () => getElementLayoutEvent,
|
|
19
|
+
getElementLayoutEventAsync: () => getElementLayoutEventAsync,
|
|
19
20
|
measureLayout: () => measureLayout,
|
|
21
|
+
measureLayoutAsync: () => measureLayoutAsync,
|
|
20
22
|
setOnLayoutStrategy: () => setOnLayoutStrategy,
|
|
21
23
|
useElementLayout: () => useElementLayout
|
|
22
24
|
});
|
|
23
25
|
module.exports = __toCommonJS(useElementLayout_exports);
|
|
24
|
-
var import_constants = require("@tamagui/constants"), import_web = require("@tamagui/web"), LayoutHandlers = /* @__PURE__ */ new WeakMap(), Nodes = /* @__PURE__ */ new Set(),
|
|
26
|
+
var import_constants = require("@tamagui/constants"), import_web = require("@tamagui/web"), LayoutHandlers = /* @__PURE__ */ new WeakMap(), Nodes = /* @__PURE__ */ new Set(), strategy = "async";
|
|
25
27
|
function setOnLayoutStrategy(state) {
|
|
26
|
-
|
|
28
|
+
strategy = state;
|
|
27
29
|
}
|
|
28
|
-
var NodeRectCache = /* @__PURE__ */ new WeakMap(), ParentRectCache = /* @__PURE__ */ new WeakMap(), rAF = typeof window < "u" ? window.requestAnimationFrame : void 0;
|
|
30
|
+
var NodeRectCache = /* @__PURE__ */ new WeakMap(), ParentRectCache = /* @__PURE__ */ new WeakMap(), DebounceTimers = /* @__PURE__ */ new WeakMap(), LastChangeTime = /* @__PURE__ */ new WeakMap(), rAF = typeof window < "u" ? window.requestAnimationFrame : void 0, DEBOUNCE_DELAY = 32;
|
|
29
31
|
if (import_constants.isClient)
|
|
30
32
|
if (rAF) {
|
|
31
|
-
let
|
|
32
|
-
var nodeRect = node.getBoundingClientRect(), parentNode = node.parentElement, parentRect = parentNode == null ? void 0 : parentNode.getBoundingClientRect(), onLayout = LayoutHandlers.get(node);
|
|
33
|
-
if (typeof onLayout == "function") {
|
|
34
|
-
var cachedRect = NodeRectCache.get(node), cachedParentRect = parentNode ? NodeRectCache.get(parentNode) : null;
|
|
35
|
-
if (!cachedRect || // has changed one rect
|
|
36
|
-
!(0, import_web.isEqualShallow)(cachedRect, nodeRect) && (!cachedParentRect || !(0, import_web.isEqualShallow)(cachedParentRect, parentRect))) {
|
|
37
|
-
NodeRectCache.set(node, nodeRect), parentRect && parentNode && ParentRectCache.set(parentNode, parentRect);
|
|
38
|
-
var event = getElementLayoutEvent(node);
|
|
39
|
-
avoidUpdates ? queuedUpdates.set(node, function() {
|
|
40
|
-
return onLayout(event);
|
|
41
|
-
}) : onLayout(event);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}, layoutOnAnimationFrame = function() {
|
|
33
|
+
let layoutOnAnimationFrame = function() {
|
|
45
34
|
var now = Date.now(), timeSinceLastFrame = now - lastFrameAt;
|
|
46
|
-
if (lastFrameAt = now,
|
|
35
|
+
if (lastFrameAt = now, strategy !== "off") {
|
|
47
36
|
var expectedFrameTime = 16.67, hasRecentSyncWork = timeSinceLastFrame > expectedFrameTime * numDroppedFramesUntilPause;
|
|
48
37
|
hasRecentSyncWork || Nodes.forEach(updateLayoutIfChanged);
|
|
49
38
|
}
|
|
50
39
|
rAF(layoutOnAnimationFrame);
|
|
51
40
|
};
|
|
52
|
-
var
|
|
41
|
+
var layoutOnAnimationFrame2 = layoutOnAnimationFrame, avoidUpdates = !0, queuedUpdates = /* @__PURE__ */ new Map(), lastFrameAt = Date.now(), numDroppedFramesUntilPause = 2;
|
|
53
42
|
(0, import_web.___onDidFinishClientRender)(function() {
|
|
54
43
|
avoidUpdates = !1, queuedUpdates && (queuedUpdates.forEach(function(cb) {
|
|
55
44
|
return cb();
|
|
56
45
|
}), queuedUpdates.clear());
|
|
57
|
-
})
|
|
46
|
+
});
|
|
47
|
+
async function updateLayoutIfChanged(node) {
|
|
48
|
+
var nodeRect = node.getBoundingClientRect(), parentNode = node.parentElement, parentRect = parentNode == null ? void 0 : parentNode.getBoundingClientRect(), onLayout = LayoutHandlers.get(node);
|
|
49
|
+
if (typeof onLayout == "function") {
|
|
50
|
+
var cachedRect = NodeRectCache.get(node), cachedParentRect = parentNode ? NodeRectCache.get(parentNode) : null;
|
|
51
|
+
if (!cachedRect || // has changed one rect
|
|
52
|
+
!(0, import_web.isEqualShallow)(cachedRect, nodeRect) && (!cachedParentRect || !(0, import_web.isEqualShallow)(cachedParentRect, parentRect)))
|
|
53
|
+
if (NodeRectCache.set(node, nodeRect), parentRect && parentNode && ParentRectCache.set(parentNode, parentRect), avoidUpdates) {
|
|
54
|
+
var event = getElementLayoutEvent(node);
|
|
55
|
+
queuedUpdates.set(node, function() {
|
|
56
|
+
return onLayout(event);
|
|
57
|
+
});
|
|
58
|
+
} else if (strategy === "async") {
|
|
59
|
+
var now = Date.now();
|
|
60
|
+
LastChangeTime.set(node, now);
|
|
61
|
+
var existingTimer = DebounceTimers.get(node);
|
|
62
|
+
existingTimer && clearTimeout(existingTimer);
|
|
63
|
+
var timer = setTimeout(async function() {
|
|
64
|
+
var lastChange = LastChangeTime.get(node) || 0, timeSinceChange = Date.now() - lastChange;
|
|
65
|
+
if (timeSinceChange >= DEBOUNCE_DELAY) {
|
|
66
|
+
var event2 = await getElementLayoutEventAsync(node);
|
|
67
|
+
onLayout(event2), DebounceTimers.delete(node);
|
|
68
|
+
} else {
|
|
69
|
+
var remainingDelay = DEBOUNCE_DELAY - timeSinceChange, newTimer = setTimeout(async function() {
|
|
70
|
+
var event3 = await getElementLayoutEventAsync(node);
|
|
71
|
+
onLayout(event3), DebounceTimers.delete(node);
|
|
72
|
+
}, remainingDelay);
|
|
73
|
+
DebounceTimers.set(node, newTimer);
|
|
74
|
+
}
|
|
75
|
+
}, DEBOUNCE_DELAY);
|
|
76
|
+
DebounceTimers.set(node, timer);
|
|
77
|
+
} else {
|
|
78
|
+
var event1 = getElementLayoutEvent(node);
|
|
79
|
+
onLayout(event1);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
rAF(layoutOnAnimationFrame);
|
|
58
84
|
} else
|
|
59
85
|
process.env.NODE_ENV === "development" && console.warn("No requestAnimationFrame - please polyfill for onLayout to work correctly");
|
|
60
86
|
var getElementLayoutEvent = function(target) {
|
|
@@ -86,6 +112,38 @@ var getElementLayoutEvent = function(target) {
|
|
|
86
112
|
callback(x, y, width, height, left, top);
|
|
87
113
|
}
|
|
88
114
|
}
|
|
115
|
+
}, getElementLayoutEventAsync = async function(target) {
|
|
116
|
+
var res = null;
|
|
117
|
+
if (await measureLayoutAsync(target, null, function(x, y, width, height, left, top) {
|
|
118
|
+
res = {
|
|
119
|
+
nativeEvent: {
|
|
120
|
+
layout: {
|
|
121
|
+
x,
|
|
122
|
+
y,
|
|
123
|
+
width,
|
|
124
|
+
height,
|
|
125
|
+
left,
|
|
126
|
+
top
|
|
127
|
+
},
|
|
128
|
+
target
|
|
129
|
+
},
|
|
130
|
+
timeStamp: Date.now()
|
|
131
|
+
};
|
|
132
|
+
}), !res)
|
|
133
|
+
throw new Error("\u203C\uFE0F");
|
|
134
|
+
return res;
|
|
135
|
+
}, measureLayoutAsync = async function(node, relativeTo, callback) {
|
|
136
|
+
var relativeNode = relativeTo || (node == null ? void 0 : node.parentElement);
|
|
137
|
+
if (relativeNode instanceof HTMLElement) {
|
|
138
|
+
var [nodeDim, relativeNodeDim] = await Promise.all([
|
|
139
|
+
node.getBoundingClientRect(),
|
|
140
|
+
relativeNode.getBoundingClientRect()
|
|
141
|
+
]);
|
|
142
|
+
if (relativeNodeDim && nodeDim) {
|
|
143
|
+
var { x, y, width, height, left, top } = getRelativeDimensions(nodeDim, relativeNodeDim);
|
|
144
|
+
callback(x, y, width, height, left, top);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
89
147
|
}, getRelativeDimensions = function(a, b) {
|
|
90
148
|
var { height, left, top, width } = a, x = left - b.left, y = top - b.top;
|
|
91
149
|
return {
|
|
@@ -106,6 +164,8 @@ function useElementLayout(ref, onLayout) {
|
|
|
106
164
|
if (node2)
|
|
107
165
|
return LayoutHandlers.set(node2, onLayout), Nodes.add(node2), onLayout(getElementLayoutEvent(node2)), function() {
|
|
108
166
|
Nodes.delete(node2), LayoutHandlers.delete(node2), NodeRectCache.delete(node2);
|
|
167
|
+
var timer = DebounceTimers.get(node2);
|
|
168
|
+
timer && (clearTimeout(timer), DebounceTimers.delete(node2)), LastChangeTime.delete(node2);
|
|
109
169
|
};
|
|
110
170
|
}
|
|
111
171
|
}, [
|
|
@@ -116,7 +176,9 @@ function useElementLayout(ref, onLayout) {
|
|
|
116
176
|
// Annotate the CommonJS export names for ESM import in node:
|
|
117
177
|
0 && (module.exports = {
|
|
118
178
|
getElementLayoutEvent,
|
|
179
|
+
getElementLayoutEventAsync,
|
|
119
180
|
measureLayout,
|
|
181
|
+
measureLayoutAsync,
|
|
120
182
|
setOnLayoutStrategy,
|
|
121
183
|
useElementLayout
|
|
122
184
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/hooks/useElementLayout.tsx"],
|
|
4
|
-
"mappings": ";;;;;;;;;;;;;;;AAAA
|
|
5
|
-
"names": ["LayoutHandlers", "WeakMap", "Nodes", "Set", "
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;AAAA;;;;;;;;;;uBAAoD,+BACpD,aAIO,yBAGDA,iBAAiB,oBAAIC,QAAAA,GACrBC,QAAQ,oBAAIC,IAAAA,GAIdC,WAAsC;AAEnC,SAASC,oBAAoBC,OAAgC;AAClEF,aAAWE;AACb;AAmBA,IAAMC,gBAAgB,oBAAIN,QAAAA,GACpBO,kBAAkB,oBAAIP,QAAAA,GACtBQ,iBAAiB,oBAA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AAGC,OAAOC,QAAQC,MAAMC,GAAAA;IACtC;EACF;AACF,GAEMW,wBAAwB,SAACI,GAAoBC,GAAAA;AACjD,MAAM,EAAElB,QAAQC,MAAMC,KAAKH,MAAK,IAAKkB,GAC/BpB,IAAII,OAAOiB,EAAEjB,MACbH,IAAII,MAAMgB,EAAEhB;AAClB,SAAO;IAAEL;IAAGC;IAAGC;IAAOC;IAAQC;IAAMC;EAAI;AAC1C;AAEO,SAASiB,iBACdC,KACAjD,UAA4C;MAG/BiD,cAAPvD,QAAOuD,eAAAA,IAAIC,aAAO,QAAXD,iBAAAA,SAAAA,SAAAA,aAAaE;AAC1B,EAAIzD,QAAQM,YACVvC,eAAe4C,IAAIX,MAAMM,QAAAA,OAG3BoD,4CAA0B,WAAA;QAEXH;AADb,QAAKjD,UACL;UAAMN,SAAOuD,gBAAAA,IAAIC,aAAO,QAAXD,kBAAAA,SAAAA,SAAAA,cAAaE;AAC1B,UAAKzD;AAELjC,8BAAe4C,IAAIX,OAAMM,QAAAA,GACzBrC,MAAM0F,IAAI3D,KAAAA,GACVM,SAASO,sBAAsBb,KAAAA,CAAAA,GAExB,WAAA;AACL/B,gBAAMoD,OAAOrB,KAAAA,GACbjC,eAAesD,OAAOrB,KAAAA,GACtB1B,cAAc+C,OAAOrB,KAAAA;AAGrB,cAAMgB,QAAQxC,eAAe+B,IAAIP,KAAAA;AACjC,UAAIgB,UACFD,aAAaC,KAAAA,GACbxC,eAAe6C,OAAOrB,KAAAA,IAExBvB,eAAe4C,OAAOrB,KAAAA;QACxB;;EACF,GAAG;IAACuD;IAAK,CAAC,CAACjD;GAAS;AACtB;",
|
|
5
|
+
"names": ["LayoutHandlers", "WeakMap", "Nodes", "Set", "strategy", "setOnLayoutStrategy", "state", "NodeRectCache", "ParentRectCache", "DebounceTimers", "LastChangeTime", "rAF", "window", "requestAnimationFrame", "undefined", "DEBOUNCE_DELAY", "isClient", "layoutOnAnimationFrame", "now", "Date", "timeSinceLastFrame", "lastFrameAt", "expectedFrameTime", "hasRecentSyncWork", "numDroppedFramesUntilPause", "forEach", "updateLayoutIfChanged", "avoidUpdates", "queuedUpdates", "Map", "___onDidFinishClientRender", "cb", "clear", "node", "nodeRect", "getBoundingClientRect", "parentNode", "parentElement", "parentRect", "onLayout", "get", "cachedRect", "cachedParentRect", "isEqualShallow", "set", "event", "getElementLayoutEvent", "existingTimer", "clearTimeout", "timer", "setTimeout", "lastChange", "timeSinceChange", "getElementLayoutEventAsync", "delete", "remainingDelay", "newTimer", "process", "env", "NODE_ENV", "console", "warn", "target", "res", "measureLayout", "x", "y", "width", "height", "left", "top", "nativeEvent", "layout", "timeStamp", "Error", "relativeTo", "callback", "relativeNode", "HTMLElement", "nodeDim", "relativeNodeDim", "getRelativeDimensions", "measureLayoutAsync", "Promise", "all", "a", "b", "useElementLayout", "ref", "current", "host", "useIsomorphicLayoutEffect", "add"]
|
|
6
6
|
}
|
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
const getBoundingClientRect = (node) => {
|
|
2
2
|
if (!(!node || node.nodeType !== 1))
|
|
3
3
|
return node.getBoundingClientRect?.();
|
|
4
|
-
}
|
|
4
|
+
}, getBoundingClientRectAsync = (element) => new Promise((resolve) => {
|
|
5
|
+
let didFallback = !1;
|
|
6
|
+
function fallbackToSync() {
|
|
7
|
+
didFallback = !0, resolve(getBoundingClientRect(element));
|
|
8
|
+
}
|
|
9
|
+
const tm = setTimeout(fallbackToSync, 32);
|
|
10
|
+
new IntersectionObserver(
|
|
11
|
+
(entries, ob) => {
|
|
12
|
+
clearTimeout(tm), ob.disconnect(), didFallback || resolve(entries[0]?.boundingClientRect);
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
threshold: 1e-4
|
|
16
|
+
}
|
|
17
|
+
).observe(element);
|
|
18
|
+
});
|
|
5
19
|
export {
|
|
6
|
-
getBoundingClientRect
|
|
20
|
+
getBoundingClientRect,
|
|
21
|
+
getBoundingClientRectAsync
|
|
7
22
|
};
|
|
8
23
|
//# sourceMappingURL=getBoundingClientRect.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/helpers/getBoundingClientRect.tsx"],
|
|
4
|
-
"mappings": "AAAO,MAAM,wBAAwB,CAAC,SAAkD;AACtF,MAAI,GAAC,QAAQ,KAAK,aAAa;AAC/B,WAAO,KAAK,wBAAwB;AACtC;",
|
|
4
|
+
"mappings": "AAAO,MAAM,wBAAwB,CAAC,SAAkD;AACtF,MAAI,GAAC,QAAQ,KAAK,aAAa;AAC/B,WAAO,KAAK,wBAAwB;AACtC,GAEa,6BAA6B,CACxC,YAEO,IAAI,QAAQ,CAAC,YAAY;AAC9B,MAAI,cAAc;AAClB,WAAS,iBAAiB;AACxB,kBAAc,IACd,QAAQ,sBAAsB,OAAO,CAAC;AAAA,EACxC;AACA,QAAM,KAAK,WAAW,gBAAgB,EAAE;AAaxC,EAZiB,IAAI;AAAA,IACnB,CAAC,SAAS,OAAO;AACf,mBAAa,EAAE,GACf,GAAG,WAAW,GACT,eACH,QAAQ,QAAQ,CAAC,GAAG,kBAAkB;AAAA,IAE1C;AAAA,IACA;AAAA,MACE,WAAW;AAAA,IACb;AAAA,EACF,EACS,QAAQ,OAAO;AAC1B,CAAC;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|