1 | /*
|
2 | * Copyright 2018 Palantir Technologies, Inc. All rights reserved.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 | import { __decorate, __extends } from "tslib";
|
17 | import * as React from "react";
|
18 | import { findDOMNode } from "react-dom";
|
19 | import { polyfill } from "react-lifecycles-compat";
|
20 | import ResizeObserver from "resize-observer-polyfill";
|
21 | import { AbstractPureComponent2 } from "../../common";
|
22 | import { DISPLAYNAME_PREFIX } from "../../common/props";
|
23 | /** `ResizeSensor` requires a single DOM element child and will error otherwise. */
|
24 | var ResizeSensor = /** @class */ (function (_super) {
|
25 | __extends(ResizeSensor, _super);
|
26 | function ResizeSensor() {
|
27 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
28 | _this.element = null;
|
29 | _this.observer = new ResizeObserver(function (entries) { var _a, _b; return (_b = (_a = _this.props).onResize) === null || _b === void 0 ? void 0 : _b.call(_a, entries); });
|
30 | return _this;
|
31 | }
|
32 | ResizeSensor.prototype.render = function () {
|
33 | // pass-through render of single child
|
34 | return React.Children.only(this.props.children);
|
35 | };
|
36 | ResizeSensor.prototype.componentDidMount = function () {
|
37 | this.observeElement();
|
38 | };
|
39 | ResizeSensor.prototype.componentDidUpdate = function (prevProps) {
|
40 | this.observeElement(this.props.observeParents !== prevProps.observeParents);
|
41 | };
|
42 | ResizeSensor.prototype.componentWillUnmount = function () {
|
43 | this.observer.disconnect();
|
44 | };
|
45 | /**
|
46 | * Observe the DOM element, if defined and different from the currently
|
47 | * observed element. Pass `force` argument to skip element checks and always
|
48 | * re-observe.
|
49 | */
|
50 | ResizeSensor.prototype.observeElement = function (force) {
|
51 | if (force === void 0) { force = false; }
|
52 | var element = this.getElement();
|
53 | if (!(element instanceof Element)) {
|
54 | // stop everything if not defined
|
55 | this.observer.disconnect();
|
56 | return;
|
57 | }
|
58 | if (element === this.element && !force) {
|
59 | // quit if given same element -- nothing to update (unless forced)
|
60 | return;
|
61 | }
|
62 | else {
|
63 | // clear observer list if new element
|
64 | this.observer.disconnect();
|
65 | // remember element reference for next time
|
66 | this.element = element;
|
67 | }
|
68 | // observer callback is invoked immediately when observing new elements
|
69 | this.observer.observe(element);
|
70 | if (this.props.observeParents) {
|
71 | var parent_1 = element.parentElement;
|
72 | while (parent_1 != null) {
|
73 | this.observer.observe(parent_1);
|
74 | parent_1 = parent_1.parentElement;
|
75 | }
|
76 | }
|
77 | };
|
78 | ResizeSensor.prototype.getElement = function () {
|
79 | try {
|
80 | // using findDOMNode for two reasons:
|
81 | // 1. cloning to insert a ref is unwieldy and not performant.
|
82 | // 2. ensure that we resolve to an actual DOM node (instead of any JSX ref instance).
|
83 | // HACKHACK: see https://github.com/palantir/blueprint/issues/3979
|
84 | /* eslint-disable-next-line react/no-find-dom-node */
|
85 | return findDOMNode(this);
|
86 | }
|
87 | catch (_a) {
|
88 | // swallow error if findDOMNode is run on unmounted component.
|
89 | return null;
|
90 | }
|
91 | };
|
92 | ResizeSensor.displayName = DISPLAYNAME_PREFIX + ".ResizeSensor";
|
93 | ResizeSensor = __decorate([
|
94 | polyfill
|
95 | ], ResizeSensor);
|
96 | return ResizeSensor;
|
97 | }(AbstractPureComponent2));
|
98 | export { ResizeSensor };
|
99 | //# sourceMappingURL=resizeSensor.js.map |
\ | No newline at end of file |