@rescui/use-resize-observer 0.3.5-RUI-308-Fix-select-issues-405b9fa40.12 → 0.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.
- package/lib/use-resize-observer.js +96 -56
- package/package.json +6 -5
|
@@ -1,77 +1,117 @@
|
|
|
1
1
|
"use client";
|
|
2
|
+
import { c } from 'react-compiler-runtime';
|
|
2
3
|
import { useRef, useEffect, useLayoutEffect } from 'react';
|
|
3
4
|
const useIsomorphicLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
|
|
4
5
|
|
|
5
|
-
const useResizeObserver =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const useResizeObserver = (target, callback, t0) => {
|
|
7
|
+
const $ = c(9);
|
|
8
|
+
let t1;
|
|
9
|
+
|
|
10
|
+
if ($[0] !== t0) {
|
|
11
|
+
t1 = t0 === undefined ? {
|
|
12
|
+
ignoreFirstCall: false
|
|
13
|
+
} : t0;
|
|
14
|
+
$[0] = t0;
|
|
15
|
+
$[1] = t1;
|
|
16
|
+
} else {
|
|
17
|
+
t1 = $[1];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const options = t1;
|
|
9
21
|
const cb = useRef(null);
|
|
10
22
|
const {
|
|
11
23
|
ignoreFirstCall
|
|
12
|
-
} = options;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
let targetEl;
|
|
23
|
-
|
|
24
|
-
if ('current' in target) {
|
|
25
|
-
targetEl = target.current;
|
|
26
|
-
} else {
|
|
27
|
-
targetEl = target;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (!targetEl) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const ownerWindow = targetEl.ownerDocument.defaultView;
|
|
35
|
-
const ResizeObserver = ownerWindow?.ResizeObserver;
|
|
36
|
-
|
|
37
|
-
if (!ResizeObserver) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
let firstCall = true;
|
|
42
|
-
let abort = false; // eslint-disable-next-line complexity
|
|
43
|
-
|
|
44
|
-
const onResize = entries => {
|
|
45
|
-
// on the first call, delay it by a frame, see https://stackoverflow.com/a/58701523
|
|
46
|
-
if (firstCall) {
|
|
47
|
-
firstCall = false;
|
|
48
|
-
|
|
49
|
-
if (!ignoreFirstCall) {
|
|
50
|
-
ownerWindow.requestAnimationFrame(() => {
|
|
51
|
-
onResize(entries);
|
|
52
|
-
});
|
|
53
|
-
}
|
|
24
|
+
} = options;
|
|
25
|
+
let t2;
|
|
26
|
+
let t3;
|
|
27
|
+
|
|
28
|
+
if ($[2] !== callback) {
|
|
29
|
+
t2 = () => {
|
|
30
|
+
cb.current = callback;
|
|
31
|
+
};
|
|
54
32
|
|
|
33
|
+
t3 = [callback];
|
|
34
|
+
$[2] = callback;
|
|
35
|
+
$[3] = t2;
|
|
36
|
+
$[4] = t3;
|
|
37
|
+
} else {
|
|
38
|
+
t2 = $[3];
|
|
39
|
+
t3 = $[4];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
useIsomorphicLayoutEffect(t2, t3);
|
|
43
|
+
let t4;
|
|
44
|
+
let t5;
|
|
45
|
+
|
|
46
|
+
if ($[5] !== ignoreFirstCall || $[6] !== target) {
|
|
47
|
+
t4 = () => {
|
|
48
|
+
if (!target) {
|
|
55
49
|
return;
|
|
56
50
|
}
|
|
57
51
|
|
|
58
|
-
|
|
52
|
+
let targetEl;
|
|
53
|
+
|
|
54
|
+
if ("current" in target) {
|
|
55
|
+
targetEl = target.current;
|
|
56
|
+
} else {
|
|
57
|
+
targetEl = target;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!targetEl) {
|
|
59
61
|
return;
|
|
60
62
|
}
|
|
61
63
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
+
const ownerWindow = targetEl.ownerDocument.defaultView;
|
|
65
|
+
const ResizeObserver = ownerWindow?.ResizeObserver;
|
|
66
|
+
|
|
67
|
+
if (!ResizeObserver) {
|
|
68
|
+
return;
|
|
64
69
|
}
|
|
65
|
-
};
|
|
66
70
|
|
|
67
|
-
|
|
68
|
-
|
|
71
|
+
let firstCall = true;
|
|
72
|
+
let abort = false;
|
|
69
73
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
const onResize = entries => {
|
|
75
|
+
if (firstCall) {
|
|
76
|
+
firstCall = false;
|
|
77
|
+
|
|
78
|
+
if (!ignoreFirstCall) {
|
|
79
|
+
ownerWindow.requestAnimationFrame(() => {
|
|
80
|
+
onResize(entries);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (!Array.isArray(entries) || !entries.length || abort) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (cb.current && typeof cb.current === "function") {
|
|
92
|
+
cb.current(entries[0]);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const resizeObserver = new ResizeObserver(onResize);
|
|
97
|
+
resizeObserver.observe(targetEl);
|
|
98
|
+
return () => {
|
|
99
|
+
abort = true;
|
|
100
|
+
resizeObserver.disconnect();
|
|
101
|
+
};
|
|
73
102
|
};
|
|
74
|
-
|
|
103
|
+
|
|
104
|
+
t5 = [target, ignoreFirstCall];
|
|
105
|
+
$[5] = ignoreFirstCall;
|
|
106
|
+
$[6] = target;
|
|
107
|
+
$[7] = t4;
|
|
108
|
+
$[8] = t5;
|
|
109
|
+
} else {
|
|
110
|
+
t4 = $[7];
|
|
111
|
+
t5 = $[8];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
useEffect(t4, t5);
|
|
75
115
|
};
|
|
76
116
|
|
|
77
117
|
export { useResizeObserver };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rescui/use-resize-observer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "ResizeObserver hook",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "JetBrains",
|
|
@@ -16,17 +16,18 @@
|
|
|
16
16
|
"access": "public"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@babel/runtime-corejs3": "^7.26.0"
|
|
19
|
+
"@babel/runtime-corejs3": "^7.26.0",
|
|
20
|
+
"react-compiler-runtime": "^1.0.0"
|
|
20
21
|
},
|
|
21
22
|
"peerDependencies": {
|
|
22
|
-
"react": ">=
|
|
23
|
+
"react": ">=17.0.0 <20"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
|
-
"@rescui/scripts": "0.5.
|
|
26
|
+
"@rescui/scripts": "^0.5.3"
|
|
26
27
|
},
|
|
27
28
|
"scripts": {
|
|
28
29
|
"build": "rescui-scripts build"
|
|
29
30
|
},
|
|
30
31
|
"nx": {},
|
|
31
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "8ffbbe20b8bcbb9ea15204113ab603675d4c03cd"
|
|
32
33
|
}
|