@vx/responsive
Version:
vx responsive svg
151 lines (117 loc) • 5.35 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.default = void 0;
var _propTypes = _interopRequireDefault(require("prop-types"));
var _debounce = _interopRequireDefault(require("lodash/debounce"));
var _react = _interopRequireDefault(require("react"));
var _resizeObserverPolyfill = _interopRequireDefault(require("resize-observer-polyfill"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
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 ParentSize = /*#__PURE__*/function (_React$Component) {
_inheritsLoose(ParentSize, _React$Component);
function ParentSize() {
var _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
_defineProperty(_assertThisInitialized(_this), "animationFrameID", 0);
_defineProperty(_assertThisInitialized(_this), "resizeObserver", void 0);
_defineProperty(_assertThisInitialized(_this), "target", null);
_defineProperty(_assertThisInitialized(_this), "state", {
width: 0,
height: 0,
top: 0,
left: 0
});
_defineProperty(_assertThisInitialized(_this), "resize", (0, _debounce.default)(function (_ref) {
var width = _ref.width,
height = _ref.height,
top = _ref.top,
left = _ref.left;
_this.setState(function () {
return {
width: width,
height: height,
top: top,
left: left
};
});
}, _this.props.debounceTime, {
leading: _this.props.enableDebounceLeadingCall
}));
_defineProperty(_assertThisInitialized(_this), "setTarget", function (ref) {
_this.target = ref;
});
return _this;
}
var _proto = ParentSize.prototype;
_proto.componentDidMount = function componentDidMount() {
var _this2 = this;
this.resizeObserver = new _resizeObserverPolyfill.default(function (entries
/** , observer */
) {
if (entries === void 0) {
entries = [];
}
entries.forEach(function (entry) {
var _entry$contentRect = entry.contentRect,
left = _entry$contentRect.left,
top = _entry$contentRect.top,
width = _entry$contentRect.width,
height = _entry$contentRect.height;
_this2.animationFrameID = window.requestAnimationFrame(function () {
_this2.resize({
width: width,
height: height,
top: top,
left: left
});
});
});
});
if (this.target) this.resizeObserver.observe(this.target);
};
_proto.componentWillUnmount = function componentWillUnmount() {
window.cancelAnimationFrame(this.animationFrameID);
if (this.resizeObserver) this.resizeObserver.disconnect();
this.resize.cancel();
};
_proto.render = function render() {
var _this$props = this.props,
className = _this$props.className,
children = _this$props.children,
debounceTime = _this$props.debounceTime,
parentSizeStyles = _this$props.parentSizeStyles,
enableDebounceLeadingCall = _this$props.enableDebounceLeadingCall,
restProps = _objectWithoutPropertiesLoose(_this$props, ["className", "children", "debounceTime", "parentSizeStyles", "enableDebounceLeadingCall"]);
return /*#__PURE__*/_react.default.createElement("div", _extends({
style: parentSizeStyles,
ref: this.setTarget,
className: className
}, restProps), children(_extends({}, this.state, {
ref: this.target,
resize: this.resize
})));
};
return ParentSize;
}(_react.default.Component);
exports.default = ParentSize;
_defineProperty(ParentSize, "propTypes", {
className: _propTypes.default.string,
debounceTime: _propTypes.default.number,
enableDebounceLeadingCall: _propTypes.default.bool,
children: _propTypes.default.func.isRequired
});
_defineProperty(ParentSize, "defaultProps", {
debounceTime: 300,
enableDebounceLeadingCall: true,
parentSizeStyles: {
width: '100%',
height: '100%'
}
});