@hanzogui/use-element-layout 3.0.6 → 4.4.0

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