@vx/responsive
Version:
vx responsive svg
142 lines (113 loc) • 4.95 kB
JavaScript
import _pt from "prop-types";
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; }
import debounce from 'lodash/debounce';
import React from 'react';
import ResizeObserver from 'resize-observer-polyfill';
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", debounce(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 ResizeObserver(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.createElement("div", _extends({
style: parentSizeStyles,
ref: this.setTarget,
className: className
}, restProps), children(_extends({}, this.state, {
ref: this.target,
resize: this.resize
})));
};
return ParentSize;
}(React.Component);
_defineProperty(ParentSize, "propTypes", {
className: _pt.string,
debounceTime: _pt.number,
enableDebounceLeadingCall: _pt.bool,
children: _pt.func.isRequired
});
_defineProperty(ParentSize, "defaultProps", {
debounceTime: 300,
enableDebounceLeadingCall: true,
parentSizeStyles: {
width: '100%',
height: '100%'
}
});
export { ParentSize as default };