oolib 2.230.1 → 2.230.2
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.
|
@@ -3,7 +3,7 @@ export interface useContainerQueryOptions {
|
|
|
3
3
|
enabled?: boolean;
|
|
4
4
|
}
|
|
5
5
|
export interface useContainerQueryReturn {
|
|
6
|
-
ref?:
|
|
6
|
+
ref?: (node: HTMLDivElement | null) => void;
|
|
7
7
|
containerQuery?: (point: breakpoint) => (styles: string | TemplateStringsArray) => string;
|
|
8
8
|
containerWidth: number;
|
|
9
9
|
containerHeight: number;
|
|
@@ -3,38 +3,57 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.useContainerQuery = void 0;
|
|
4
4
|
var mixins_1 = require("../../themes/mixins");
|
|
5
5
|
var react_1 = require("react");
|
|
6
|
+
var RESIZE_DEBOUNCE_MS = 200;
|
|
6
7
|
var useContainerQuery = function (options) {
|
|
7
8
|
if (options === void 0) { options = {}; }
|
|
8
9
|
var enabled = options.enabled;
|
|
9
|
-
var
|
|
10
|
-
var
|
|
10
|
+
var _a = (0, react_1.useState)(null), node = _a[0], setNode = _a[1];
|
|
11
|
+
var _b = (0, react_1.useState)({ width: 0, height: 0 }), containerSize = _b[0], setContainerSize = _b[1];
|
|
12
|
+
var debounceTimerRef = (0, react_1.useRef)(null);
|
|
11
13
|
(0, react_1.useEffect)(function () {
|
|
14
|
+
if (!node)
|
|
15
|
+
return;
|
|
16
|
+
// Synchronous initial measurement to avoid flash of incorrect layout
|
|
17
|
+
var rect = node.getBoundingClientRect();
|
|
18
|
+
setContainerSize({ width: rect.width, height: rect.height });
|
|
12
19
|
var resizeObserver = new ResizeObserver(function (entries) {
|
|
20
|
+
var _loop_1 = function (entry) {
|
|
21
|
+
var _a = entry.contentBoxSize[0], inlineSize = _a.inlineSize, blockSize = _a.blockSize;
|
|
22
|
+
if (debounceTimerRef.current) {
|
|
23
|
+
clearTimeout(debounceTimerRef.current);
|
|
24
|
+
}
|
|
25
|
+
debounceTimerRef.current = setTimeout(function () {
|
|
26
|
+
setContainerSize(function (prev) {
|
|
27
|
+
if (Math.abs(prev.width - inlineSize) > 2 || Math.abs(prev.height - blockSize) > 2) {
|
|
28
|
+
return { width: inlineSize, height: blockSize };
|
|
29
|
+
}
|
|
30
|
+
return prev;
|
|
31
|
+
});
|
|
32
|
+
}, RESIZE_DEBOUNCE_MS);
|
|
33
|
+
};
|
|
13
34
|
for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
|
|
14
35
|
var entry = entries_1[_i];
|
|
15
|
-
|
|
16
|
-
setContainerSize({ width: inlineSize, height: blockSize });
|
|
36
|
+
_loop_1(entry);
|
|
17
37
|
}
|
|
18
38
|
});
|
|
19
|
-
|
|
20
|
-
resizeObserver.observe(ref.current);
|
|
21
|
-
}
|
|
39
|
+
resizeObserver.observe(node);
|
|
22
40
|
return function () {
|
|
23
|
-
|
|
24
|
-
|
|
41
|
+
resizeObserver.disconnect();
|
|
42
|
+
if (debounceTimerRef.current) {
|
|
43
|
+
clearTimeout(debounceTimerRef.current);
|
|
25
44
|
}
|
|
26
45
|
};
|
|
27
|
-
}, [
|
|
46
|
+
}, [node]);
|
|
28
47
|
//if container queries are written for multiple breakpoints
|
|
29
|
-
//then they must be written in the order of small to big.
|
|
30
|
-
//so for example, the 'sm' query should be written before the 'md'
|
|
48
|
+
//then they must be written in the order of small to big.
|
|
49
|
+
//so for example, the 'sm' query should be written before the 'md'
|
|
31
50
|
//query and so on. Else bugs will happen.
|
|
32
51
|
var containerQuery = function (point) { return function (styles) {
|
|
33
52
|
var breakpointWidth = (0, mixins_1.getBreakPoint)(point);
|
|
34
53
|
return containerSize.width >= breakpointWidth ? styles : "";
|
|
35
54
|
}; };
|
|
36
55
|
return {
|
|
37
|
-
ref: enabled ?
|
|
56
|
+
ref: enabled ? setNode : undefined,
|
|
38
57
|
containerQuery: enabled ? containerQuery : undefined,
|
|
39
58
|
containerWidth: containerSize.width,
|
|
40
59
|
containerHeight: containerSize.height,
|