skedify-calendar
Version:
Skedify Calendar React component
117 lines (83 loc) • 4.11 kB
JavaScript
;
exports.__esModule = true;
exports.ComponentMeasurer = undefined;
var _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; };
var _react = require('react');
var _isFunction = require('../../utils/isFunction');
var _createSubscription = require('../../utils/createSubscription');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var ComponentMeasurer = exports.ComponentMeasurer = function (_Component) {
_inherits(ComponentMeasurer, _Component);
function ComponentMeasurer() {
_classCallCheck(this, ComponentMeasurer);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var _this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args)));
_this.handleResize = function () {
if (_this.ref && _this.ref.current) {
var onUpdate = _this.props.onUpdate;
var finish = (0, _isFunction.isFunction)(onUpdate) ? function () {
return onUpdate(_extends({ ref: _this.ref }, _this.state));
} : undefined;
_this.setState(_this.getSize(), finish);
}
};
_this.state = {};
_this.ref = (0, _react.createRef)();
_this.subscriptions = (0, _createSubscription.createSubscription)();
return _this;
}
ComponentMeasurer.prototype.componentDidMount = function componentDidMount() {
var _this2 = this;
var rafId = requestAnimationFrame(this.handleResize);
this.subscriptions.add(function () {
return cancelAnimationFrame(rafId);
});
if (window.ResizeObserver) {
var resizeObserver = new ResizeObserver(this.handleResize);
resizeObserver.observe(this.ref.current);
this.subscriptions.add(function () {
resizeObserver.disconnect(_this2.ref.current);
resizeObserver = null;
});
} else {
window.addEventListener('resize', this.handleResize);
this.subscriptions.add(function () {
window.removeEventListener('resize', _this2.handleResize);
});
}
};
ComponentMeasurer.prototype.componentWillUnmount = function componentWillUnmount() {
this.subscriptions.cancel();
};
ComponentMeasurer.prototype.getSize = function getSize() {
var _this3 = this;
if (!this.ref.current) {
return {};
}
var _ref$current$getBound = this.ref.current.getBoundingClientRect(),
width = _ref$current$getBound.width,
height = _ref$current$getBound.height;
return {
height: height,
width: width,
element: this.ref.current,
rect: function rect() {
var _ref$current$getBound2 = _this3.ref.current.getBoundingClientRect(),
top = _ref$current$getBound2.top,
left = _ref$current$getBound2.left;
return { top: top, left: left };
}
};
};
ComponentMeasurer.prototype.render = function render() {
var render = this.props.render;
return render(_extends({
ref: this.ref
}, this.state));
};
return ComponentMeasurer;
}(_react.Component);