@tamagui/use-element-layout 2.7.7 → 3.0.0-beta.1091.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.
@@ -1,392 +1,350 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
2
  import { useIsomorphicLayoutEffect } from "@tamagui/constants";
3
3
  import { createContext, useContext, useId } from "react";
4
- function _instanceof(left, right) {
5
- if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) return !!right[Symbol.hasInstance](left);else return left instanceof right;
6
- }
7
- var LayoutHandlers = /* @__PURE__ */new WeakMap();
8
- var LayoutDisableKey = /* @__PURE__ */new WeakMap();
9
- var Nodes = /* @__PURE__ */new Set();
10
- var usePretransformDimensions = function () {
11
- return globalThis.__TAMAGUI_ONLAYOUT_PRETRANSFORM === true || process.env.TAMAGUI_ONLAYOUT_PRETRANSFORM === "1";
4
+
5
+ var LayoutHandlers = /* @__PURE__ */ new WeakMap();
6
+ var LayoutDisableKey = /* @__PURE__ */ new WeakMap();
7
+ var Nodes = /* @__PURE__ */ new Set();
8
+ var NodeRectCache = /* @__PURE__ */ new WeakMap();
9
+ var PrevHostNode = /* @__PURE__ */ new WeakMap();
10
+ var usePretransformDimensions = function() {
11
+ return globalThis.__TAMAGUI_ONLAYOUT_PRETRANSFORM === true || process.env.TAMAGUI_ONLAYOUT_PRETRANSFORM === "1";
12
+ };
13
+ var isDebugLayout = function() {
14
+ var _window_location_search, _window_location;
15
+ return typeof window !== "undefined" && ((_window_location = window.location) === null || _window_location === void 0 ? void 0 : (_window_location_search = _window_location.search) === null || _window_location_search === void 0 ? void 0 : _window_location_search.includes("__tamaDebugLayout"));
12
16
  };
13
- var _debugLayout;
14
- function isDebugLayout() {
15
- if (_debugLayout === void 0) _debugLayout = typeof window !== "undefined" && new URLSearchParams(window.location.search).has("__tamaDebugLayout");
16
- return _debugLayout;
17
- }
18
17
  var DisableLayoutContextValues = {};
19
- var DisableLayoutContextKey = /* @__PURE__ */createContext("");
18
+ var DisableLayoutContextKey = /* @__PURE__ */ createContext("");
20
19
  var ENABLE = false;
21
- var LayoutMeasurementController = function (param) {
22
- var {
23
- disable,
24
- children
25
- } = param;
26
- var id = useId();
27
- useIsomorphicLayoutEffect(function () {
28
- var wasDisabled = DisableLayoutContextValues[id] === true;
29
- DisableLayoutContextValues[id] = disable;
30
- if (wasDisabled && !disable) measureOnNextFrame === null || measureOnNextFrame === void 0 || measureOnNextFrame();
31
- }, [disable, id]);
32
- return /* @__PURE__ */jsx(DisableLayoutContextKey.Provider, {
33
- value: id,
34
- children
35
- });
20
+ var LayoutMeasurementController = function(param) {
21
+ var { disable, children } = param;
22
+ var id = useId();
23
+ useIsomorphicLayoutEffect(function() {
24
+ var wasDisabled = DisableLayoutContextValues[id] === true;
25
+ DisableLayoutContextValues[id] = disable;
26
+ if (wasDisabled && !disable) measureOnNextFrame === null || measureOnNextFrame === void 0 || measureOnNextFrame();
27
+ }, [disable, id]);
28
+ return /* @__PURE__ */ jsx(DisableLayoutContextKey.Provider, {
29
+ value: id,
30
+ children
31
+ });
36
32
  };
37
33
  var strategy = "async";
38
34
  var resumeLayoutLoop;
39
35
  var measureOnNextFrame;
40
- function setOnLayoutStrategy(state) {
41
- strategy = state;
42
- resumeLayoutLoop === null || resumeLayoutLoop === void 0 || resumeLayoutLoop();
43
- }
44
- var NodeRectCache = /* @__PURE__ */new WeakMap();
45
36
  var avoidUpdates = true;
46
- var queuedUpdates = /* @__PURE__ */new Map();
37
+ var queuedUpdates = /* @__PURE__ */ new Map();
47
38
  function enable() {
48
- if (avoidUpdates) {
49
- avoidUpdates = false;
50
- if (queuedUpdates) {
51
- queuedUpdates.forEach(function (cb) {
52
- return cb();
53
- });
54
- queuedUpdates.clear();
55
- }
56
- }
39
+ if (avoidUpdates) {
40
+ avoidUpdates = false;
41
+ if (queuedUpdates) {
42
+ queuedUpdates.forEach(function(cb) {
43
+ return cb();
44
+ });
45
+ queuedUpdates.clear();
46
+ }
47
+ }
57
48
  }
58
49
  function rectsEqual(a, b) {
59
- return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
50
+ return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
60
51
  }
61
52
  if (ENABLE) {
62
- let ensureRectFetchObserver2 = function () {
63
- if (rectFetchObserver) return rectFetchObserver;
64
- rectFetchObserver = new IntersectionObserver(function (entries) {
65
- lastCallbackDelay = Math.round(performance.now() - rectFetchStartTime);
66
- for (var i = 0; i < entries.length; i++) BoundingRects.set(entries[i].target, entries[i].boundingClientRect);
67
- if (process.env.NODE_ENV === "development" && isDebugLayout() && lastCallbackDelay > 50) console.warn("[onLayout-io-delay]", lastCallbackDelay + "ms", entries.length, "entries");
68
- if (rectFetchResolve) {
69
- rectFetchResolve(true);
70
- rectFetchResolve = null;
71
- }
72
- }, {
73
- threshold: 0
74
- });
75
- return rectFetchObserver;
76
- },
77
- scheduleLayoutFrame2 = function () {
78
- if (frameScheduled || strategy === "off" || Nodes.size === 0 || document.hidden) return;
79
- frameScheduled = true;
80
- rAF ? rAF(layoutOnAnimationFrame) : setTimeout(layoutOnAnimationFrame, 16);
81
- };
82
- var BoundingRects = /* @__PURE__ */new WeakMap();
83
- var rectFetchObserver = null;
84
- var rectFetchResolve = null;
85
- var rectFetchStartTime = 0;
86
- var lastCallbackDelay = 0;
87
- async function updateLayoutIfChanged(node) {
88
- if (typeof LayoutHandlers.get(node) !== "function") return;
89
- var parentNode = node.parentElement;
90
- if (!parentNode) return;
91
- var nodeRect;
92
- var parentRect;
93
- if (strategy === "async") {
94
- nodeRect = BoundingRects.get(node);
95
- parentRect = BoundingRects.get(parentNode);
96
- if (!nodeRect || !parentRect) return;
97
- } else {
98
- nodeRect = node.getBoundingClientRect();
99
- parentRect = parentNode.getBoundingClientRect();
100
- }
101
- emitLayoutIfChanged(node, parentNode, nodeRect, parentRect);
102
- }
103
- var rAF = typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame : void 0;
104
- var userSkipVal = process.env.TAMAGUI_LAYOUT_FRAME_SKIP;
105
- var BASE_SKIP_FRAMES = userSkipVal ? +userSkipVal : 10;
106
- var MAX_SKIP_FRAMES = 20;
107
- var skipFrames = BASE_SKIP_FRAMES;
108
- var frameCount = 0;
109
- var frameScheduled = false;
110
- async function layoutOnAnimationFrame() {
111
- if (frameCount++ % skipFrames !== 0) {
112
- frameScheduled = false;
113
- scheduleLayoutFrame2();
114
- return;
115
- }
116
- if (frameCount >= Number.MAX_SAFE_INTEGER) frameCount = 0;
117
- if (strategy !== "off") {
118
- var activeNodes = [];
119
- var parentsToObserve = /* @__PURE__ */new Set();
120
- var _iteratorNormalCompletion = true,
121
- _didIteratorError = false,
122
- _iteratorError = void 0;
123
- try {
124
- for (var _iterator = Nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
125
- var node = _step.value;
126
- var parentElement = node.parentElement;
127
- if (!_instanceof(parentElement, HTMLElement)) {
128
- cleanupNode(node);
129
- continue;
130
- }
131
- var disableKey = LayoutDisableKey.get(node);
132
- if (disableKey && DisableLayoutContextValues[disableKey] === true) continue;
133
- activeNodes.push(node);
134
- parentsToObserve.add(parentElement);
135
- }
136
- } catch (err) {
137
- _didIteratorError = true;
138
- _iteratorError = err;
139
- } finally {
140
- try {
141
- if (!_iteratorNormalCompletion && _iterator.return != null) _iterator.return();
142
- } finally {
143
- if (_didIteratorError) throw _iteratorError;
144
- }
145
- }
146
- if (activeNodes.length > 0) {
147
- var io = ensureRectFetchObserver2();
148
- rectFetchStartTime = performance.now();
149
- for (var i = 0; i < activeNodes.length; i++) io.observe(activeNodes[i]);
150
- var _iteratorNormalCompletion1 = true,
151
- _didIteratorError1 = false,
152
- _iteratorError1 = void 0;
153
- try {
154
- for (var _iterator1 = parentsToObserve[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
155
- var parent = _step1.value;
156
- io.observe(parent);
157
- }
158
- } catch (err) {
159
- _didIteratorError1 = true;
160
- _iteratorError1 = err;
161
- } finally {
162
- try {
163
- if (!_iteratorNormalCompletion1 && _iterator1.return != null) _iterator1.return();
164
- } finally {
165
- if (_didIteratorError1) throw _iteratorError1;
166
- }
167
- }
168
- await new Promise(function (res) {
169
- rectFetchResolve = res;
170
- });
171
- for (var i1 = 0; i1 < activeNodes.length; i1++) io.unobserve(activeNodes[i1]);
172
- var _iteratorNormalCompletion2 = true,
173
- _didIteratorError2 = false,
174
- _iteratorError2 = void 0;
175
- try {
176
- for (var _iterator2 = parentsToObserve[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
177
- var parent1 = _step2.value;
178
- io.unobserve(parent1);
179
- }
180
- } catch (err) {
181
- _didIteratorError2 = true;
182
- _iteratorError2 = err;
183
- } finally {
184
- try {
185
- if (!_iteratorNormalCompletion2 && _iterator2.return != null) _iterator2.return();
186
- } finally {
187
- if (_didIteratorError2) throw _iteratorError2;
188
- }
189
- }
190
- if (lastCallbackDelay > 50) skipFrames = Math.min(skipFrames + 2, MAX_SKIP_FRAMES);else if (lastCallbackDelay < 20) skipFrames = Math.max(skipFrames - 1, BASE_SKIP_FRAMES);
191
- for (var i2 = 0; i2 < activeNodes.length; i2++) updateLayoutIfChanged(activeNodes[i2]);
192
- }
193
- }
194
- frameScheduled = false;
195
- scheduleLayoutFrame2();
196
- }
197
- resumeLayoutLoop = scheduleLayoutFrame2;
198
- measureOnNextFrame = function () {
199
- frameCount = 0;
200
- scheduleLayoutFrame2();
201
- };
202
- document.addEventListener("visibilitychange", scheduleLayoutFrame2);
203
- scheduleLayoutFrame2();
53
+ let ensureRectFetchObserver2 = function() {
54
+ if (rectFetchObserver) return rectFetchObserver;
55
+ rectFetchObserver = new IntersectionObserver(function(entries) {
56
+ lastCallbackDelay = Math.round(performance.now() - rectFetchStartTime);
57
+ for (var i = 0; i < entries.length; i++) BoundingRects.set(entries[i].target, entries[i].boundingClientRect);
58
+ if (process.env.NODE_ENV === "development" && isDebugLayout() && lastCallbackDelay > 50) console.warn("[onLayout-io-delay]", lastCallbackDelay + "ms", entries.length, "entries");
59
+ if (rectFetchResolve) {
60
+ rectFetchResolve(true);
61
+ rectFetchResolve = null;
62
+ }
63
+ }, { threshold: 0 });
64
+ return rectFetchObserver;
65
+ }, updateLayoutIfChanged2 = function(node) {
66
+ if (typeof LayoutHandlers.get(node) !== "function") return;
67
+ var parentNode = node.parentElement;
68
+ if (!parentNode) return;
69
+ var nodeRect;
70
+ var parentRect;
71
+ if (strategy === "async") {
72
+ nodeRect = BoundingRects.get(node);
73
+ parentRect = BoundingRects.get(parentNode);
74
+ if (!nodeRect || !parentRect) return;
75
+ } else {
76
+ nodeRect = node.getBoundingClientRect();
77
+ parentRect = parentNode.getBoundingClientRect();
78
+ }
79
+ emitLayoutIfChanged(node, parentNode, nodeRect, parentRect);
80
+ }, scheduleLayoutFrame2 = function() {
81
+ if (frameScheduled || strategy === "off" || Nodes.size === 0 || document.hidden) return;
82
+ frameScheduled = true;
83
+ rAF ? rAF(layoutOnAnimationFrame) : setTimeout(layoutOnAnimationFrame, 16);
84
+ };
85
+ var BoundingRects = /* @__PURE__ */ new WeakMap();
86
+ var rectFetchObserver = null;
87
+ var rectFetchResolve = null;
88
+ var rectFetchStartTime = 0;
89
+ var lastCallbackDelay = 0;
90
+ var rAF = typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame : void 0;
91
+ var userSkipVal = process.env.TAMAGUI_LAYOUT_FRAME_SKIP;
92
+ var BASE_SKIP_FRAMES = userSkipVal ? +userSkipVal : 10;
93
+ var MAX_SKIP_FRAMES = 20;
94
+ var skipFrames = BASE_SKIP_FRAMES;
95
+ var frameCount = 0;
96
+ var frameScheduled = false;
97
+ async function layoutOnAnimationFrame() {
98
+ if (frameCount++ % skipFrames !== 0) {
99
+ frameScheduled = false;
100
+ scheduleLayoutFrame2();
101
+ return;
102
+ }
103
+ if (frameCount >= Number.MAX_SAFE_INTEGER) frameCount = 0;
104
+ if (strategy !== "off") {
105
+ var activeNodes = [];
106
+ var parentsToObserve = /* @__PURE__ */ new Set();
107
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
108
+ try {
109
+ for (var _iterator = Nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
110
+ var node = _step.value;
111
+ var parentElement = node.parentElement;
112
+ if (!(parentElement instanceof HTMLElement)) {
113
+ cleanupNode(node);
114
+ continue;
115
+ }
116
+ var disableKey = LayoutDisableKey.get(node);
117
+ if (disableKey && DisableLayoutContextValues[disableKey] === true) continue;
118
+ activeNodes.push(node);
119
+ parentsToObserve.add(parentElement);
120
+ }
121
+ } catch (err) {
122
+ _didIteratorError = true;
123
+ _iteratorError = err;
124
+ } finally {
125
+ try {
126
+ if (!_iteratorNormalCompletion && _iterator.return != null) _iterator.return();
127
+ } finally {
128
+ if (_didIteratorError) throw _iteratorError;
129
+ }
130
+ }
131
+ if (activeNodes.length > 0) {
132
+ var io = ensureRectFetchObserver2();
133
+ rectFetchStartTime = performance.now();
134
+ for (var i = 0; i < activeNodes.length; i++) io.observe(activeNodes[i]);
135
+ var _iteratorNormalCompletion1 = true, _didIteratorError1 = false, _iteratorError1 = void 0;
136
+ try {
137
+ for (var _iterator1 = parentsToObserve[Symbol.iterator](), _step1; !(_iteratorNormalCompletion1 = (_step1 = _iterator1.next()).done); _iteratorNormalCompletion1 = true) {
138
+ var parent = _step1.value;
139
+ io.observe(parent);
140
+ }
141
+ } catch (err) {
142
+ _didIteratorError1 = true;
143
+ _iteratorError1 = err;
144
+ } finally {
145
+ try {
146
+ if (!_iteratorNormalCompletion1 && _iterator1.return != null) _iterator1.return();
147
+ } finally {
148
+ if (_didIteratorError1) throw _iteratorError1;
149
+ }
150
+ }
151
+ await new Promise(function(res) {
152
+ rectFetchResolve = res;
153
+ });
154
+ for (var i1 = 0; i1 < activeNodes.length; i1++) io.unobserve(activeNodes[i1]);
155
+ var _iteratorNormalCompletion2 = true, _didIteratorError2 = false, _iteratorError2 = void 0;
156
+ try {
157
+ for (var _iterator2 = parentsToObserve[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
158
+ var parent1 = _step2.value;
159
+ io.unobserve(parent1);
160
+ }
161
+ } catch (err) {
162
+ _didIteratorError2 = true;
163
+ _iteratorError2 = err;
164
+ } finally {
165
+ try {
166
+ if (!_iteratorNormalCompletion2 && _iterator2.return != null) _iterator2.return();
167
+ } finally {
168
+ if (_didIteratorError2) throw _iteratorError2;
169
+ }
170
+ }
171
+ if (lastCallbackDelay > 50) skipFrames = Math.min(skipFrames + 2, MAX_SKIP_FRAMES);
172
+ else if (lastCallbackDelay < 20) skipFrames = Math.max(skipFrames - 1, BASE_SKIP_FRAMES);
173
+ for (var i2 = 0; i2 < activeNodes.length; i2++) updateLayoutIfChanged2(activeNodes[i2]);
174
+ }
175
+ }
176
+ frameScheduled = false;
177
+ scheduleLayoutFrame2();
178
+ }
179
+ resumeLayoutLoop = scheduleLayoutFrame2;
180
+ measureOnNextFrame = function() {
181
+ frameCount = 0;
182
+ scheduleLayoutFrame2();
183
+ };
184
+ document.addEventListener("visibilitychange", scheduleLayoutFrame2);
185
+ scheduleLayoutFrame2();
204
186
  }
205
- var getElementLayoutEvent = function (nodeRect, parentRect, node) {
206
- return {
207
- nativeEvent: {
208
- layout: getRelativeDimensions(nodeRect, parentRect, node),
209
- target: nodeRect
210
- },
211
- timeStamp: Date.now()
212
- };
187
+ var getElementLayoutEvent = function(nodeRect, parentRect, node) {
188
+ return {
189
+ nativeEvent: {
190
+ layout: getRelativeDimensions(nodeRect, parentRect, node),
191
+ target: nodeRect
192
+ },
193
+ timeStamp: Date.now()
194
+ };
213
195
  };
214
- var getPreTransformDimensions = function (node) {
215
- return {
216
- width: node.offsetWidth,
217
- height: node.offsetHeight
218
- };
196
+ var getPreTransformDimensions = function(node) {
197
+ return {
198
+ width: node.offsetWidth,
199
+ height: node.offsetHeight
200
+ };
219
201
  };
220
- var getRelativeDimensions = function (a, b, aNode) {
221
- var {
222
- left,
223
- top
224
- } = a;
225
- var x = left - b.left;
226
- var y = top - b.top;
227
- var {
228
- width,
229
- height
230
- } = usePretransformDimensions() && aNode ? getPreTransformDimensions(aNode) : {
231
- width: a.width,
232
- height: a.height
233
- };
234
- return {
235
- x,
236
- y,
237
- width,
238
- height,
239
- pageX: a.left,
240
- pageY: a.top
241
- };
202
+ var getRelativeDimensions = function(a, b, aNode) {
203
+ var left = a.left - b.left;
204
+ var top = a.top - b.top;
205
+ var { width, height } = usePretransformDimensions() && aNode ? getPreTransformDimensions(aNode) : {
206
+ width: a.width,
207
+ height: a.height
208
+ };
209
+ return {
210
+ x: left,
211
+ y: top,
212
+ width,
213
+ height,
214
+ pageX: a.left,
215
+ pageY: a.top
216
+ };
242
217
  };
243
218
  function emitLayoutIfChanged(node, parentNode, nodeRect, parentRect) {
244
- var onLayout = LayoutHandlers.get(node);
245
- if (typeof onLayout !== "function") return;
246
- var cachedRect = NodeRectCache.get(node);
247
- var cachedParentRect = NodeRectCache.get(parentNode);
248
- var nodeChanged = !cachedRect || !rectsEqual(cachedRect, nodeRect);
249
- var parentChanged = !cachedParentRect || !rectsEqual(cachedParentRect, parentRect);
250
- if (!nodeChanged && !parentChanged) return;
251
- NodeRectCache.set(node, nodeRect);
252
- NodeRectCache.set(parentNode, parentRect);
253
- var event = getElementLayoutEvent(nodeRect, parentRect, node);
254
- if (process.env.NODE_ENV === "development" && isDebugLayout()) console.log("[useElementLayout] change", {
255
- tag: node.tagName,
256
- id: node.id || void 0,
257
- className: (node.className || "").slice(0, 60) || void 0,
258
- layout: event.nativeEvent.layout,
259
- first: !cachedRect
260
- });
261
- if (avoidUpdates) queuedUpdates.set(node, function () {
262
- return onLayout(event);
263
- });else onLayout(event);
219
+ var onLayout = LayoutHandlers.get(node);
220
+ if (typeof onLayout !== "function") return;
221
+ var cachedRect = NodeRectCache.get(node);
222
+ var cachedParentRect = NodeRectCache.get(parentNode);
223
+ var nodeChanged = !cachedRect || !rectsEqual(cachedRect, nodeRect);
224
+ var parentChanged = !cachedParentRect || !rectsEqual(cachedParentRect, parentRect);
225
+ if (!nodeChanged && !parentChanged) return;
226
+ NodeRectCache.set(node, nodeRect);
227
+ NodeRectCache.set(parentNode, parentRect);
228
+ var event = getElementLayoutEvent(nodeRect, parentRect, node);
229
+ if (avoidUpdates) queuedUpdates.set(node, function() {
230
+ return onLayout(event);
231
+ });
232
+ else onLayout(event);
264
233
  }
265
234
  function observeLayoutNode(node, disableKey) {
266
- Nodes.add(node);
267
- if (disableKey) LayoutDisableKey.set(node, disableKey);else LayoutDisableKey.delete(node);
268
- resumeLayoutLoop === null || resumeLayoutLoop === void 0 || resumeLayoutLoop();
235
+ Nodes.add(node);
236
+ if (disableKey) LayoutDisableKey.set(node, disableKey);
237
+ else LayoutDisableKey.delete(node);
238
+ resumeLayoutLoop === null || resumeLayoutLoop === void 0 || resumeLayoutLoop();
269
239
  }
270
240
  function registerLayoutNode(node, onChange, disableKey) {
271
- LayoutHandlers.set(node, onChange);
272
- observeLayoutNode(node, disableKey);
273
- return function () {
274
- return cleanupNode(node);
275
- };
241
+ LayoutHandlers.set(node, onChange);
242
+ observeLayoutNode(node, disableKey);
243
+ return function() {
244
+ return cleanupNode(node);
245
+ };
276
246
  }
277
247
  function cleanupNode(node) {
278
- Nodes.delete(node);
279
- LayoutHandlers.delete(node);
280
- LayoutDisableKey.delete(node);
281
- NodeRectCache.delete(node);
248
+ Nodes.delete(node);
249
+ LayoutHandlers.delete(node);
250
+ LayoutDisableKey.delete(node);
251
+ NodeRectCache.delete(node);
282
252
  }
283
- var PrevHostNode = /* @__PURE__ */new WeakMap();
284
253
  function emitLayoutSync(node) {
285
- var onLayout = LayoutHandlers.get(node);
286
- if (typeof onLayout !== "function") return;
287
- var parentNode = node.parentElement;
288
- if (!parentNode) return;
289
- var nodeRect = node.getBoundingClientRect();
290
- var parentRect = parentNode.getBoundingClientRect();
291
- NodeRectCache.set(node, nodeRect);
292
- NodeRectCache.set(parentNode, parentRect);
293
- onLayout(getElementLayoutEvent(nodeRect, parentRect, node));
254
+ var onLayout = LayoutHandlers.get(node);
255
+ if (typeof onLayout !== "function") return;
256
+ var parentNode = node.parentElement;
257
+ if (!parentNode) return;
258
+ var nodeRect = node.getBoundingClientRect();
259
+ var parentRect = parentNode.getBoundingClientRect();
260
+ NodeRectCache.set(node, nodeRect);
261
+ NodeRectCache.set(parentNode, parentRect);
262
+ onLayout(getElementLayoutEvent(nodeRect, parentRect, node));
294
263
  }
295
264
  function useElementLayout(ref, onLayout) {
296
- var _ref_current;
297
- var disableKey = useContext(DisableLayoutContextKey);
298
- var node = ensureWebElement((_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.host);
299
- if (node && onLayout) {
300
- LayoutHandlers.set(node, onLayout);
301
- LayoutDisableKey.set(node, disableKey);
302
- }
303
- useIsomorphicLayoutEffect(function () {
304
- var _ref_current2;
305
- if (!onLayout) return;
306
- var nextNode = ensureWebElement((_ref_current2 = ref.current) === null || _ref_current2 === void 0 ? void 0 : _ref_current2.host);
307
- var prevNode = PrevHostNode.get(ref);
308
- if (nextNode === prevNode) return;
309
- if (prevNode) cleanupNode(prevNode);
310
- PrevHostNode.set(ref, nextNode);
311
- if (!nextNode) return;
312
- LayoutHandlers.set(nextNode, onLayout);
313
- observeLayoutNode(nextNode, disableKey);
314
- emitLayoutSync(nextNode);
315
- });
316
- useIsomorphicLayoutEffect(function () {
317
- var _ref_current2;
318
- if (!onLayout) return;
319
- var node2 = (_ref_current2 = ref.current) === null || _ref_current2 === void 0 ? void 0 : _ref_current2.host;
320
- if (!node2) return;
321
- LayoutHandlers.set(node2, onLayout);
322
- observeLayoutNode(node2, disableKey);
323
- if (process.env.NODE_ENV === "development" && isDebugLayout()) console.log("[useElementLayout] register", {
324
- tag: node2.tagName,
325
- id: node2.id || void 0,
326
- className: (node2.className || "").slice(0, 60) || void 0,
327
- totalNodes: Nodes.size
328
- });
329
- return function () {
330
- cleanupNode(node2);
331
- var swappedNode = PrevHostNode.get(ref);
332
- if (swappedNode && swappedNode !== node2) cleanupNode(swappedNode);
333
- PrevHostNode.delete(ref);
334
- };
335
- }, [ref, !!onLayout]);
265
+ var _ref_current;
266
+ var disableKey = useContext(DisableLayoutContextKey);
267
+ var node = ensureWebElement((_ref_current = ref.current) === null || _ref_current === void 0 ? void 0 : _ref_current.host);
268
+ if (node && onLayout) {
269
+ LayoutHandlers.set(node, onLayout);
270
+ LayoutDisableKey.set(node, disableKey);
271
+ }
272
+ useIsomorphicLayoutEffect(function() {
273
+ var _ref_current2;
274
+ if (!onLayout) return;
275
+ var nextNode = ensureWebElement((_ref_current2 = ref.current) === null || _ref_current2 === void 0 ? void 0 : _ref_current2.host);
276
+ var prevNode = PrevHostNode.get(ref);
277
+ if (nextNode !== prevNode) {
278
+ if (prevNode) cleanupNode(prevNode);
279
+ PrevHostNode.set(ref, nextNode);
280
+ if (nextNode) {
281
+ LayoutHandlers.set(nextNode, onLayout);
282
+ observeLayoutNode(nextNode, disableKey);
283
+ emitLayoutSync(nextNode);
284
+ }
285
+ }
286
+ return function() {
287
+ var _ref_current3;
288
+ var activeNode = ensureWebElement((_ref_current3 = ref.current) === null || _ref_current3 === void 0 ? void 0 : _ref_current3.host);
289
+ if (activeNode) cleanupNode(activeNode);
290
+ var swapped = PrevHostNode.get(ref);
291
+ if (swapped && swapped !== activeNode) cleanupNode(swapped);
292
+ PrevHostNode.delete(ref);
293
+ };
294
+ }, [ref, !!onLayout]);
336
295
  }
337
296
  function ensureWebElement(x) {
338
- if (typeof HTMLElement === "undefined") return;
339
- return _instanceof(x, HTMLElement) ? x : void 0;
297
+ if (typeof HTMLElement === "undefined") return void 0;
298
+ return x instanceof HTMLElement ? x : void 0;
340
299
  }
341
- var getBoundingClientRectAsync = function (node) {
342
- return new Promise(function (res) {
343
- if (!node || node.nodeType !== 1) return res(false);
344
- var io = new IntersectionObserver(function (entries) {
345
- io.disconnect();
346
- return res(entries[0].boundingClientRect);
347
- }, {
348
- threshold: 0
349
- });
350
- io.observe(node);
351
- });
300
+ var getBoundingClientRectAsync = function(node) {
301
+ return new Promise(function(res) {
302
+ if (!node || node.nodeType !== 1) return res(false);
303
+ var io = new IntersectionObserver(function(entries) {
304
+ io.disconnect();
305
+ res(entries[0].boundingClientRect);
306
+ });
307
+ io.observe(node);
308
+ });
352
309
  };
353
- var measureNode = async function (node, relativeTo) {
354
- var relativeNode = relativeTo || (node === null || node === void 0 ? void 0 : node.parentElement);
355
- if (_instanceof(relativeNode, HTMLElement)) {
356
- var [nodeDim, relativeNodeDim] = await Promise.all([getBoundingClientRectAsync(node), getBoundingClientRectAsync(relativeNode)]);
357
- if (relativeNodeDim && nodeDim) return getRelativeDimensions(nodeDim, relativeNodeDim, node);
358
- }
359
- return null;
310
+ var measureNode = async function(node, relativeTo) {
311
+ var relativeNode = relativeTo || (node === null || node === void 0 ? void 0 : node.parentElement);
312
+ if (relativeNode instanceof HTMLElement) {
313
+ var [nodeDim, relativeNodeDim] = await Promise.all([getBoundingClientRectAsync(node), getBoundingClientRectAsync(relativeNode)]);
314
+ if (relativeNodeDim && nodeDim) return getRelativeDimensions(nodeDim, relativeNodeDim, node);
315
+ }
316
+ return null;
360
317
  };
361
- var measure = async function (node, callback) {
362
- var out = await measureNode(node, _instanceof(node.parentNode, HTMLElement) ? node.parentNode : null);
363
- if (out) callback === null || callback === void 0 || callback(out.x, out.y, out.width, out.height, out.pageX, out.pageY);
364
- return out;
318
+ var measure = async function(node, callback) {
319
+ var out = await measureNode(node, node.parentElement);
320
+ if (out) callback === null || callback === void 0 || callback(out.x, out.y, out.width, out.height, out.pageX, out.pageY);
321
+ return out;
365
322
  };
366
- function createMeasure(node) {
367
- return function (callback) {
368
- return measure(node, callback);
369
- };
370
- }
371
- var measureInWindow = async function (node, callback) {
372
- var out = await measureNode(node, null);
373
- if (out) callback === null || callback === void 0 || callback(out.pageX, out.pageY, out.width, out.height);
374
- return out;
323
+ var createMeasure = function(node) {
324
+ return function(callback) {
325
+ return measure(node, callback);
326
+ };
375
327
  };
376
- var createMeasureInWindow = function (node) {
377
- return function (callback) {
378
- return measureInWindow(node, callback);
379
- };
328
+ var measureInWindow = async function(node, callback) {
329
+ var out = await measureNode(node, null);
330
+ if (out) callback === null || callback === void 0 || callback(out.pageX, out.pageY, out.width, out.height);
331
+ return out;
380
332
  };
381
- var measureLayout = async function (node, relativeNode, callback) {
382
- var out = await measureNode(node, relativeNode);
383
- if (out) callback === null || callback === void 0 || callback(out.x, out.y, out.width, out.height, out.pageX, out.pageY);
384
- return out;
333
+ var createMeasureInWindow = function(node) {
334
+ return function(callback) {
335
+ return measureInWindow(node, callback);
336
+ };
385
337
  };
386
- function createMeasureLayout(node) {
387
- return function (relativeTo, callback) {
388
- return measureLayout(node, relativeTo, callback);
389
- };
390
- }
391
- export { LayoutMeasurementController, createMeasure, createMeasureInWindow, createMeasureLayout, enable, getBoundingClientRectAsync, getElementLayoutEvent, measure, measureInWindow, measureLayout, measureNode, registerLayoutNode, setOnLayoutStrategy, useElementLayout };
338
+ var measureLayout = async function(node, relativeNode, callback) {
339
+ var out = await measureNode(node, relativeNode);
340
+ if (out) callback === null || callback === void 0 || callback(out.x, out.y, out.width, out.height, out.pageX, out.pageY);
341
+ return out;
342
+ };
343
+ var createMeasureLayout = function(node) {
344
+ return function(relativeTo, callback) {
345
+ return measureLayout(node, relativeTo, callback);
346
+ };
347
+ };
348
+
349
+ export { LayoutMeasurementController, createMeasure, createMeasureInWindow, createMeasureLayout, enable, getBoundingClientRectAsync, getElementLayoutEvent, measure, measureInWindow, measureLayout, measureNode, registerLayoutNode, useElementLayout };
392
350
  //# sourceMappingURL=index.native.js.map