react-measure
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -80,2 +80,15 @@ 'use strict'; | ||
/** | ||
* Returns the global window object associated with provided element. | ||
*/ | ||
function getWindowOf(target) { | ||
// Assume that the element is an instance of Node, which means that it | ||
// has the "ownerDocument" property from which we can retrieve a | ||
// corresponding global object. | ||
var ownerGlobal = target && target.ownerDocument && target.ownerDocument.defaultView; // Return the local window object if it's not possible extract one from | ||
// provided element. | ||
return ownerGlobal || window; | ||
} | ||
function withContentRect(types) { | ||
@@ -111,2 +124,3 @@ return function (WrappedComponent) { | ||
_this._node = null; | ||
_this._window = null; | ||
@@ -120,3 +134,3 @@ _this.measure = function (entries) { | ||
_this._animationFrameID = window.requestAnimationFrame(function () { | ||
_this._animationFrameID = _this._window.requestAnimationFrame(function () { | ||
if (_this._resizeObserver !== null) { | ||
@@ -140,2 +154,3 @@ _this.setState({ | ||
_this._node = node; | ||
_this._window = getWindowOf(_this._node); | ||
@@ -175,2 +190,6 @@ if (_this._resizeObserver !== null && _this._node !== null) { | ||
_proto.componentWillUnmount = function componentWillUnmount() { | ||
if (this._window !== null) { | ||
this._window.cancelAnimationFrame(this._animationFrameID); | ||
} | ||
if (this._resizeObserver !== null) { | ||
@@ -181,4 +200,2 @@ this._resizeObserver.disconnect(); | ||
} | ||
window.cancelAnimationFrame(this._animationFrameID); | ||
}; | ||
@@ -185,0 +202,0 @@ |
@@ -74,2 +74,15 @@ import _extends from '@babel/runtime/helpers/esm/extends'; | ||
/** | ||
* Returns the global window object associated with provided element. | ||
*/ | ||
function getWindowOf(target) { | ||
// Assume that the element is an instance of Node, which means that it | ||
// has the "ownerDocument" property from which we can retrieve a | ||
// corresponding global object. | ||
var ownerGlobal = target && target.ownerDocument && target.ownerDocument.defaultView; // Return the local window object if it's not possible extract one from | ||
// provided element. | ||
return ownerGlobal || window; | ||
} | ||
function withContentRect(types) { | ||
@@ -105,2 +118,3 @@ return function (WrappedComponent) { | ||
_this._node = null; | ||
_this._window = null; | ||
@@ -114,3 +128,3 @@ _this.measure = function (entries) { | ||
_this._animationFrameID = window.requestAnimationFrame(function () { | ||
_this._animationFrameID = _this._window.requestAnimationFrame(function () { | ||
if (_this._resizeObserver !== null) { | ||
@@ -134,2 +148,3 @@ _this.setState({ | ||
_this._node = node; | ||
_this._window = getWindowOf(_this._node); | ||
@@ -169,2 +184,6 @@ if (_this._resizeObserver !== null && _this._node !== null) { | ||
_proto.componentWillUnmount = function componentWillUnmount() { | ||
if (this._window !== null) { | ||
this._window.cancelAnimationFrame(this._animationFrameID); | ||
} | ||
if (this._resizeObserver !== null) { | ||
@@ -175,4 +194,2 @@ this._resizeObserver.disconnect(); | ||
} | ||
window.cancelAnimationFrame(this._animationFrameID); | ||
}; | ||
@@ -179,0 +196,0 @@ |
@@ -59,2 +59,3 @@ (function (global, factory) { | ||
} | ||
/** | ||
@@ -69,34 +70,41 @@ * Returns index in provided array that matches the specified key. | ||
var result = -1; | ||
arr.some(function (entry, index) { | ||
if (entry[0] === key) { | ||
result = index; | ||
return true; | ||
} | ||
return false; | ||
}); | ||
return result; | ||
} | ||
return /** @class */ (function () { | ||
function class_1() { | ||
return (function () { | ||
function anonymous() { | ||
this.__entries__ = []; | ||
} | ||
Object.defineProperty(class_1.prototype, "size", { | ||
/** | ||
* @returns {boolean} | ||
*/ | ||
get: function () { | ||
return this.__entries__.length; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
var prototypeAccessors = { size: { configurable: true } }; | ||
/** | ||
* @returns {boolean} | ||
*/ | ||
prototypeAccessors.size.get = function () { | ||
return this.__entries__.length; | ||
}; | ||
/** | ||
* @param {*} key | ||
* @returns {*} | ||
*/ | ||
class_1.prototype.get = function (key) { | ||
anonymous.prototype.get = function (key) { | ||
var index = getIndex(this.__entries__, key); | ||
var entry = this.__entries__[index]; | ||
return entry && entry[1]; | ||
}; | ||
/** | ||
@@ -107,11 +115,12 @@ * @param {*} key | ||
*/ | ||
class_1.prototype.set = function (key, value) { | ||
anonymous.prototype.set = function (key, value) { | ||
var index = getIndex(this.__entries__, key); | ||
if (~index) { | ||
this.__entries__[index][1] = value; | ||
} | ||
else { | ||
} else { | ||
this.__entries__.push([key, value]); | ||
} | ||
}; | ||
/** | ||
@@ -121,5 +130,6 @@ * @param {*} key | ||
*/ | ||
class_1.prototype.delete = function (key) { | ||
anonymous.prototype.delete = function (key) { | ||
var entries = this.__entries__; | ||
var index = getIndex(entries, key); | ||
if (~index) { | ||
@@ -129,2 +139,3 @@ entries.splice(index, 1); | ||
}; | ||
/** | ||
@@ -134,11 +145,13 @@ * @param {*} key | ||
*/ | ||
class_1.prototype.has = function (key) { | ||
anonymous.prototype.has = function (key) { | ||
return !!~getIndex(this.__entries__, key); | ||
}; | ||
/** | ||
* @returns {void} | ||
*/ | ||
class_1.prototype.clear = function () { | ||
anonymous.prototype.clear = function () { | ||
this.__entries__.splice(0); | ||
}; | ||
/** | ||
@@ -149,10 +162,16 @@ * @param {Function} callback | ||
*/ | ||
class_1.prototype.forEach = function (callback, ctx) { | ||
if (ctx === void 0) { ctx = null; } | ||
for (var _i = 0, _a = this.__entries__; _i < _a.length; _i++) { | ||
var entry = _a[_i]; | ||
anonymous.prototype.forEach = function (callback, ctx) { | ||
var this$1 = this; | ||
if ( ctx === void 0 ) ctx = null; | ||
for (var i = 0, list = this$1.__entries__; i < list.length; i += 1) { | ||
var entry = list[i]; | ||
callback.call(ctx, entry[1], entry[0]); | ||
} | ||
}; | ||
return class_1; | ||
Object.defineProperties( anonymous.prototype, prototypeAccessors ); | ||
return anonymous; | ||
}()); | ||
@@ -171,8 +190,11 @@ })(); | ||
} | ||
if (typeof self !== 'undefined' && self.Math === Math) { | ||
return self; | ||
} | ||
if (typeof window !== 'undefined' && window.Math === Math) { | ||
return window; | ||
} | ||
// eslint-disable-next-line no-new-func | ||
@@ -195,2 +217,3 @@ return Function('return this')(); | ||
} | ||
return function (callback) { return setTimeout(function () { return callback(Date.now()); }, 1000 / 60); }; | ||
@@ -201,2 +224,3 @@ })(); | ||
var trailingTimeout = 2; | ||
/** | ||
@@ -210,4 +234,7 @@ * Creates a wrapper function which ensures that provided callback will be | ||
*/ | ||
function throttle (callback, delay) { | ||
var leadingCall = false, trailingCall = false, lastCallTime = 0; | ||
var throttle = function (callback, delay) { | ||
var leadingCall = false, | ||
trailingCall = false, | ||
lastCallTime = 0; | ||
/** | ||
@@ -222,4 +249,6 @@ * Invokes the original callback function and schedules new invocation if | ||
leadingCall = false; | ||
callback(); | ||
} | ||
if (trailingCall) { | ||
@@ -229,2 +258,3 @@ proxy(); | ||
} | ||
/** | ||
@@ -240,2 +270,3 @@ * Callback invoked after the specified delay. It will further postpone | ||
} | ||
/** | ||
@@ -248,2 +279,3 @@ * Schedules invocation of the original function. | ||
var timeStamp = Date.now(); | ||
if (leadingCall) { | ||
@@ -254,2 +286,3 @@ // Reject immediately following calls. | ||
} | ||
// Schedule new call to be in invoked when the pending one is resolved. | ||
@@ -260,219 +293,239 @@ // This is important for "transitions" which never actually start | ||
trailingCall = true; | ||
} | ||
else { | ||
} else { | ||
leadingCall = true; | ||
trailingCall = false; | ||
setTimeout(timeoutCallback, delay); | ||
} | ||
lastCallTime = timeStamp; | ||
} | ||
return proxy; | ||
} | ||
}; | ||
// Minimum delay before invoking the update of observers. | ||
var REFRESH_DELAY = 20; | ||
// A list of substrings of CSS properties used to find transition events that | ||
// might affect dimensions of observed elements. | ||
var transitionKeys = ['top', 'right', 'bottom', 'left', 'width', 'height', 'size', 'weight']; | ||
// Check if MutationObserver is available. | ||
var mutationObserverSupported = typeof MutationObserver !== 'undefined'; | ||
/** | ||
* Singleton controller class which handles updates of ResizeObserver instances. | ||
*/ | ||
var ResizeObserverController = /** @class */ (function () { | ||
/** | ||
* Creates a new instance of ResizeObserverController. | ||
* | ||
* @private | ||
*/ | ||
function ResizeObserverController() { | ||
/** | ||
* Indicates whether DOM listeners have been added. | ||
* | ||
* @private {boolean} | ||
*/ | ||
this.connected_ = false; | ||
/** | ||
* Tells that controller has subscribed for Mutation Events. | ||
* | ||
* @private {boolean} | ||
*/ | ||
this.mutationEventsAdded_ = false; | ||
/** | ||
* Keeps reference to the instance of MutationObserver. | ||
* | ||
* @private {MutationObserver} | ||
*/ | ||
this.mutationsObserver_ = null; | ||
/** | ||
* A list of connected observers. | ||
* | ||
* @private {Array<ResizeObserverSPI>} | ||
*/ | ||
this.observers_ = []; | ||
this.onTransitionEnd_ = this.onTransitionEnd_.bind(this); | ||
this.refresh = throttle(this.refresh.bind(this), REFRESH_DELAY); | ||
var ResizeObserverController = function() { | ||
this.connected_ = false; | ||
this.mutationEventsAdded_ = false; | ||
this.mutationsObserver_ = null; | ||
this.observers_ = []; | ||
this.onTransitionEnd_ = this.onTransitionEnd_.bind(this); | ||
this.refresh = throttle(this.refresh.bind(this), REFRESH_DELAY); | ||
}; | ||
/** | ||
* Adds observer to observers list. | ||
* | ||
* @param {ResizeObserverSPI} observer - Observer to be added. | ||
* @returns {void} | ||
*/ | ||
/** | ||
* Holds reference to the controller's instance. | ||
* | ||
* @private {ResizeObserverController} | ||
*/ | ||
/** | ||
* Keeps reference to the instance of MutationObserver. | ||
* | ||
* @private {MutationObserver} | ||
*/ | ||
/** | ||
* Indicates whether DOM listeners have been added. | ||
* | ||
* @private {boolean} | ||
*/ | ||
ResizeObserverController.prototype.addObserver = function (observer) { | ||
if (!~this.observers_.indexOf(observer)) { | ||
this.observers_.push(observer); | ||
} | ||
/** | ||
* Adds observer to observers list. | ||
* | ||
* @param {ResizeObserverSPI} observer - Observer to be added. | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.addObserver = function (observer) { | ||
if (!~this.observers_.indexOf(observer)) { | ||
this.observers_.push(observer); | ||
} | ||
// Add listeners if they haven't been added yet. | ||
if (!this.connected_) { | ||
this.connect_(); | ||
} | ||
}; | ||
/** | ||
* Removes observer from observers list. | ||
* | ||
* @param {ResizeObserverSPI} observer - Observer to be removed. | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.removeObserver = function (observer) { | ||
var observers = this.observers_; | ||
var index = observers.indexOf(observer); | ||
// Remove observer if it's present in registry. | ||
if (~index) { | ||
observers.splice(index, 1); | ||
} | ||
// Remove listeners if controller has no connected observers. | ||
if (!observers.length && this.connected_) { | ||
this.disconnect_(); | ||
} | ||
}; | ||
/** | ||
* Invokes the update of observers. It will continue running updates insofar | ||
* it detects changes. | ||
* | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.refresh = function () { | ||
var changesDetected = this.updateObservers_(); | ||
// Continue running updates if changes have been detected as there might | ||
// be future ones caused by CSS transitions. | ||
if (changesDetected) { | ||
this.refresh(); | ||
} | ||
}; | ||
/** | ||
* Updates every observer from observers list and notifies them of queued | ||
* entries. | ||
* | ||
* @private | ||
* @returns {boolean} Returns "true" if any observer has detected changes in | ||
* dimensions of it's elements. | ||
*/ | ||
ResizeObserverController.prototype.updateObservers_ = function () { | ||
// Collect observers that have active observations. | ||
var activeObservers = this.observers_.filter(function (observer) { | ||
return observer.gatherActive(), observer.hasActive(); | ||
// Add listeners if they haven't been added yet. | ||
if (!this.connected_) { | ||
this.connect_(); | ||
} | ||
}; | ||
/** | ||
* Removes observer from observers list. | ||
* | ||
* @param {ResizeObserverSPI} observer - Observer to be removed. | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.removeObserver = function (observer) { | ||
var observers = this.observers_; | ||
var index = observers.indexOf(observer); | ||
// Remove observer if it's present in registry. | ||
if (~index) { | ||
observers.splice(index, 1); | ||
} | ||
// Remove listeners if controller has no connected observers. | ||
if (!observers.length && this.connected_) { | ||
this.disconnect_(); | ||
} | ||
}; | ||
/** | ||
* Invokes the update of observers. It will continue running updates insofar | ||
* it detects changes. | ||
* | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.refresh = function () { | ||
var changesDetected = this.updateObservers_(); | ||
// Continue running updates if changes have been detected as there might | ||
// be future ones caused by CSS transitions. | ||
if (changesDetected) { | ||
this.refresh(); | ||
} | ||
}; | ||
/** | ||
* Updates every observer from observers list and notifies them of queued | ||
* entries. | ||
* | ||
* @private | ||
* @returns {boolean} Returns "true" if any observer has detected changes in | ||
* dimensions of it's elements. | ||
*/ | ||
ResizeObserverController.prototype.updateObservers_ = function () { | ||
// Collect observers that have active observations. | ||
var activeObservers = this.observers_.filter(function (observer) { | ||
return observer.gatherActive(), observer.hasActive(); | ||
}); | ||
// Deliver notifications in a separate cycle in order to avoid any | ||
// collisions between observers, e.g. when multiple instances of | ||
// ResizeObserver are tracking the same element and the callback of one | ||
// of them changes content dimensions of the observed target. Sometimes | ||
// this may result in notifications being blocked for the rest of observers. | ||
activeObservers.forEach(function (observer) { return observer.broadcastActive(); }); | ||
return activeObservers.length > 0; | ||
}; | ||
/** | ||
* Initializes DOM listeners. | ||
* | ||
* @private | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.connect_ = function () { | ||
// Do nothing if running in a non-browser environment or if listeners | ||
// have been already added. | ||
if (!isBrowser || this.connected_) { | ||
return; | ||
} | ||
// Subscription to the "Transitionend" event is used as a workaround for | ||
// delayed transitions. This way it's possible to capture at least the | ||
// final state of an element. | ||
document.addEventListener('transitionend', this.onTransitionEnd_); | ||
window.addEventListener('resize', this.refresh); | ||
if (mutationObserverSupported) { | ||
this.mutationsObserver_ = new MutationObserver(this.refresh); | ||
this.mutationsObserver_.observe(document, { | ||
attributes: true, | ||
childList: true, | ||
characterData: true, | ||
subtree: true | ||
}); | ||
// Deliver notifications in a separate cycle in order to avoid any | ||
// collisions between observers, e.g. when multiple instances of | ||
// ResizeObserver are tracking the same element and the callback of one | ||
// of them changes content dimensions of the observed target. Sometimes | ||
// this may result in notifications being blocked for the rest of observers. | ||
activeObservers.forEach(function (observer) { return observer.broadcastActive(); }); | ||
return activeObservers.length > 0; | ||
}; | ||
/** | ||
* Initializes DOM listeners. | ||
* | ||
* @private | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.connect_ = function () { | ||
// Do nothing if running in a non-browser environment or if listeners | ||
// have been already added. | ||
if (!isBrowser || this.connected_) { | ||
return; | ||
} | ||
// Subscription to the "Transitionend" event is used as a workaround for | ||
// delayed transitions. This way it's possible to capture at least the | ||
// final state of an element. | ||
document.addEventListener('transitionend', this.onTransitionEnd_); | ||
window.addEventListener('resize', this.refresh); | ||
if (mutationObserverSupported) { | ||
this.mutationsObserver_ = new MutationObserver(this.refresh); | ||
this.mutationsObserver_.observe(document, { | ||
attributes: true, | ||
childList: true, | ||
characterData: true, | ||
subtree: true | ||
}); | ||
} | ||
else { | ||
document.addEventListener('DOMSubtreeModified', this.refresh); | ||
this.mutationEventsAdded_ = true; | ||
} | ||
this.connected_ = true; | ||
}; | ||
/** | ||
* Removes DOM listeners. | ||
* | ||
* @private | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.disconnect_ = function () { | ||
// Do nothing if running in a non-browser environment or if listeners | ||
// have been already removed. | ||
if (!isBrowser || !this.connected_) { | ||
return; | ||
} | ||
document.removeEventListener('transitionend', this.onTransitionEnd_); | ||
window.removeEventListener('resize', this.refresh); | ||
if (this.mutationsObserver_) { | ||
this.mutationsObserver_.disconnect(); | ||
} | ||
if (this.mutationEventsAdded_) { | ||
document.removeEventListener('DOMSubtreeModified', this.refresh); | ||
} | ||
this.mutationsObserver_ = null; | ||
this.mutationEventsAdded_ = false; | ||
this.connected_ = false; | ||
}; | ||
/** | ||
* "Transitionend" event handler. | ||
* | ||
* @private | ||
* @param {TransitionEvent} event | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.onTransitionEnd_ = function (_a) { | ||
var _b = _a.propertyName, propertyName = _b === void 0 ? '' : _b; | ||
// Detect whether transition may affect dimensions of an element. | ||
var isReflowProperty = transitionKeys.some(function (key) { | ||
return !!~propertyName.indexOf(key); | ||
}); | ||
if (isReflowProperty) { | ||
this.refresh(); | ||
} | ||
}; | ||
/** | ||
* Returns instance of the ResizeObserverController. | ||
* | ||
* @returns {ResizeObserverController} | ||
*/ | ||
ResizeObserverController.getInstance = function () { | ||
if (!this.instance_) { | ||
this.instance_ = new ResizeObserverController(); | ||
} | ||
return this.instance_; | ||
}; | ||
/** | ||
* Holds reference to the controller's instance. | ||
* | ||
* @private {ResizeObserverController} | ||
*/ | ||
ResizeObserverController.instance_ = null; | ||
return ResizeObserverController; | ||
}()); | ||
} else { | ||
document.addEventListener('DOMSubtreeModified', this.refresh); | ||
this.mutationEventsAdded_ = true; | ||
} | ||
this.connected_ = true; | ||
}; | ||
/** | ||
* Removes DOM listeners. | ||
* | ||
* @private | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.disconnect_ = function () { | ||
// Do nothing if running in a non-browser environment or if listeners | ||
// have been already removed. | ||
if (!isBrowser || !this.connected_) { | ||
return; | ||
} | ||
document.removeEventListener('transitionend', this.onTransitionEnd_); | ||
window.removeEventListener('resize', this.refresh); | ||
if (this.mutationsObserver_) { | ||
this.mutationsObserver_.disconnect(); | ||
} | ||
if (this.mutationEventsAdded_) { | ||
document.removeEventListener('DOMSubtreeModified', this.refresh); | ||
} | ||
this.mutationsObserver_ = null; | ||
this.mutationEventsAdded_ = false; | ||
this.connected_ = false; | ||
}; | ||
/** | ||
* "Transitionend" event handler. | ||
* | ||
* @private | ||
* @param {TransitionEvent} event | ||
* @returns {void} | ||
*/ | ||
ResizeObserverController.prototype.onTransitionEnd_ = function (ref) { | ||
var propertyName = ref.propertyName; if ( propertyName === void 0 ) propertyName = ''; | ||
// Detect whether transition may affect dimensions of an element. | ||
var isReflowProperty = transitionKeys.some(function (key) { | ||
return !!~propertyName.indexOf(key); | ||
}); | ||
if (isReflowProperty) { | ||
this.refresh(); | ||
} | ||
}; | ||
/** | ||
* Returns instance of the ResizeObserverController. | ||
* | ||
* @returns {ResizeObserverController} | ||
*/ | ||
ResizeObserverController.getInstance = function () { | ||
if (!this.instance_) { | ||
this.instance_ = new ResizeObserverController(); | ||
} | ||
return this.instance_; | ||
}; | ||
ResizeObserverController.instance_ = null; | ||
/** | ||
* Defines non-writable/enumerable properties of the provided target object. | ||
@@ -485,4 +538,5 @@ * | ||
var defineConfigurable = (function (target, props) { | ||
for (var _i = 0, _a = Object.keys(props); _i < _a.length; _i++) { | ||
var key = _a[_i]; | ||
for (var i = 0, list = Object.keys(props); i < list.length; i += 1) { | ||
var key = list[i]; | ||
Object.defineProperty(target, key, { | ||
@@ -495,2 +549,3 @@ value: props[key], | ||
} | ||
return target; | ||
@@ -510,2 +565,3 @@ }); | ||
var ownerGlobal = target && target.ownerDocument && target.ownerDocument.defaultView; | ||
// Return the local global object if it's not possible extract one from | ||
@@ -518,2 +574,3 @@ // provided element. | ||
var emptyRect = createRectInit(0, 0, 0, 0); | ||
/** | ||
@@ -528,2 +585,3 @@ * Converts provided string to a number. | ||
} | ||
/** | ||
@@ -537,11 +595,12 @@ * Extracts borders size from provided styles. | ||
function getBordersSize(styles) { | ||
var positions = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
positions[_i - 1] = arguments[_i]; | ||
} | ||
var positions = [], len = arguments.length - 1; | ||
while ( len-- > 0 ) positions[ len ] = arguments[ len + 1 ]; | ||
return positions.reduce(function (size, position) { | ||
var value = styles['border-' + position + '-width']; | ||
return size + toFloat(value); | ||
}, 0); | ||
} | ||
/** | ||
@@ -556,9 +615,14 @@ * Extracts paddings sizes from provided styles. | ||
var paddings = {}; | ||
for (var _i = 0, positions_1 = positions; _i < positions_1.length; _i++) { | ||
var position = positions_1[_i]; | ||
for (var i = 0, list = positions; i < list.length; i += 1) { | ||
var position = list[i]; | ||
var value = styles['padding-' + position]; | ||
paddings[position] = toFloat(value); | ||
} | ||
return paddings; | ||
} | ||
/** | ||
@@ -573,4 +637,6 @@ * Calculates content rectangle of provided SVG element. | ||
var bbox = target.getBBox(); | ||
return createRectInit(0, 0, bbox.width, bbox.height); | ||
} | ||
/** | ||
@@ -585,3 +651,5 @@ * Calculates content rectangle of provided HTMLElement. | ||
// used exclusively as they provide rounded values. | ||
var clientWidth = target.clientWidth, clientHeight = target.clientHeight; | ||
var clientWidth = target.clientWidth; | ||
var clientHeight = target.clientHeight; | ||
// By this condition we can catch all non-replaced inline, hidden and | ||
@@ -598,2 +666,3 @@ // detached elements. Though elements with width & height properties less | ||
} | ||
var styles = getWindowOf(target).getComputedStyle(target); | ||
@@ -603,2 +672,3 @@ var paddings = getPaddings(styles); | ||
var vertPad = paddings.top + paddings.bottom; | ||
// Computed styles of width & height are being used because they are the | ||
@@ -608,3 +678,5 @@ // only dimensions available to JS that contain non-rounded values. It could | ||
// affected by CSS transformations let alone paddings, borders and scroll bars. | ||
var width = toFloat(styles.width), height = toFloat(styles.height); | ||
var width = toFloat(styles.width), | ||
height = toFloat(styles.height); | ||
// Width & height include paddings and borders when the 'border-box' box | ||
@@ -622,2 +694,3 @@ // model is applied (except for IE). | ||
} | ||
if (Math.round(height + vertPad) !== clientHeight) { | ||
@@ -627,2 +700,3 @@ height -= getBordersSize(styles, 'top', 'bottom') + vertPad; | ||
} | ||
// Following steps can't be applied to the document's root element as its | ||
@@ -639,2 +713,3 @@ // client[Width/Height] properties represent viewport area of the window. | ||
var horizScrollbar = Math.round(height + vertPad) - clientHeight; | ||
// Chrome has a rather weird rounding of "client" properties. | ||
@@ -648,2 +723,3 @@ // E.g. for an element with content width of 314.2px it sometimes gives | ||
} | ||
if (Math.abs(horizScrollbar) !== 1) { | ||
@@ -653,4 +729,6 @@ height -= horizScrollbar; | ||
} | ||
return createRectInit(paddings.left, paddings.top, width, height); | ||
} | ||
/** | ||
@@ -668,8 +746,9 @@ * Checks whether provided element is an instance of the SVGGraphicsElement. | ||
} | ||
// If it's so, then check that element is at least an instance of the | ||
// SVGElement and that it has the "getBBox" method. | ||
// eslint-disable-next-line no-extra-parens | ||
return function (target) { return (target instanceof getWindowOf(target).SVGElement && | ||
typeof target.getBBox === 'function'); }; | ||
return function (target) { return target instanceof getWindowOf(target).SVGElement && typeof target.getBBox === 'function'; }; | ||
})(); | ||
/** | ||
@@ -684,2 +763,3 @@ * Checks whether provided element is a document element (<html>). | ||
} | ||
/** | ||
@@ -695,7 +775,10 @@ * Calculates an appropriate content rectangle for provided html or svg element. | ||
} | ||
if (isSVGGraphicsElement(target)) { | ||
return getSVGContentRect(target); | ||
} | ||
return getHTMLElementContentRect(target); | ||
} | ||
/** | ||
@@ -708,7 +791,12 @@ * Creates rectangle with an interface of the DOMRectReadOnly. | ||
*/ | ||
function createReadOnlyRect(_a) { | ||
var x = _a.x, y = _a.y, width = _a.width, height = _a.height; | ||
function createReadOnlyRect(ref) { | ||
var x = ref.x; | ||
var y = ref.y; | ||
var width = ref.width; | ||
var height = ref.height; | ||
// If DOMRectReadOnly is available use it as a prototype for the rectangle. | ||
var Constr = typeof DOMRectReadOnly !== 'undefined' ? DOMRectReadOnly : Object; | ||
var rect = Object.create(Constr.prototype); | ||
// Rectangle's properties are not writable and non-enumerable. | ||
@@ -722,4 +810,6 @@ defineConfigurable(rect, { | ||
}); | ||
return rect; | ||
} | ||
/** | ||
@@ -743,225 +833,239 @@ * Creates DOMRectInit object based on the provided dimensions and the x/y coordinates. | ||
*/ | ||
var ResizeObservation = /** @class */ (function () { | ||
/** | ||
* Creates an instance of ResizeObservation. | ||
* | ||
* @param {Element} target - Element to be observed. | ||
*/ | ||
function ResizeObservation(target) { | ||
/** | ||
* Broadcasted width of content rectangle. | ||
* | ||
* @type {number} | ||
*/ | ||
this.broadcastWidth = 0; | ||
/** | ||
* Broadcasted height of content rectangle. | ||
* | ||
* @type {number} | ||
*/ | ||
this.broadcastHeight = 0; | ||
/** | ||
* Reference to the last observed content rectangle. | ||
* | ||
* @private {DOMRectInit} | ||
*/ | ||
this.contentRect_ = createRectInit(0, 0, 0, 0); | ||
this.target = target; | ||
var ResizeObservation = function(target) { | ||
this.broadcastWidth = 0; | ||
this.broadcastHeight = 0; | ||
this.contentRect_ = createRectInit(0, 0, 0, 0); | ||
this.target = target; | ||
}; | ||
/** | ||
* Updates content rectangle and tells whether it's width or height properties | ||
* have changed since the last broadcast. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
/** | ||
* Reference to the last observed content rectangle. | ||
* | ||
* @private {DOMRectInit} | ||
*/ | ||
/** | ||
* Broadcasted width of content rectangle. | ||
* | ||
* @type {number} | ||
*/ | ||
ResizeObservation.prototype.isActive = function () { | ||
var rect = getContentRect(this.target); | ||
this.contentRect_ = rect; | ||
return rect.width !== this.broadcastWidth || rect.height !== this.broadcastHeight; | ||
}; | ||
/** | ||
* Updates 'broadcastWidth' and 'broadcastHeight' properties with a data | ||
* from the corresponding properties of the last observed content rectangle. | ||
* | ||
* @returns {DOMRectInit} Last observed content rectangle. | ||
*/ | ||
ResizeObservation.prototype.broadcastRect = function () { | ||
var rect = this.contentRect_; | ||
this.broadcastWidth = rect.width; | ||
this.broadcastHeight = rect.height; | ||
return rect; | ||
}; | ||
var ResizeObserverEntry = function(target, rectInit) { | ||
var contentRect = createReadOnlyRect(rectInit); | ||
// According to the specification following properties are not writable | ||
// and are also not enumerable in the native implementation. | ||
// | ||
// Property accessors are not being used as they'd require to define a | ||
// private WeakMap storage which may cause memory leaks in browsers that | ||
// don't support this type of collections. | ||
defineConfigurable(this, { target: target, contentRect: contentRect }); | ||
}; | ||
var ResizeObserverSPI = function(callback, controller, callbackCtx) { | ||
this.activeObservations_ = []; | ||
this.observations_ = new MapShim(); | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('The callback provided as parameter 1 is not a function.'); | ||
} | ||
/** | ||
* Updates content rectangle and tells whether it's width or height properties | ||
* have changed since the last broadcast. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
ResizeObservation.prototype.isActive = function () { | ||
var rect = getContentRect(this.target); | ||
this.contentRect_ = rect; | ||
return (rect.width !== this.broadcastWidth || | ||
rect.height !== this.broadcastHeight); | ||
}; | ||
/** | ||
* Updates 'broadcastWidth' and 'broadcastHeight' properties with a data | ||
* from the corresponding properties of the last observed content rectangle. | ||
* | ||
* @returns {DOMRectInit} Last observed content rectangle. | ||
*/ | ||
ResizeObservation.prototype.broadcastRect = function () { | ||
var rect = this.contentRect_; | ||
this.broadcastWidth = rect.width; | ||
this.broadcastHeight = rect.height; | ||
return rect; | ||
}; | ||
return ResizeObservation; | ||
}()); | ||
var ResizeObserverEntry = /** @class */ (function () { | ||
/** | ||
* Creates an instance of ResizeObserverEntry. | ||
* | ||
* @param {Element} target - Element that is being observed. | ||
* @param {DOMRectInit} rectInit - Data of the element's content rectangle. | ||
*/ | ||
function ResizeObserverEntry(target, rectInit) { | ||
var contentRect = createReadOnlyRect(rectInit); | ||
// According to the specification following properties are not writable | ||
// and are also not enumerable in the native implementation. | ||
// | ||
// Property accessors are not being used as they'd require to define a | ||
// private WeakMap storage which may cause memory leaks in browsers that | ||
// don't support this type of collections. | ||
defineConfigurable(this, { target: target, contentRect: contentRect }); | ||
this.callback_ = callback; | ||
this.controller_ = controller; | ||
this.callbackCtx_ = callbackCtx; | ||
}; | ||
/** | ||
* Starts observing provided element. | ||
* | ||
* @param {Element} target - Element to be observed. | ||
* @returns {void} | ||
*/ | ||
/** | ||
* Registry of the ResizeObservation instances. | ||
* | ||
* @private {Map<Element, ResizeObservation>} | ||
*/ | ||
/** | ||
* Public ResizeObserver instance which will be passed to the callback | ||
* function and used as a value of it's "this" binding. | ||
* | ||
* @private {ResizeObserver} | ||
*/ | ||
/** | ||
* Collection of resize observations that have detected changes in dimensions | ||
* of elements. | ||
* | ||
* @private {Array<ResizeObservation>} | ||
*/ | ||
ResizeObserverSPI.prototype.observe = function (target) { | ||
if (!arguments.length) { | ||
throw new TypeError('1 argument required, but only 0 present.'); | ||
} | ||
return ResizeObserverEntry; | ||
}()); | ||
var ResizeObserverSPI = /** @class */ (function () { | ||
/** | ||
* Creates a new instance of ResizeObserver. | ||
* | ||
* @param {ResizeObserverCallback} callback - Callback function that is invoked | ||
* when one of the observed elements changes it's content dimensions. | ||
* @param {ResizeObserverController} controller - Controller instance which | ||
* is responsible for the updates of observer. | ||
* @param {ResizeObserver} callbackCtx - Reference to the public | ||
* ResizeObserver instance which will be passed to callback function. | ||
*/ | ||
function ResizeObserverSPI(callback, controller, callbackCtx) { | ||
/** | ||
* Collection of resize observations that have detected changes in dimensions | ||
* of elements. | ||
* | ||
* @private {Array<ResizeObservation>} | ||
*/ | ||
this.activeObservations_ = []; | ||
/** | ||
* Registry of the ResizeObservation instances. | ||
* | ||
* @private {Map<Element, ResizeObservation>} | ||
*/ | ||
this.observations_ = new MapShim(); | ||
if (typeof callback !== 'function') { | ||
throw new TypeError('The callback provided as parameter 1 is not a function.'); | ||
} | ||
this.callback_ = callback; | ||
this.controller_ = controller; | ||
this.callbackCtx_ = callbackCtx; | ||
// Do nothing if current environment doesn't have the Element interface. | ||
if (typeof Element === 'undefined' || !(Element instanceof Object)) { | ||
return; | ||
} | ||
/** | ||
* Starts observing provided element. | ||
* | ||
* @param {Element} target - Element to be observed. | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.observe = function (target) { | ||
if (!arguments.length) { | ||
throw new TypeError('1 argument required, but only 0 present.'); | ||
} | ||
// Do nothing if current environment doesn't have the Element interface. | ||
if (typeof Element === 'undefined' || !(Element instanceof Object)) { | ||
return; | ||
} | ||
if (!(target instanceof getWindowOf(target).Element)) { | ||
throw new TypeError('parameter 1 is not of type "Element".'); | ||
} | ||
var observations = this.observations_; | ||
// Do nothing if element is already being observed. | ||
if (observations.has(target)) { | ||
return; | ||
} | ||
observations.set(target, new ResizeObservation(target)); | ||
this.controller_.addObserver(this); | ||
// Force the update of observations. | ||
this.controller_.refresh(); | ||
}; | ||
/** | ||
* Stops observing provided element. | ||
* | ||
* @param {Element} target - Element to stop observing. | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.unobserve = function (target) { | ||
if (!arguments.length) { | ||
throw new TypeError('1 argument required, but only 0 present.'); | ||
} | ||
// Do nothing if current environment doesn't have the Element interface. | ||
if (typeof Element === 'undefined' || !(Element instanceof Object)) { | ||
return; | ||
} | ||
if (!(target instanceof getWindowOf(target).Element)) { | ||
throw new TypeError('parameter 1 is not of type "Element".'); | ||
} | ||
var observations = this.observations_; | ||
// Do nothing if element is not being observed. | ||
if (!observations.has(target)) { | ||
return; | ||
} | ||
observations.delete(target); | ||
if (!observations.size) { | ||
this.controller_.removeObserver(this); | ||
} | ||
}; | ||
/** | ||
* Stops observing all elements. | ||
* | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.disconnect = function () { | ||
this.clearActive(); | ||
this.observations_.clear(); | ||
if (!(target instanceof getWindowOf(target).Element)) { | ||
throw new TypeError('parameter 1 is not of type "Element".'); | ||
} | ||
var observations = this.observations_; | ||
// Do nothing if element is already being observed. | ||
if (observations.has(target)) { | ||
return; | ||
} | ||
observations.set(target, new ResizeObservation(target)); | ||
this.controller_.addObserver(this); | ||
// Force the update of observations. | ||
this.controller_.refresh(); | ||
}; | ||
/** | ||
* Stops observing provided element. | ||
* | ||
* @param {Element} target - Element to stop observing. | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.unobserve = function (target) { | ||
if (!arguments.length) { | ||
throw new TypeError('1 argument required, but only 0 present.'); | ||
} | ||
// Do nothing if current environment doesn't have the Element interface. | ||
if (typeof Element === 'undefined' || !(Element instanceof Object)) { | ||
return; | ||
} | ||
if (!(target instanceof getWindowOf(target).Element)) { | ||
throw new TypeError('parameter 1 is not of type "Element".'); | ||
} | ||
var observations = this.observations_; | ||
// Do nothing if element is not being observed. | ||
if (!observations.has(target)) { | ||
return; | ||
} | ||
observations.delete(target); | ||
if (!observations.size) { | ||
this.controller_.removeObserver(this); | ||
}; | ||
/** | ||
* Collects observation instances the associated element of which has changed | ||
* it's content rectangle. | ||
* | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.gatherActive = function () { | ||
var _this = this; | ||
this.clearActive(); | ||
this.observations_.forEach(function (observation) { | ||
if (observation.isActive()) { | ||
_this.activeObservations_.push(observation); | ||
} | ||
}); | ||
}; | ||
/** | ||
* Invokes initial callback function with a list of ResizeObserverEntry | ||
* instances collected from active resize observations. | ||
* | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.broadcastActive = function () { | ||
// Do nothing if observer doesn't have active observations. | ||
if (!this.hasActive()) { | ||
return; | ||
} | ||
}; | ||
/** | ||
* Stops observing all elements. | ||
* | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.disconnect = function () { | ||
this.clearActive(); | ||
this.observations_.clear(); | ||
this.controller_.removeObserver(this); | ||
}; | ||
/** | ||
* Collects observation instances the associated element of which has changed | ||
* it's content rectangle. | ||
* | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.gatherActive = function () { | ||
var this$1 = this; | ||
this.clearActive(); | ||
this.observations_.forEach(function (observation) { | ||
if (observation.isActive()) { | ||
this$1.activeObservations_.push(observation); | ||
} | ||
var ctx = this.callbackCtx_; | ||
// Create ResizeObserverEntry instance for every active observation. | ||
var entries = this.activeObservations_.map(function (observation) { | ||
return new ResizeObserverEntry(observation.target, observation.broadcastRect()); | ||
}); | ||
this.callback_.call(ctx, entries, ctx); | ||
this.clearActive(); | ||
}; | ||
/** | ||
* Clears the collection of active observations. | ||
* | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.clearActive = function () { | ||
this.activeObservations_.splice(0); | ||
}; | ||
/** | ||
* Tells whether observer has active observations. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
ResizeObserverSPI.prototype.hasActive = function () { | ||
return this.activeObservations_.length > 0; | ||
}; | ||
return ResizeObserverSPI; | ||
}()); | ||
}); | ||
}; | ||
/** | ||
* Invokes initial callback function with a list of ResizeObserverEntry | ||
* instances collected from active resize observations. | ||
* | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.broadcastActive = function () { | ||
// Do nothing if observer doesn't have active observations. | ||
if (!this.hasActive()) { | ||
return; | ||
} | ||
var ctx = this.callbackCtx_; | ||
// Create ResizeObserverEntry instance for every active observation. | ||
var entries = this.activeObservations_.map(function (observation) { | ||
return new ResizeObserverEntry(observation.target, observation.broadcastRect()); | ||
}); | ||
this.callback_.call(ctx, entries, ctx); | ||
this.clearActive(); | ||
}; | ||
/** | ||
* Clears the collection of active observations. | ||
* | ||
* @returns {void} | ||
*/ | ||
ResizeObserverSPI.prototype.clearActive = function () { | ||
this.activeObservations_.splice(0); | ||
}; | ||
/** | ||
* Tells whether observer has active observations. | ||
* | ||
* @returns {boolean} | ||
*/ | ||
ResizeObserverSPI.prototype.hasActive = function () { | ||
return this.activeObservations_.length > 0; | ||
}; | ||
// Registry of internal observers. If WeakMap is not available use current shim | ||
@@ -971,2 +1075,3 @@ // for the Map collection as it has all required methods and because WeakMap | ||
var observers = typeof WeakMap !== 'undefined' ? new WeakMap() : new MapShim(); | ||
/** | ||
@@ -976,31 +1081,21 @@ * ResizeObserver API. Encapsulates the ResizeObserver SPI implementation | ||
*/ | ||
var ResizeObserver = /** @class */ (function () { | ||
/** | ||
* Creates a new instance of ResizeObserver. | ||
* | ||
* @param {ResizeObserverCallback} callback - Callback that is invoked when | ||
* dimensions of the observed elements change. | ||
*/ | ||
function ResizeObserver(callback) { | ||
if (!(this instanceof ResizeObserver)) { | ||
throw new TypeError('Cannot call a class as a function.'); | ||
} | ||
if (!arguments.length) { | ||
throw new TypeError('1 argument required, but only 0 present.'); | ||
} | ||
var controller = ResizeObserverController.getInstance(); | ||
var observer = new ResizeObserverSPI(callback, controller, this); | ||
observers.set(this, observer); | ||
var ResizeObserver = function(callback) { | ||
if (!(this instanceof ResizeObserver)) { | ||
throw new TypeError('Cannot call a class as a function.'); | ||
} | ||
return ResizeObserver; | ||
}()); | ||
if (!arguments.length) { | ||
throw new TypeError('1 argument required, but only 0 present.'); | ||
} | ||
var controller = ResizeObserverController.getInstance(); | ||
var observer = new ResizeObserverSPI(callback, controller, this); | ||
observers.set(this, observer); | ||
}; | ||
// Expose public methods of ResizeObserver. | ||
[ | ||
'observe', | ||
'unobserve', | ||
'disconnect' | ||
].forEach(function (method) { | ||
['observe', 'unobserve', 'disconnect'].forEach(function (method) { | ||
ResizeObserver.prototype[method] = function () { | ||
var _a; | ||
return (_a = observers.get(this))[method].apply(_a, arguments); | ||
return (ref = observers.get(this))[method].apply(ref, arguments); | ||
var ref; | ||
}; | ||
@@ -1014,2 +1109,3 @@ }); | ||
} | ||
return ResizeObserver; | ||
@@ -1084,2 +1180,15 @@ })(); | ||
/** | ||
* Returns the global window object associated with provided element. | ||
*/ | ||
function getWindowOf$1(target) { | ||
// Assume that the element is an instance of Node, which means that it | ||
// has the "ownerDocument" property from which we can retrieve a | ||
// corresponding global object. | ||
var ownerGlobal = target && target.ownerDocument && target.ownerDocument.defaultView; // Return the local window object if it's not possible extract one from | ||
// provided element. | ||
return ownerGlobal || window; | ||
} | ||
function withContentRect(types) { | ||
@@ -1115,2 +1224,3 @@ return function (WrappedComponent) { | ||
_this._node = null; | ||
_this._window = null; | ||
@@ -1124,3 +1234,3 @@ _this.measure = function (entries) { | ||
_this._animationFrameID = window.requestAnimationFrame(function () { | ||
_this._animationFrameID = _this._window.requestAnimationFrame(function () { | ||
if (_this._resizeObserver !== null) { | ||
@@ -1144,2 +1254,3 @@ _this.setState({ | ||
_this._node = node; | ||
_this._window = getWindowOf$1(_this._node); | ||
@@ -1179,2 +1290,6 @@ if (_this._resizeObserver !== null && _this._node !== null) { | ||
_proto.componentWillUnmount = function componentWillUnmount() { | ||
if (this._window !== null) { | ||
this._window.cancelAnimationFrame(this._animationFrameID); | ||
} | ||
if (this._resizeObserver !== null) { | ||
@@ -1185,4 +1300,2 @@ this._resizeObserver.disconnect(); | ||
} | ||
window.cancelAnimationFrame(this._animationFrameID); | ||
}; | ||
@@ -1189,0 +1302,0 @@ |
{ | ||
"name": "react-measure", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Compute measurements of React components.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js", |
@@ -7,2 +7,3 @@ import { Component, createElement } from 'react' | ||
import getContentRect from './get-content-rect' | ||
import getWindowOf from './get-window-of' | ||
@@ -39,2 +40,4 @@ function withContentRect(types) { | ||
_window = null | ||
componentDidMount() { | ||
@@ -54,2 +57,6 @@ this._resizeObserver = new ResizeObserver(this.measure) | ||
componentWillUnmount() { | ||
if (this._window !== null) { | ||
this._window.cancelAnimationFrame(this._animationFrameID) | ||
} | ||
if (this._resizeObserver !== null) { | ||
@@ -59,3 +66,2 @@ this._resizeObserver.disconnect() | ||
} | ||
window.cancelAnimationFrame(this._animationFrameID) | ||
} | ||
@@ -73,3 +79,3 @@ | ||
this._animationFrameID = window.requestAnimationFrame(() => { | ||
this._animationFrameID = this._window.requestAnimationFrame(() => { | ||
if (this._resizeObserver !== null) { | ||
@@ -90,2 +96,3 @@ this.setState({ contentRect }) | ||
this._node = node | ||
this._window = getWindowOf(this._node) | ||
@@ -92,0 +99,0 @@ if (this._resizeObserver !== null && this._node !== null) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15
1744
79120