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