1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | var tslib_1 = require("tslib");
|
---|
4 | var react_1 = require("react");
|
---|
5 | var useIsomorphicLayoutEffect_1 = tslib_1.__importDefault(require("./useIsomorphicLayoutEffect"));
|
---|
6 | var util_1 = require("./misc/util");
|
---|
7 | var defaultState = {
|
---|
8 | x: 0,
|
---|
9 | y: 0,
|
---|
10 | width: 0,
|
---|
11 | height: 0,
|
---|
12 | top: 0,
|
---|
13 | left: 0,
|
---|
14 | bottom: 0,
|
---|
15 | right: 0,
|
---|
16 | };
|
---|
17 | function useMeasure() {
|
---|
18 | var _a = react_1.useState(null), element = _a[0], ref = _a[1];
|
---|
19 | var _b = react_1.useState(defaultState), rect = _b[0], setRect = _b[1];
|
---|
20 | var observer = react_1.useMemo(function () {
|
---|
21 | return new window.ResizeObserver(function (entries) {
|
---|
22 | if (entries[0]) {
|
---|
23 | var _a = entries[0].contentRect, x = _a.x, y = _a.y, width = _a.width, height = _a.height, top_1 = _a.top, left = _a.left, bottom = _a.bottom, right = _a.right;
|
---|
24 | setRect({ x: x, y: y, width: width, height: height, top: top_1, left: left, bottom: bottom, right: right });
|
---|
25 | }
|
---|
26 | });
|
---|
27 | }, []);
|
---|
28 | useIsomorphicLayoutEffect_1.default(function () {
|
---|
29 | if (!element)
|
---|
30 | return;
|
---|
31 | observer.observe(element);
|
---|
32 | return function () {
|
---|
33 | observer.disconnect();
|
---|
34 | };
|
---|
35 | }, [element]);
|
---|
36 | return [ref, rect];
|
---|
37 | }
|
---|
38 | exports.default = util_1.isBrowser && typeof window.ResizeObserver !== 'undefined'
|
---|
39 | ? useMeasure
|
---|
40 | : (function () { return [util_1.noop, defaultState]; });
|
---|