react-resize-detector
Version:
React resize detector
319 lines (246 loc) • 12.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactDom = require("react-dom");
var _resizeObserverPolyfill = _interopRequireDefault(require("resize-observer-polyfill"));
var _rafSchd = _interopRequireDefault(require("raf-schd"));
var _propTypes = require("prop-types");
var _utils = require("../lib/utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var ResizeDetector = /*#__PURE__*/function (_PureComponent) {
_inherits(ResizeDetector, _PureComponent);
var _super = _createSuper(ResizeDetector);
function ResizeDetector(props) {
var _this;
_classCallCheck(this, ResizeDetector);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "cancelHandler", function () {
if (_this.resizeHandler && _this.resizeHandler.cancel) {
// cancel debounced handler
_this.resizeHandler.cancel();
_this.resizeHandler = null;
}
});
_defineProperty(_assertThisInitialized(_this), "rafClean", function () {
if (_this.raf && _this.raf.cancel) {
_this.raf.cancel();
_this.raf = null;
}
});
_defineProperty(_assertThisInitialized(_this), "toggleObserver", function (type) {
var element = _this.getElement();
if (!element || !_this.resizeObserver[type]) return;
_this.resizeObserver[type](element);
});
_defineProperty(_assertThisInitialized(_this), "getElement", function () {
var _this$props = _this.props,
querySelector = _this$props.querySelector,
targetDomEl = _this$props.targetDomEl;
if ((0, _utils.isSSR)()) return undefined; // in case we pass a querySelector
if (querySelector) return document.querySelector(querySelector); // in case we pass a DOM element
if (targetDomEl && (0, _utils.isDOMElement)(targetDomEl)) return targetDomEl; // in case we pass a React ref using React.createRef()
if (_this.targetRef && (0, _utils.isDOMElement)(_this.targetRef.current)) return _this.targetRef.current; // the worse case when we don't receive any information from the parent and the library doesn't add any wrappers
// we have to use a deprecated `findDOMNode` method in order to find a DOM element to attach to
// eslint-disable-next-line react/no-find-dom-node
var currentElement = (0, _reactDom.findDOMNode)(_assertThisInitialized(_this));
if (!currentElement) return undefined;
var renderType = _this.getRenderType();
switch (renderType) {
case 'renderProp':
return currentElement;
case 'childFunction':
return currentElement;
case 'child':
return currentElement;
case 'childArray':
return currentElement;
default:
return currentElement.parentElement;
}
});
_defineProperty(_assertThisInitialized(_this), "createUpdater", function () {
_this.rafClean();
_this.raf = (0, _rafSchd["default"])(function (_ref) {
var width = _ref.width,
height = _ref.height;
var onResize = _this.props.onResize;
if ((0, _utils.isFunction)(onResize)) {
onResize(width, height);
}
_this.setState({
width: width,
height: height
});
});
return _this.raf;
});
_defineProperty(_assertThisInitialized(_this), "createResizeHandler", function (entries) {
var _this$state = _this.state,
widthCurrent = _this$state.width,
heightCurrent = _this$state.height;
var _this$props2 = _this.props,
handleWidth = _this$props2.handleWidth,
handleHeight = _this$props2.handleHeight;
if (!handleWidth && !handleHeight) return;
var updater = _this.createUpdater();
entries.forEach(function (entry) {
var _ref2 = entry && entry.contentRect || {},
width = _ref2.width,
height = _ref2.height;
var isWidthChanged = handleWidth && widthCurrent !== width;
var isHeightChanged = handleHeight && heightCurrent !== height;
var isSizeChanged = isWidthChanged || isHeightChanged;
var shouldSetSize = !_this.skipOnMount && isSizeChanged && !(0, _utils.isSSR)();
if (shouldSetSize) {
updater({
width: width,
height: height
});
}
_this.skipOnMount = false;
});
});
_defineProperty(_assertThisInitialized(_this), "getRenderType", function () {
var _this$props3 = _this.props,
render = _this$props3.render,
children = _this$props3.children;
if ((0, _utils.isFunction)(render)) {
// DEPRECATED. Use `Child Function Pattern` instead
return 'renderProp';
}
if ((0, _utils.isFunction)(children)) {
return 'childFunction';
}
if ( /*#__PURE__*/(0, _react.isValidElement)(children)) {
return 'child';
}
if (Array.isArray(children)) {
// DEPRECATED. Wrap children with a single parent
return 'childArray';
} // DEPRECATED. Use `Child Function Pattern` instead
return 'parent';
});
var skipOnMount = props.skipOnMount,
refreshMode = props.refreshMode,
refreshRate = props.refreshRate,
refreshOptions = props.refreshOptions;
_this.state = {
width: undefined,
height: undefined
};
_this.skipOnMount = skipOnMount;
_this.raf = null;
_this.unmounted = false;
_this.targetRef = /*#__PURE__*/(0, _react.createRef)();
var handle = (0, _utils.getHandle)(refreshMode);
_this.resizeHandler = handle ? handle(_this.createResizeHandler, refreshRate, refreshOptions) : _this.createResizeHandler;
_this.resizeObserver = new _resizeObserverPolyfill["default"](_this.resizeHandler);
return _this;
}
_createClass(ResizeDetector, [{
key: "componentDidMount",
value: function componentDidMount() {
var targetRef = this.props.targetRef;
if (targetRef && targetRef.current) {
this.targetRef.current = targetRef.current;
}
this.toggleObserver('observe');
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.toggleObserver('unobserve');
this.rafClean();
this.cancelHandler();
this.unmounted = true;
}
}, {
key: "render",
value: function render() {
var _this$props4 = this.props,
render = _this$props4.render,
children = _this$props4.children,
WrapperTag = _this$props4.nodeType;
var _this$state2 = this.state,
width = _this$state2.width,
height = _this$state2.height;
var childProps = {
width: width,
height: height,
targetRef: this.targetRef
};
var renderType = this.getRenderType();
switch (renderType) {
case 'renderProp':
return render(childProps);
case 'childFunction':
return children(childProps);
case 'child':
return /*#__PURE__*/(0, _react.cloneElement)(children, childProps);
case 'childArray':
return children.map(function (el) {
return !!el && /*#__PURE__*/(0, _react.cloneElement)(el, childProps);
});
default:
return /*#__PURE__*/_react["default"].createElement(WrapperTag, null);
}
}
}]);
return ResizeDetector;
}(_react.PureComponent);
ResizeDetector.propTypes = {
handleWidth: _propTypes.bool,
handleHeight: _propTypes.bool,
skipOnMount: _propTypes.bool,
refreshRate: _propTypes.number,
refreshMode: _propTypes.string,
refreshOptions: (0, _propTypes.shape)({
leading: _propTypes.bool,
trailing: _propTypes.bool
}),
querySelector: _propTypes.string,
targetDomEl: _propTypes.any,
// eslint-disable-line react/forbid-prop-types
targetRef: (0, _propTypes.oneOfType)([_propTypes.func, (0, _propTypes.shape)({
current: (0, _propTypes.instanceOf)(Element)
})]),
onResize: _propTypes.func,
render: _propTypes.func,
children: _propTypes.any,
// eslint-disable-line react/forbid-prop-types
nodeType: _propTypes.node
};
ResizeDetector.defaultProps = {
handleWidth: true,
handleHeight: true,
skipOnMount: false,
refreshRate: 1000,
refreshMode: undefined,
refreshOptions: undefined,
querySelector: null,
targetDomEl: null,
targetRef: null,
onResize: null,
render: undefined,
children: null,
nodeType: 'div'
};
var _default = ResizeDetector;
exports["default"] = _default;