@tamagui/use-element-layout 1.129.6-1751237024118 → 1.129.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +45 -64
- package/dist/cjs/index.js +56 -50
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/index.native.js +61 -63
- package/dist/cjs/index.native.js.map +2 -2
- package/dist/esm/index.js +56 -50
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +45 -64
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/index.native.js +60 -69
- package/dist/esm/index.native.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +74 -65
- package/types/index.d.ts +1 -2
- package/types/index.d.ts.map +5 -3
package/dist/esm/index.js
CHANGED
|
@@ -5,7 +5,8 @@ let strategy = "async";
|
|
|
5
5
|
function setOnLayoutStrategy(state) {
|
|
6
6
|
strategy = state;
|
|
7
7
|
}
|
|
8
|
-
const NodeRectCache = /* @__PURE__ */ new WeakMap(), ParentRectCache = /* @__PURE__ */ new WeakMap()
|
|
8
|
+
const NodeRectCache = /* @__PURE__ */ new WeakMap(), ParentRectCache = /* @__PURE__ */ new WeakMap();
|
|
9
|
+
const LastChangeTime = /* @__PURE__ */ new WeakMap(), rAF = typeof window < "u" ? window.requestAnimationFrame : void 0;
|
|
9
10
|
let avoidUpdates = !0;
|
|
10
11
|
const queuedUpdates = /* @__PURE__ */ new Map();
|
|
11
12
|
function enable() {
|
|
@@ -15,61 +16,52 @@ if (isClient)
|
|
|
15
16
|
if (rAF) {
|
|
16
17
|
let layoutOnAnimationFrame = function() {
|
|
17
18
|
const now = Date.now(), timeSinceLastFrame = now - lastFrameAt;
|
|
18
|
-
lastFrameAt = now, strategy !== "off" && (timeSinceLastFrame > 16.67 * numDroppedFramesUntilPause || Nodes.forEach(
|
|
19
|
+
lastFrameAt = now, strategy !== "off" && (timeSinceLastFrame > 16.67 * numDroppedFramesUntilPause || Nodes.forEach((node) => {
|
|
20
|
+
updateLayoutIfChanged(node, lastFrameAt);
|
|
21
|
+
})), rAF(layoutOnAnimationFrame);
|
|
19
22
|
}, lastFrameAt = Date.now();
|
|
20
23
|
const numDroppedFramesUntilPause = 2;
|
|
21
|
-
async function updateLayoutIfChanged(node) {
|
|
22
|
-
const
|
|
24
|
+
async function updateLayoutIfChanged(node, frameId) {
|
|
25
|
+
const parentNode = node.parentElement;
|
|
26
|
+
let nodeRect, parentRect;
|
|
27
|
+
if (strategy === "async") {
|
|
28
|
+
const [nr, pr] = await Promise.all([
|
|
29
|
+
getBoundingClientRectAsync(node),
|
|
30
|
+
getBoundingClientRectAsync(parentNode)
|
|
31
|
+
]);
|
|
32
|
+
if (frameId !== lastFrameAt)
|
|
33
|
+
return;
|
|
34
|
+
nodeRect = nr, parentRect = pr;
|
|
35
|
+
} else
|
|
36
|
+
nodeRect = node.getBoundingClientRect(), parentRect = parentNode?.getBoundingClientRect();
|
|
37
|
+
if (!parentRect)
|
|
38
|
+
return;
|
|
39
|
+
const onLayout = LayoutHandlers.get(node);
|
|
23
40
|
if (typeof onLayout != "function") return;
|
|
24
41
|
const cachedRect = NodeRectCache.get(node), cachedParentRect = parentNode ? NodeRectCache.get(parentNode) : null;
|
|
25
42
|
if (!cachedRect || // has changed one rect
|
|
26
|
-
!isEqualShallow(cachedRect, nodeRect) && (!cachedParentRect || !isEqualShallow(cachedParentRect, parentRect)))
|
|
43
|
+
!isEqualShallow(cachedRect, nodeRect) && (!cachedParentRect || !isEqualShallow(cachedParentRect, parentRect))) {
|
|
27
44
|
if (NodeRectCache.set(node, nodeRect), parentRect && parentNode && ParentRectCache.set(parentNode, parentRect), avoidUpdates) {
|
|
28
|
-
const event = getElementLayoutEvent(
|
|
45
|
+
const event = getElementLayoutEvent(nodeRect, parentRect);
|
|
29
46
|
queuedUpdates.set(node, () => onLayout(event));
|
|
30
|
-
} else if (strategy
|
|
31
|
-
const
|
|
32
|
-
LastChangeTime.set(node, now);
|
|
33
|
-
const existingTimer = DebounceTimers.get(node);
|
|
34
|
-
existingTimer && clearTimeout(existingTimer);
|
|
35
|
-
const timer = setTimeout(async () => {
|
|
36
|
-
const lastChange = LastChangeTime.get(node) || 0, timeSinceChange = Date.now() - lastChange;
|
|
37
|
-
if (timeSinceChange >= DEBOUNCE_DELAY) {
|
|
38
|
-
const event = await getElementLayoutEventAsync(node);
|
|
39
|
-
onLayout(event), DebounceTimers.delete(node);
|
|
40
|
-
} else {
|
|
41
|
-
const remainingDelay = DEBOUNCE_DELAY - timeSinceChange, newTimer = setTimeout(async () => {
|
|
42
|
-
const event = await getElementLayoutEventAsync(node);
|
|
43
|
-
onLayout(event), DebounceTimers.delete(node);
|
|
44
|
-
}, remainingDelay);
|
|
45
|
-
DebounceTimers.set(node, newTimer);
|
|
46
|
-
}
|
|
47
|
-
}, DEBOUNCE_DELAY);
|
|
48
|
-
DebounceTimers.set(node, timer);
|
|
49
|
-
} else {
|
|
50
|
-
const event = getElementLayoutEvent(node);
|
|
47
|
+
} else if (strategy !== "async") {
|
|
48
|
+
const event = getElementLayoutEvent(nodeRect, parentRect);
|
|
51
49
|
onLayout(event);
|
|
52
50
|
}
|
|
51
|
+
}
|
|
53
52
|
}
|
|
54
53
|
rAF(layoutOnAnimationFrame);
|
|
55
54
|
} else
|
|
56
55
|
process.env.NODE_ENV === "development" && console.warn(
|
|
57
56
|
"No requestAnimationFrame - please polyfill for onLayout to work correctly"
|
|
58
57
|
);
|
|
59
|
-
const getElementLayoutEvent = (
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
},
|
|
67
|
-
timeStamp: Date.now()
|
|
68
|
-
};
|
|
69
|
-
}), !res)
|
|
70
|
-
throw new Error("\u203C\uFE0F");
|
|
71
|
-
return res;
|
|
72
|
-
}, measureLayout = (node, relativeTo, callback) => {
|
|
58
|
+
const getElementLayoutEvent = (nodeRect, parentRect) => ({
|
|
59
|
+
nativeEvent: {
|
|
60
|
+
layout: getRelativeDimensions(nodeRect, parentRect),
|
|
61
|
+
target: nodeRect
|
|
62
|
+
},
|
|
63
|
+
timeStamp: Date.now()
|
|
64
|
+
}), measureLayout = (node, relativeTo, callback) => {
|
|
73
65
|
const relativeNode = relativeTo || node?.parentElement;
|
|
74
66
|
if (relativeNode instanceof HTMLElement) {
|
|
75
67
|
const nodeDim = node.getBoundingClientRect(), relativeNodeDim = relativeNode.getBoundingClientRect();
|
|
@@ -96,8 +88,8 @@ const getElementLayoutEvent = (target) => {
|
|
|
96
88
|
const relativeNode = relativeTo || node?.parentElement;
|
|
97
89
|
if (relativeNode instanceof HTMLElement) {
|
|
98
90
|
const [nodeDim, relativeNodeDim] = await Promise.all([
|
|
99
|
-
node
|
|
100
|
-
relativeNode
|
|
91
|
+
getBoundingClientRectAsync(node),
|
|
92
|
+
getBoundingClientRectAsync(relativeNode)
|
|
101
93
|
]);
|
|
102
94
|
if (relativeNodeDim && nodeDim) {
|
|
103
95
|
const { x, y, width, height, left, top } = getRelativeDimensions(
|
|
@@ -117,19 +109,33 @@ function useElementLayout(ref, onLayout) {
|
|
|
117
109
|
node && onLayout && LayoutHandlers.set(node, onLayout), useIsomorphicLayoutEffect(() => {
|
|
118
110
|
if (!onLayout) return;
|
|
119
111
|
const node2 = ref.current?.host;
|
|
120
|
-
if (node2)
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
112
|
+
if (!node2) return;
|
|
113
|
+
LayoutHandlers.set(node2, onLayout), Nodes.add(node2);
|
|
114
|
+
const parentNode = node2.parentNode;
|
|
115
|
+
return parentNode && onLayout(
|
|
116
|
+
getElementLayoutEvent(
|
|
117
|
+
node2.getBoundingClientRect(),
|
|
118
|
+
parentNode.getBoundingClientRect()
|
|
119
|
+
)
|
|
120
|
+
), () => {
|
|
121
|
+
Nodes.delete(node2), LayoutHandlers.delete(node2), NodeRectCache.delete(node2), LastChangeTime.delete(node2);
|
|
122
|
+
};
|
|
126
123
|
}, [ref, !!onLayout]);
|
|
127
124
|
}
|
|
128
125
|
function ensureWebElement(x) {
|
|
129
126
|
if (!(typeof HTMLElement > "u"))
|
|
130
127
|
return x instanceof HTMLElement ? x : void 0;
|
|
131
128
|
}
|
|
132
|
-
const
|
|
129
|
+
const getBoundingClientRectAsync = (node) => new Promise((res) => {
|
|
130
|
+
if (!node || node.nodeType !== 1) return;
|
|
131
|
+
const io = new IntersectionObserver(
|
|
132
|
+
(entries) => (io.disconnect(), res(entries[0].boundingClientRect)),
|
|
133
|
+
{
|
|
134
|
+
threshold: 0
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
io.observe(node);
|
|
138
|
+
}), getBoundingClientRect = (node) => {
|
|
133
139
|
if (!(!node || node.nodeType !== 1))
|
|
134
140
|
return node.getBoundingClientRect?.();
|
|
135
141
|
}, getRect = (node) => {
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
|
-
"mappings": "AAAA,SAAS,UAAU,iCAAiC;AACpD,SAAS,sBAAsB;AAG/B,MAAM,iBAAiB,oBAAI,QAA+B,GACpD,QAAQ,oBAAI,IAAiB;AAQnC,IAAI,WAAsC;AAEnC,SAAS,oBAAoB,OAAwC;AAC1E,aAAW;AACb;AAmBA,MAAM,gBAAgB,oBAAI,QAA8B,GAClD,kBAAkB,oBAAI,QAA8B,
|
|
4
|
+
"mappings": "AAAA,SAAS,UAAU,iCAAiC;AACpD,SAAS,sBAAsB;AAG/B,MAAM,iBAAiB,oBAAI,QAA+B,GACpD,QAAQ,oBAAI,IAAiB;AAQnC,IAAI,WAAsC;AAEnC,SAAS,oBAAoB,OAAwC;AAC1E,aAAW;AACb;AAmBA,MAAM,gBAAgB,oBAAI,QAA8B,GAClD,kBAAkB,oBAAI,QAA8B;AAE1D,MAAM,iBAAiB,oBAAI,QAA6B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AK,aAAa,EAAG;AAClC,QAAM,KAAK,IAAI;AAAA,IACb,CAAC,aACC,GAAG,WAAW,GACP,IAAI,QAAQ,CAAC,EAAE,kBAAkB;AAAA,IAE1C;AAAA,MACE,WAAW;AAAA,IACb;AAAA,EACF;AACA,KAAG,QAAQ,IAAI;AACjB,CAAC,GAGG,wBAAwB,CAAC,SAAkD;AAC/E,MAAI,GAAC,QAAQ,KAAK,aAAa;AAC/B,WAAO,KAAK,wBAAwB;AACtC,GAEa,UAAU,CAAC,SAA+C;AACrE,QAAM,OAAO,sBAAsB,IAAI;AACvC,MAAI,CAAC,KAAM;AACX,QAAM,EAAE,GAAG,GAAG,KAAK,KAAK,IAAI;AAC5B,SAAO,EAAE,GAAG,GAAG,OAAO,KAAK,aAAa,QAAQ,KAAK,cAAc,KAAK,KAAK;AAC/E;",
|
|
5
5
|
"names": ["node"]
|
|
6
6
|
}
|
package/dist/esm/index.mjs
CHANGED
|
@@ -7,11 +7,9 @@ function setOnLayoutStrategy(state) {
|
|
|
7
7
|
strategy = state;
|
|
8
8
|
}
|
|
9
9
|
const NodeRectCache = /* @__PURE__ */new WeakMap(),
|
|
10
|
-
ParentRectCache = /* @__PURE__ */new WeakMap()
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
rAF = typeof window < "u" ? window.requestAnimationFrame : void 0,
|
|
14
|
-
DEBOUNCE_DELAY = 32;
|
|
10
|
+
ParentRectCache = /* @__PURE__ */new WeakMap();
|
|
11
|
+
const LastChangeTime = /* @__PURE__ */new WeakMap(),
|
|
12
|
+
rAF = typeof window < "u" ? window.requestAnimationFrame : void 0;
|
|
15
13
|
let avoidUpdates = !0;
|
|
16
14
|
const queuedUpdates = /* @__PURE__ */new Map();
|
|
17
15
|
function enable() {
|
|
@@ -21,71 +19,46 @@ if (isClient) if (rAF) {
|
|
|
21
19
|
let layoutOnAnimationFrame = function () {
|
|
22
20
|
const now = Date.now(),
|
|
23
21
|
timeSinceLastFrame = now - lastFrameAt;
|
|
24
|
-
lastFrameAt = now, strategy !== "off" && (timeSinceLastFrame > 16.67 * numDroppedFramesUntilPause || Nodes.forEach(
|
|
22
|
+
lastFrameAt = now, strategy !== "off" && (timeSinceLastFrame > 16.67 * numDroppedFramesUntilPause || Nodes.forEach(node => {
|
|
23
|
+
updateLayoutIfChanged(node, lastFrameAt);
|
|
24
|
+
})), rAF(layoutOnAnimationFrame);
|
|
25
25
|
},
|
|
26
26
|
lastFrameAt = Date.now();
|
|
27
27
|
const numDroppedFramesUntilPause = 2;
|
|
28
|
-
async function updateLayoutIfChanged(node) {
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
async function updateLayoutIfChanged(node, frameId) {
|
|
29
|
+
const parentNode = node.parentElement;
|
|
30
|
+
let nodeRect, parentRect;
|
|
31
|
+
if (strategy === "async") {
|
|
32
|
+
const [nr, pr] = await Promise.all([getBoundingClientRectAsync(node), getBoundingClientRectAsync(parentNode)]);
|
|
33
|
+
if (frameId !== lastFrameAt) return;
|
|
34
|
+
nodeRect = nr, parentRect = pr;
|
|
35
|
+
} else nodeRect = node.getBoundingClientRect(), parentRect = parentNode?.getBoundingClientRect();
|
|
36
|
+
if (!parentRect) return;
|
|
37
|
+
const onLayout = LayoutHandlers.get(node);
|
|
33
38
|
if (typeof onLayout != "function") return;
|
|
34
39
|
const cachedRect = NodeRectCache.get(node),
|
|
35
40
|
cachedParentRect = parentNode ? NodeRectCache.get(parentNode) : null;
|
|
36
41
|
if (!cachedRect ||
|
|
37
42
|
// has changed one rect
|
|
38
|
-
!isEqualShallow(cachedRect, nodeRect) && (!cachedParentRect || !isEqualShallow(cachedParentRect, parentRect)))
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const timer = setTimeout(async () => {
|
|
47
|
-
const lastChange = LastChangeTime.get(node) || 0,
|
|
48
|
-
timeSinceChange = Date.now() - lastChange;
|
|
49
|
-
if (timeSinceChange >= DEBOUNCE_DELAY) {
|
|
50
|
-
const event = await getElementLayoutEventAsync(node);
|
|
51
|
-
onLayout(event), DebounceTimers.delete(node);
|
|
52
|
-
} else {
|
|
53
|
-
const remainingDelay = DEBOUNCE_DELAY - timeSinceChange,
|
|
54
|
-
newTimer = setTimeout(async () => {
|
|
55
|
-
const event = await getElementLayoutEventAsync(node);
|
|
56
|
-
onLayout(event), DebounceTimers.delete(node);
|
|
57
|
-
}, remainingDelay);
|
|
58
|
-
DebounceTimers.set(node, newTimer);
|
|
59
|
-
}
|
|
60
|
-
}, DEBOUNCE_DELAY);
|
|
61
|
-
DebounceTimers.set(node, timer);
|
|
62
|
-
} else {
|
|
63
|
-
const event = getElementLayoutEvent(node);
|
|
64
|
-
onLayout(event);
|
|
43
|
+
!isEqualShallow(cachedRect, nodeRect) && (!cachedParentRect || !isEqualShallow(cachedParentRect, parentRect))) {
|
|
44
|
+
if (NodeRectCache.set(node, nodeRect), parentRect && parentNode && ParentRectCache.set(parentNode, parentRect), avoidUpdates) {
|
|
45
|
+
const event = getElementLayoutEvent(nodeRect, parentRect);
|
|
46
|
+
queuedUpdates.set(node, () => onLayout(event));
|
|
47
|
+
} else if (strategy !== "async") {
|
|
48
|
+
const event = getElementLayoutEvent(nodeRect, parentRect);
|
|
49
|
+
onLayout(event);
|
|
50
|
+
}
|
|
65
51
|
}
|
|
66
52
|
}
|
|
67
53
|
rAF(layoutOnAnimationFrame);
|
|
68
54
|
} else process.env.NODE_ENV === "development" && console.warn("No requestAnimationFrame - please polyfill for onLayout to work correctly");
|
|
69
|
-
const getElementLayoutEvent =
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
y,
|
|
77
|
-
width,
|
|
78
|
-
height,
|
|
79
|
-
left,
|
|
80
|
-
top
|
|
81
|
-
},
|
|
82
|
-
target
|
|
83
|
-
},
|
|
84
|
-
timeStamp: Date.now()
|
|
85
|
-
};
|
|
86
|
-
}), !res) throw new Error("\u203C\uFE0F");
|
|
87
|
-
return res;
|
|
88
|
-
},
|
|
55
|
+
const getElementLayoutEvent = (nodeRect, parentRect) => ({
|
|
56
|
+
nativeEvent: {
|
|
57
|
+
layout: getRelativeDimensions(nodeRect, parentRect),
|
|
58
|
+
target: nodeRect
|
|
59
|
+
},
|
|
60
|
+
timeStamp: Date.now()
|
|
61
|
+
}),
|
|
89
62
|
measureLayout = (node, relativeTo, callback) => {
|
|
90
63
|
const relativeNode = relativeTo || node?.parentElement;
|
|
91
64
|
if (relativeNode instanceof HTMLElement) {
|
|
@@ -118,7 +91,7 @@ const getElementLayoutEvent = target => {
|
|
|
118
91
|
measureLayoutAsync = async (node, relativeTo) => {
|
|
119
92
|
const relativeNode = relativeTo || node?.parentElement;
|
|
120
93
|
if (relativeNode instanceof HTMLElement) {
|
|
121
|
-
const [nodeDim, relativeNodeDim] = await Promise.all([node
|
|
94
|
+
const [nodeDim, relativeNodeDim] = await Promise.all([getBoundingClientRectAsync(node), getBoundingClientRectAsync(relativeNode)]);
|
|
122
95
|
if (relativeNodeDim && nodeDim) {
|
|
123
96
|
const {
|
|
124
97
|
x,
|
|
@@ -163,17 +136,25 @@ function useElementLayout(ref, onLayout) {
|
|
|
163
136
|
node && onLayout && LayoutHandlers.set(node, onLayout), useIsomorphicLayoutEffect(() => {
|
|
164
137
|
if (!onLayout) return;
|
|
165
138
|
const node2 = ref.current?.host;
|
|
166
|
-
if (node2) return
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
139
|
+
if (!node2) return;
|
|
140
|
+
LayoutHandlers.set(node2, onLayout), Nodes.add(node2);
|
|
141
|
+
const parentNode = node2.parentNode;
|
|
142
|
+
return parentNode && onLayout(getElementLayoutEvent(node2.getBoundingClientRect(), parentNode.getBoundingClientRect())), () => {
|
|
143
|
+
Nodes.delete(node2), LayoutHandlers.delete(node2), NodeRectCache.delete(node2), LastChangeTime.delete(node2);
|
|
170
144
|
};
|
|
171
145
|
}, [ref, !!onLayout]);
|
|
172
146
|
}
|
|
173
147
|
function ensureWebElement(x) {
|
|
174
148
|
if (!(typeof HTMLElement > "u")) return x instanceof HTMLElement ? x : void 0;
|
|
175
149
|
}
|
|
176
|
-
const
|
|
150
|
+
const getBoundingClientRectAsync = node => new Promise(res => {
|
|
151
|
+
if (!node || node.nodeType !== 1) return;
|
|
152
|
+
const io = new IntersectionObserver(entries => (io.disconnect(), res(entries[0].boundingClientRect)), {
|
|
153
|
+
threshold: 0
|
|
154
|
+
});
|
|
155
|
+
io.observe(node);
|
|
156
|
+
}),
|
|
157
|
+
getBoundingClientRect = node => {
|
|
177
158
|
if (!(!node || node.nodeType !== 1)) return node.getBoundingClientRect?.();
|
|
178
159
|
},
|
|
179
160
|
getRect = node => {
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["isClient","useIsomorphicLayoutEffect","isEqualShallow","LayoutHandlers","WeakMap","Nodes","Set","strategy","setOnLayoutStrategy","state","NodeRectCache","ParentRectCache","
|
|
1
|
+
{"version":3,"names":["isClient","useIsomorphicLayoutEffect","isEqualShallow","LayoutHandlers","WeakMap","Nodes","Set","strategy","setOnLayoutStrategy","state","NodeRectCache","ParentRectCache","LastChangeTime","rAF","window","requestAnimationFrame","avoidUpdates","queuedUpdates","Map","enable","forEach","cb","clear","layoutOnAnimationFrame","now","Date","timeSinceLastFrame","lastFrameAt","numDroppedFramesUntilPause","node","updateLayoutIfChanged","frameId","parentNode","parentElement","nodeRect","parentRect","nr","pr","Promise","all","getBoundingClientRectAsync","getBoundingClientRect","onLayout","get","cachedRect","cachedParentRect","set","event","getElementLayoutEvent","process","env","NODE_ENV","console","warn","nativeEvent","layout","getRelativeDimensions","target","timeStamp","measureLayout","relativeTo","callback","relativeNode","HTMLElement","nodeDim","relativeNodeDim","x","y","width","height","left","top","getElementLayoutEventAsync","measureLayoutAsync","Error","a","b","useElementLayout","ref","ensureWebElement","current","host","node2","add","delete","res","nodeType","io","IntersectionObserver","entries","disconnect","boundingClientRect","threshold","observe","getRect","rect","offsetWidth","offsetHeight"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,SAASA,QAAA,EAAUC,yBAAA,QAAiC;AACpD,SAASC,cAAA,QAAsB;AAG/B,MAAMC,cAAA,GAAiB,mBAAIC,OAAA,CAA+B;EACpDC,KAAA,GAAQ,mBAAIC,GAAA,CAAiB;AAQnC,IAAIC,QAAA,GAAsC;AAEnC,SAASC,oBAAoBC,KAAA,EAAwC;EAC1EF,QAAA,GAAWE,KAAA;AACb;AAmBA,MAAMC,aAAA,GAAgB,mBAAIN,OAAA,CAA8B;EAClDO,eAAA,GAAkB,mBAAIP,OAAA,CAA8B;AAE1D,MAAMQ,cAAA,GAAiB,mBAAIR,OAAA,CAA6B;EAElDS,GAAA,GAAM,OAAOC,MAAA,GAAW,MAAcA,MAAA,CAAOC,qBAAA,GAAwB;AAG3E,IAAIC,YAAA,GAAe;AACnB,MAAMC,aAAA,GAAgB,mBAAIC,GAAA,CAA2B;AAE9C,SAASC,OAAA,EAAe;EACzBH,YAAA,KACFA,YAAA,GAAe,IACXC,aAAA,KACFA,aAAA,CAAcG,OAAA,CAASC,EAAA,IAAOA,EAAA,CAAG,CAAC,GAClCJ,aAAA,CAAcK,KAAA,CAAM;AAG1B;AAEA,IAAItB,QAAA,EACF,IAAIa,GAAA,EAAK;EAkEP,IAASU,sBAAA,GAAT,SAAAA,CAAA,EAAkC;MAChC,MAAMC,GAAA,GAAMC,IAAA,CAAKD,GAAA,CAAI;QACfE,kBAAA,GAAqBF,GAAA,GAAMG,WAAA;MACjCA,WAAA,GAAcH,GAAA,EAEVjB,QAAA,KAAa,UAKbmB,kBAAA,GAAqB,QAAoBE,0BAAA,IAGzCvB,KAAA,CAAMe,OAAA,CAASS,IAAA,IAAS;QACtBC,qBAAA,CAAsBD,IAAA,EAAMF,WAAW;MACzC,CAAC,IAILd,GAAA,CAA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,qBAAA,GAAyBZ,IAAA,IAAkD;IAC/E,IAAI,GAACA,IAAA,IAAQA,IAAA,CAAKyD,QAAA,KAAa,IAC/B,OAAOzD,IAAA,CAAKY,qBAAA,GAAwB;EACtC;EAEaqD,OAAA,GAAWjE,IAAA,IAA+C;IACrE,MAAMkE,IAAA,GAAOtD,qBAAA,CAAsBZ,IAAI;IACvC,IAAI,CAACkE,IAAA,EAAM;IACX,MAAM;MAAE7B,CAAA;MAAGC,CAAA;MAAGI,GAAA;MAAKD;IAAK,IAAIyB,IAAA;IAC5B,OAAO;MAAE7B,CAAA;MAAGC,CAAA;MAAGC,KAAA,EAAOvC,IAAA,CAAKmE,WAAA;MAAa3B,MAAA,EAAQxC,IAAA,CAAKoE,YAAA;MAAc1B,GAAA;MAAKD;IAAK;EAC/E","ignoreList":[]}
|
package/dist/esm/index.native.js
CHANGED
|
@@ -7,11 +7,9 @@ function setOnLayoutStrategy(state) {
|
|
|
7
7
|
strategy = state;
|
|
8
8
|
}
|
|
9
9
|
var NodeRectCache = /* @__PURE__ */new WeakMap(),
|
|
10
|
-
ParentRectCache = /* @__PURE__ */new WeakMap()
|
|
11
|
-
|
|
12
|
-
LastChangeTime = /* @__PURE__ */new WeakMap(),
|
|
10
|
+
ParentRectCache = /* @__PURE__ */new WeakMap();
|
|
11
|
+
var LastChangeTime = /* @__PURE__ */new WeakMap(),
|
|
13
12
|
rAF = typeof window < "u" ? window.requestAnimationFrame : void 0,
|
|
14
|
-
DEBOUNCE_DELAY = 32,
|
|
15
13
|
avoidUpdates = !0,
|
|
16
14
|
queuedUpdates = /* @__PURE__ */new Map();
|
|
17
15
|
function enable() {
|
|
@@ -26,76 +24,55 @@ if (isClient) if (rAF) {
|
|
|
26
24
|
if (lastFrameAt = now, strategy !== "off") {
|
|
27
25
|
var expectedFrameTime = 16.67,
|
|
28
26
|
hasRecentSyncWork = timeSinceLastFrame > expectedFrameTime * numDroppedFramesUntilPause;
|
|
29
|
-
hasRecentSyncWork || Nodes.forEach(
|
|
27
|
+
hasRecentSyncWork || Nodes.forEach(function (node) {
|
|
28
|
+
updateLayoutIfChanged(node, lastFrameAt);
|
|
29
|
+
});
|
|
30
30
|
}
|
|
31
31
|
rAF(layoutOnAnimationFrame);
|
|
32
32
|
};
|
|
33
33
|
var layoutOnAnimationFrame2 = layoutOnAnimationFrame,
|
|
34
34
|
lastFrameAt = Date.now(),
|
|
35
35
|
numDroppedFramesUntilPause = 2;
|
|
36
|
-
async function updateLayoutIfChanged(node) {
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
-
parentRect
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
onLayout(event2), DebounceTimers.delete(node);
|
|
62
|
-
} else {
|
|
63
|
-
var remainingDelay = DEBOUNCE_DELAY - timeSinceChange,
|
|
64
|
-
newTimer = setTimeout(async function () {
|
|
65
|
-
var event3 = await getElementLayoutEventAsync(node);
|
|
66
|
-
onLayout(event3), DebounceTimers.delete(node);
|
|
67
|
-
}, remainingDelay);
|
|
68
|
-
DebounceTimers.set(node, newTimer);
|
|
36
|
+
async function updateLayoutIfChanged(node, frameId) {
|
|
37
|
+
var parentNode = node.parentElement,
|
|
38
|
+
nodeRect,
|
|
39
|
+
parentRect;
|
|
40
|
+
if (strategy === "async") {
|
|
41
|
+
var [nr, pr] = await Promise.all([getBoundingClientRectAsync(node), getBoundingClientRectAsync(parentNode)]);
|
|
42
|
+
if (frameId !== lastFrameAt) return;
|
|
43
|
+
nodeRect = nr, parentRect = pr;
|
|
44
|
+
} else nodeRect = node.getBoundingClientRect(), parentRect = parentNode?.getBoundingClientRect();
|
|
45
|
+
if (parentRect) {
|
|
46
|
+
var onLayout = LayoutHandlers.get(node);
|
|
47
|
+
if (typeof onLayout == "function") {
|
|
48
|
+
var cachedRect = NodeRectCache.get(node),
|
|
49
|
+
cachedParentRect = parentNode ? NodeRectCache.get(parentNode) : null;
|
|
50
|
+
if (!cachedRect ||
|
|
51
|
+
// has changed one rect
|
|
52
|
+
!isEqualShallow(cachedRect, nodeRect) && (!cachedParentRect || !isEqualShallow(cachedParentRect, parentRect))) {
|
|
53
|
+
if (NodeRectCache.set(node, nodeRect), parentRect && parentNode && ParentRectCache.set(parentNode, parentRect), avoidUpdates) {
|
|
54
|
+
var event = getElementLayoutEvent(nodeRect, parentRect);
|
|
55
|
+
queuedUpdates.set(node, function () {
|
|
56
|
+
return onLayout(event);
|
|
57
|
+
});
|
|
58
|
+
} else if (strategy !== "async") {
|
|
59
|
+
var event1 = getElementLayoutEvent(nodeRect, parentRect);
|
|
60
|
+
onLayout(event1);
|
|
69
61
|
}
|
|
70
|
-
}
|
|
71
|
-
DebounceTimers.set(node, timer);
|
|
72
|
-
} else {
|
|
73
|
-
var event1 = getElementLayoutEvent(node);
|
|
74
|
-
onLayout(event1);
|
|
62
|
+
}
|
|
75
63
|
}
|
|
76
64
|
}
|
|
77
65
|
}
|
|
78
66
|
rAF(layoutOnAnimationFrame);
|
|
79
67
|
} else process.env.NODE_ENV === "development" && console.warn("No requestAnimationFrame - please polyfill for onLayout to work correctly");
|
|
80
|
-
var getElementLayoutEvent = function (
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
width,
|
|
89
|
-
height,
|
|
90
|
-
left,
|
|
91
|
-
top
|
|
92
|
-
},
|
|
93
|
-
target
|
|
94
|
-
},
|
|
95
|
-
timeStamp: Date.now()
|
|
96
|
-
};
|
|
97
|
-
}), !res) throw new Error("\u203C\uFE0F");
|
|
98
|
-
return res;
|
|
68
|
+
var getElementLayoutEvent = function (nodeRect, parentRect) {
|
|
69
|
+
return {
|
|
70
|
+
nativeEvent: {
|
|
71
|
+
layout: getRelativeDimensions(nodeRect, parentRect),
|
|
72
|
+
target: nodeRect
|
|
73
|
+
},
|
|
74
|
+
timeStamp: Date.now()
|
|
75
|
+
};
|
|
99
76
|
},
|
|
100
77
|
measureLayout = function (node, relativeTo, callback) {
|
|
101
78
|
var relativeNode = relativeTo || node?.parentElement;
|
|
@@ -129,7 +106,7 @@ var getElementLayoutEvent = function (target) {
|
|
|
129
106
|
measureLayoutAsync = async function (node, relativeTo) {
|
|
130
107
|
var relativeNode = relativeTo || node?.parentElement;
|
|
131
108
|
if (relativeNode instanceof HTMLElement) {
|
|
132
|
-
var [nodeDim, relativeNodeDim] = await Promise.all([node
|
|
109
|
+
var [nodeDim, relativeNodeDim] = await Promise.all([getBoundingClientRectAsync(node), getBoundingClientRectAsync(relativeNode)]);
|
|
133
110
|
if (relativeNodeDim && nodeDim) {
|
|
134
111
|
var {
|
|
135
112
|
x,
|
|
@@ -176,18 +153,32 @@ function useElementLayout(ref, onLayout) {
|
|
|
176
153
|
var _ref_current2;
|
|
177
154
|
if (onLayout) {
|
|
178
155
|
var node2 = (_ref_current2 = ref.current) === null || _ref_current2 === void 0 ? void 0 : _ref_current2.host;
|
|
179
|
-
if (node2)
|
|
180
|
-
|
|
181
|
-
var
|
|
182
|
-
|
|
183
|
-
|
|
156
|
+
if (node2) {
|
|
157
|
+
LayoutHandlers.set(node2, onLayout), Nodes.add(node2);
|
|
158
|
+
var parentNode = node2.parentNode;
|
|
159
|
+
return parentNode && onLayout(getElementLayoutEvent(node2.getBoundingClientRect(), parentNode.getBoundingClientRect())), function () {
|
|
160
|
+
Nodes.delete(node2), LayoutHandlers.delete(node2), NodeRectCache.delete(node2), LastChangeTime.delete(node2);
|
|
161
|
+
};
|
|
162
|
+
}
|
|
184
163
|
}
|
|
185
164
|
}, [ref, !!onLayout]);
|
|
186
165
|
}
|
|
187
166
|
function ensureWebElement(x) {
|
|
188
167
|
if (!(typeof HTMLElement > "u")) return x instanceof HTMLElement ? x : void 0;
|
|
189
168
|
}
|
|
190
|
-
var
|
|
169
|
+
var getBoundingClientRectAsync = function (node) {
|
|
170
|
+
return new Promise(function (res) {
|
|
171
|
+
if (!(!node || node.nodeType !== 1)) {
|
|
172
|
+
var io = new IntersectionObserver(function (entries) {
|
|
173
|
+
return io.disconnect(), res(entries[0].boundingClientRect);
|
|
174
|
+
}, {
|
|
175
|
+
threshold: 0
|
|
176
|
+
});
|
|
177
|
+
io.observe(node);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
},
|
|
181
|
+
getBoundingClientRect = function (node) {
|
|
191
182
|
var _node_getBoundingClientRect;
|
|
192
183
|
if (!(!node || node.nodeType !== 1)) return (_node_getBoundingClientRect = node.getBoundingClientRect) === null || _node_getBoundingClientRect === void 0 ? void 0 : _node_getBoundingClientRect.call(node);
|
|
193
184
|
},
|