@hanzogui/use-element-layout 2.0.0-rc.41-hanzoai.5

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Nate Wienert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,363 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all) __defProp(target, name, {
7
+ get: all[name],
8
+ enumerable: true
9
+ });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
14
+ get: () => from[key],
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ return to;
19
+ };
20
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
21
+ value: true
22
+ }), mod);
23
+ var index_exports = {};
24
+ __export(index_exports, {
25
+ LayoutMeasurementController: () => LayoutMeasurementController,
26
+ createMeasure: () => createMeasure,
27
+ createMeasureInWindow: () => createMeasureInWindow,
28
+ createMeasureLayout: () => createMeasureLayout,
29
+ enable: () => enable,
30
+ getBoundingClientRectAsync: () => getBoundingClientRectAsync,
31
+ getElementLayoutEvent: () => getElementLayoutEvent,
32
+ measure: () => measure,
33
+ measureInWindow: () => measureInWindow,
34
+ measureLayout: () => measureLayout,
35
+ measureNode: () => measureNode,
36
+ registerLayoutNode: () => registerLayoutNode,
37
+ setOnLayoutStrategy: () => setOnLayoutStrategy,
38
+ useElementLayout: () => useElementLayout
39
+ });
40
+ module.exports = __toCommonJS(index_exports);
41
+ var import_constants = require("@hanzogui/constants");
42
+ var import_react = require("react");
43
+ var import_jsx_runtime = require("react/jsx-runtime");
44
+ const LayoutHandlers = /* @__PURE__ */new WeakMap();
45
+ const LayoutDisableKey = /* @__PURE__ */new WeakMap();
46
+ const Nodes = /* @__PURE__ */new Set();
47
+ const IntersectionState = /* @__PURE__ */new WeakMap();
48
+ const usePretransformDimensions = () => globalThis.__TAMAGUI_ONLAYOUT_PRETRANSFORM === true || process.env.TAMAGUI_ONLAYOUT_PRETRANSFORM === "1";
49
+ let _debugLayout;
50
+ function isDebugLayout() {
51
+ if (_debugLayout === void 0) _debugLayout = typeof window !== "undefined" && new URLSearchParams(window.location.search).has("__tamaDebugLayout");
52
+ return _debugLayout;
53
+ }
54
+ const DisableLayoutContextValues = {};
55
+ const DisableLayoutContextKey = (0, import_react.createContext)("");
56
+ const ENABLE = typeof IntersectionObserver !== "undefined";
57
+ const LayoutMeasurementController = ({
58
+ disable,
59
+ children
60
+ }) => {
61
+ const id = (0, import_react.useId)();
62
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
63
+ DisableLayoutContextValues[id] = disable;
64
+ }, [disable, id]);
65
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(DisableLayoutContextKey.Provider, {
66
+ value: id,
67
+ children
68
+ });
69
+ };
70
+ let globalIntersectionObserver = null;
71
+ let strategy = "async";
72
+ function setOnLayoutStrategy(state) {
73
+ strategy = state;
74
+ }
75
+ const NodeRectCache = /* @__PURE__ */new WeakMap();
76
+ let avoidUpdates = true;
77
+ const queuedUpdates = /* @__PURE__ */new Map();
78
+ function enable() {
79
+ if (avoidUpdates) {
80
+ avoidUpdates = false;
81
+ if (queuedUpdates) {
82
+ queuedUpdates.forEach(cb => cb());
83
+ queuedUpdates.clear();
84
+ }
85
+ }
86
+ }
87
+ function startGlobalObservers() {
88
+ if (!ENABLE || globalIntersectionObserver) return;
89
+ globalIntersectionObserver = new IntersectionObserver(entries => {
90
+ for (let i = 0; i < entries.length; i++) {
91
+ const entry = entries[i];
92
+ const node = entry.target;
93
+ if (IntersectionState.get(node) !== entry.isIntersecting) IntersectionState.set(node, entry.isIntersecting);
94
+ }
95
+ }, {
96
+ threshold: 0
97
+ });
98
+ }
99
+ function rectsEqual(a, b) {
100
+ return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
101
+ }
102
+ if (ENABLE) {
103
+ let ensureRectFetchObserver = function () {
104
+ if (rectFetchObserver) return rectFetchObserver;
105
+ rectFetchObserver = new IntersectionObserver(entries => {
106
+ lastCallbackDelay = Math.round(performance.now() - rectFetchStartTime);
107
+ for (let i = 0; i < entries.length; i++) BoundingRects.set(entries[i].target, entries[i].boundingClientRect);
108
+ if (process.env.NODE_ENV === "development" && isDebugLayout() && lastCallbackDelay > 50) console.warn("[onLayout-io-delay]", lastCallbackDelay + "ms", entries.length, "entries");
109
+ if (rectFetchResolve) {
110
+ rectFetchResolve(true);
111
+ rectFetchResolve = null;
112
+ }
113
+ }, {
114
+ threshold: 0
115
+ });
116
+ return rectFetchObserver;
117
+ };
118
+ const BoundingRects = /* @__PURE__ */new WeakMap();
119
+ let rectFetchObserver = null;
120
+ let rectFetchResolve = null;
121
+ let rectFetchStartTime = 0;
122
+ let lastCallbackDelay = 0;
123
+ async function updateLayoutIfChanged(node) {
124
+ const onLayout = LayoutHandlers.get(node);
125
+ if (typeof onLayout !== "function") return;
126
+ const parentNode = node.parentElement;
127
+ if (!parentNode) return;
128
+ let nodeRect;
129
+ let parentRect;
130
+ if (strategy === "async") {
131
+ nodeRect = BoundingRects.get(node);
132
+ parentRect = BoundingRects.get(parentNode);
133
+ if (!nodeRect || !parentRect) return;
134
+ } else {
135
+ nodeRect = node.getBoundingClientRect();
136
+ parentRect = parentNode.getBoundingClientRect();
137
+ }
138
+ const cachedRect = NodeRectCache.get(node);
139
+ const cachedParentRect = NodeRectCache.get(parentNode);
140
+ const nodeChanged = !cachedRect || !rectsEqual(cachedRect, nodeRect);
141
+ const parentChanged = !cachedParentRect || !rectsEqual(cachedParentRect, parentRect);
142
+ if (nodeChanged || parentChanged) {
143
+ NodeRectCache.set(node, nodeRect);
144
+ NodeRectCache.set(parentNode, parentRect);
145
+ const event = getElementLayoutEvent(nodeRect, parentRect, node);
146
+ if (process.env.NODE_ENV === "development" && isDebugLayout()) console.log("[useElementLayout] change", {
147
+ tag: node.tagName,
148
+ id: node.id || void 0,
149
+ className: (node.className || "").slice(0, 60) || void 0,
150
+ layout: event.nativeEvent.layout,
151
+ first: !cachedRect
152
+ });
153
+ if (avoidUpdates) queuedUpdates.set(node, () => onLayout(event));else onLayout(event);
154
+ }
155
+ }
156
+ const rAF = typeof requestAnimationFrame !== "undefined" ? requestAnimationFrame : void 0;
157
+ const userSkipVal = process.env.TAMAGUI_LAYOUT_FRAME_SKIP;
158
+ const BASE_SKIP_FRAMES = userSkipVal ? +userSkipVal : 10;
159
+ const MAX_SKIP_FRAMES = 20;
160
+ let skipFrames = BASE_SKIP_FRAMES;
161
+ let frameCount = 0;
162
+ async function layoutOnAnimationFrame() {
163
+ if (frameCount++ % skipFrames !== 0) {
164
+ rAF ? rAF(layoutOnAnimationFrame) : setTimeout(layoutOnAnimationFrame, 16);
165
+ return;
166
+ }
167
+ if (frameCount >= Number.MAX_SAFE_INTEGER) frameCount = 0;
168
+ if (strategy !== "off") {
169
+ const visibleNodes = [];
170
+ const parentsToObserve = /* @__PURE__ */new Set();
171
+ for (const node of Nodes) {
172
+ const parentElement = node.parentElement;
173
+ if (!(parentElement instanceof HTMLElement)) {
174
+ cleanupNode(node);
175
+ continue;
176
+ }
177
+ const disableKey = LayoutDisableKey.get(node);
178
+ if (disableKey && DisableLayoutContextValues[disableKey] === true) continue;
179
+ if (IntersectionState.get(node) === false) continue;
180
+ visibleNodes.push(node);
181
+ parentsToObserve.add(parentElement);
182
+ }
183
+ if (visibleNodes.length > 0) {
184
+ const io = ensureRectFetchObserver();
185
+ rectFetchStartTime = performance.now();
186
+ for (let i = 0; i < visibleNodes.length; i++) io.observe(visibleNodes[i]);
187
+ for (const parent of parentsToObserve) io.observe(parent);
188
+ await new Promise(res => {
189
+ rectFetchResolve = res;
190
+ });
191
+ for (let i = 0; i < visibleNodes.length; i++) io.unobserve(visibleNodes[i]);
192
+ for (const parent of parentsToObserve) io.unobserve(parent);
193
+ if (lastCallbackDelay > 50) skipFrames = Math.min(skipFrames + 2, MAX_SKIP_FRAMES);else if (lastCallbackDelay < 20) skipFrames = Math.max(skipFrames - 1, BASE_SKIP_FRAMES);
194
+ for (let i = 0; i < visibleNodes.length; i++) updateLayoutIfChanged(visibleNodes[i]);
195
+ }
196
+ }
197
+ rAF ? rAF(layoutOnAnimationFrame) : setTimeout(layoutOnAnimationFrame, 16);
198
+ }
199
+ layoutOnAnimationFrame();
200
+ }
201
+ const getElementLayoutEvent = (nodeRect, parentRect, node) => {
202
+ return {
203
+ nativeEvent: {
204
+ layout: getRelativeDimensions(nodeRect, parentRect, node),
205
+ target: nodeRect
206
+ },
207
+ timeStamp: Date.now()
208
+ };
209
+ };
210
+ const getPreTransformDimensions = node => {
211
+ return {
212
+ width: node.offsetWidth,
213
+ height: node.offsetHeight
214
+ };
215
+ };
216
+ const getRelativeDimensions = (a, b, aNode) => {
217
+ const {
218
+ left,
219
+ top
220
+ } = a;
221
+ const x = left - b.left;
222
+ const y = top - b.top;
223
+ const {
224
+ width,
225
+ height
226
+ } = usePretransformDimensions() && aNode ? getPreTransformDimensions(aNode) : {
227
+ width: a.width,
228
+ height: a.height
229
+ };
230
+ return {
231
+ x,
232
+ y,
233
+ width,
234
+ height,
235
+ pageX: a.left,
236
+ pageY: a.top
237
+ };
238
+ };
239
+ function registerLayoutNode(node, onChange, disableKey) {
240
+ Nodes.add(node);
241
+ LayoutHandlers.set(node, onChange);
242
+ if (disableKey) LayoutDisableKey.set(node, disableKey);
243
+ startGlobalObservers();
244
+ if (globalIntersectionObserver) {
245
+ globalIntersectionObserver.observe(node);
246
+ IntersectionState.set(node, true);
247
+ }
248
+ return () => cleanupNode(node);
249
+ }
250
+ function cleanupNode(node) {
251
+ Nodes.delete(node);
252
+ LayoutHandlers.delete(node);
253
+ LayoutDisableKey.delete(node);
254
+ NodeRectCache.delete(node);
255
+ IntersectionState.delete(node);
256
+ if (globalIntersectionObserver) globalIntersectionObserver.unobserve(node);
257
+ }
258
+ const PrevHostNode = /* @__PURE__ */new WeakMap();
259
+ function useElementLayout(ref, onLayout) {
260
+ const disableKey = (0, import_react.useContext)(DisableLayoutContextKey);
261
+ const node = ensureWebElement(ref.current?.host);
262
+ if (node && onLayout) {
263
+ LayoutHandlers.set(node, onLayout);
264
+ LayoutDisableKey.set(node, disableKey);
265
+ }
266
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
267
+ if (!onLayout) return;
268
+ const nextNode = ensureWebElement(ref.current?.host);
269
+ const prevNode = PrevHostNode.get(ref);
270
+ if (nextNode === prevNode) return;
271
+ if (prevNode) cleanupNode(prevNode);
272
+ PrevHostNode.set(ref, nextNode);
273
+ if (!nextNode) return;
274
+ Nodes.add(nextNode);
275
+ startGlobalObservers();
276
+ if (globalIntersectionObserver) {
277
+ globalIntersectionObserver.observe(nextNode);
278
+ IntersectionState.set(nextNode, true);
279
+ }
280
+ const handler = LayoutHandlers.get(nextNode);
281
+ if (typeof handler !== "function") return;
282
+ const parentNode = nextNode.parentElement;
283
+ if (!parentNode) return;
284
+ const nodeRect = nextNode.getBoundingClientRect();
285
+ const parentRect = parentNode.getBoundingClientRect();
286
+ NodeRectCache.set(nextNode, nodeRect);
287
+ NodeRectCache.set(parentNode, parentRect);
288
+ handler(getElementLayoutEvent(nodeRect, parentRect, nextNode));
289
+ });
290
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
291
+ if (!onLayout) return;
292
+ const node2 = ref.current?.host;
293
+ if (!node2) return;
294
+ Nodes.add(node2);
295
+ startGlobalObservers();
296
+ if (globalIntersectionObserver) {
297
+ globalIntersectionObserver.observe(node2);
298
+ IntersectionState.set(node2, true);
299
+ }
300
+ if (process.env.NODE_ENV === "development" && isDebugLayout()) console.log("[useElementLayout] register", {
301
+ tag: node2.tagName,
302
+ id: node2.id || void 0,
303
+ className: (node2.className || "").slice(0, 60) || void 0,
304
+ totalNodes: Nodes.size
305
+ });
306
+ const parentNode = node2.parentNode;
307
+ if (parentNode) onLayout(getElementLayoutEvent(node2.getBoundingClientRect(), parentNode.getBoundingClientRect(), node2));
308
+ return () => {
309
+ cleanupNode(node2);
310
+ const swappedNode = PrevHostNode.get(ref);
311
+ if (swappedNode && swappedNode !== node2) cleanupNode(swappedNode);
312
+ PrevHostNode.delete(ref);
313
+ };
314
+ }, [ref, !!onLayout]);
315
+ }
316
+ function ensureWebElement(x) {
317
+ if (typeof HTMLElement === "undefined") return;
318
+ return x instanceof HTMLElement ? x : void 0;
319
+ }
320
+ const getBoundingClientRectAsync = node => {
321
+ return new Promise(res => {
322
+ if (!node || node.nodeType !== 1) return res(false);
323
+ const io = new IntersectionObserver(entries => {
324
+ io.disconnect();
325
+ return res(entries[0].boundingClientRect);
326
+ }, {
327
+ threshold: 0
328
+ });
329
+ io.observe(node);
330
+ });
331
+ };
332
+ const measureNode = async (node, relativeTo) => {
333
+ const relativeNode = relativeTo || node?.parentElement;
334
+ if (relativeNode instanceof HTMLElement) {
335
+ const [nodeDim, relativeNodeDim] = await Promise.all([getBoundingClientRectAsync(node), getBoundingClientRectAsync(relativeNode)]);
336
+ if (relativeNodeDim && nodeDim) return getRelativeDimensions(nodeDim, relativeNodeDim, node);
337
+ }
338
+ return null;
339
+ };
340
+ const measure = async (node, callback) => {
341
+ const out = await measureNode(node, node.parentNode instanceof HTMLElement ? node.parentNode : null);
342
+ if (out) callback?.(out.x, out.y, out.width, out.height, out.pageX, out.pageY);
343
+ return out;
344
+ };
345
+ function createMeasure(node) {
346
+ return callback => measure(node, callback);
347
+ }
348
+ const measureInWindow = async (node, callback) => {
349
+ const out = await measureNode(node, null);
350
+ if (out) callback?.(out.pageX, out.pageY, out.width, out.height);
351
+ return out;
352
+ };
353
+ const createMeasureInWindow = node => {
354
+ return callback => measureInWindow(node, callback);
355
+ };
356
+ const measureLayout = async (node, relativeNode, callback) => {
357
+ const out = await measureNode(node, relativeNode);
358
+ if (out) callback?.(out.x, out.y, out.width, out.height, out.pageX, out.pageY);
359
+ return out;
360
+ };
361
+ function createMeasureLayout(node) {
362
+ return (relativeTo, callback) => measureLayout(node, relativeTo, callback);
363
+ }