react-measure
Advanced tools
Comparing version 2.2.4 to 2.2.5
## CHANGELOG | ||
### 2.2.5 | ||
Fix sporadic `ResizeObserver` loop limit exceeded error when using `onResize` [#133](https://github.com/souporserious/react-measure/pull/133) | ||
### 2.2.4 | ||
@@ -4,0 +8,0 @@ |
@@ -123,8 +123,8 @@ 'use strict'; | ||
}); | ||
if (typeof _this.props.onResize === 'function') { | ||
_this.props.onResize(contentRect); | ||
} | ||
} | ||
}); | ||
if (typeof _this.props.onResize === 'function') { | ||
_this.props.onResize(contentRect); | ||
} | ||
}; | ||
@@ -131,0 +131,0 @@ |
@@ -117,8 +117,8 @@ import _extends from '@babel/runtime/helpers/esm/extends'; | ||
}); | ||
if (typeof _this.props.onResize === 'function') { | ||
_this.props.onResize(contentRect); | ||
} | ||
} | ||
}); | ||
if (typeof _this.props.onResize === 'function') { | ||
_this.props.onResize(contentRect); | ||
} | ||
}; | ||
@@ -125,0 +125,0 @@ |
@@ -59,3 +59,2 @@ (function (global, factory) { | ||
} | ||
/** | ||
@@ -70,41 +69,34 @@ * 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 (function () { | ||
function anonymous() { | ||
return /** @class */ (function () { | ||
function class_1() { | ||
this.__entries__ = []; | ||
} | ||
var prototypeAccessors = { size: { configurable: true } }; | ||
Object.defineProperty(class_1.prototype, "size", { | ||
/** | ||
* @returns {boolean} | ||
*/ | ||
get: function () { | ||
return this.__entries__.length; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
/** | ||
* @returns {boolean} | ||
*/ | ||
prototypeAccessors.size.get = function () { | ||
return this.__entries__.length; | ||
}; | ||
/** | ||
* @param {*} key | ||
* @returns {*} | ||
*/ | ||
anonymous.prototype.get = function (key) { | ||
class_1.prototype.get = function (key) { | ||
var index = getIndex(this.__entries__, key); | ||
var entry = this.__entries__[index]; | ||
return entry && entry[1]; | ||
}; | ||
/** | ||
@@ -115,12 +107,11 @@ * @param {*} key | ||
*/ | ||
anonymous.prototype.set = function (key, value) { | ||
class_1.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]); | ||
} | ||
}; | ||
/** | ||
@@ -130,6 +121,5 @@ * @param {*} key | ||
*/ | ||
anonymous.prototype.delete = function (key) { | ||
class_1.prototype.delete = function (key) { | ||
var entries = this.__entries__; | ||
var index = getIndex(entries, key); | ||
if (~index) { | ||
@@ -139,3 +129,2 @@ entries.splice(index, 1); | ||
}; | ||
/** | ||
@@ -145,13 +134,11 @@ * @param {*} key | ||
*/ | ||
anonymous.prototype.has = function (key) { | ||
class_1.prototype.has = function (key) { | ||
return !!~getIndex(this.__entries__, key); | ||
}; | ||
/** | ||
* @returns {void} | ||
*/ | ||
anonymous.prototype.clear = function () { | ||
class_1.prototype.clear = function () { | ||
this.__entries__.splice(0); | ||
}; | ||
/** | ||
@@ -162,16 +149,10 @@ * @param {Function} callback | ||
*/ | ||
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]; | ||
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]; | ||
callback.call(ctx, entry[1], entry[0]); | ||
} | ||
}; | ||
Object.defineProperties( anonymous.prototype, prototypeAccessors ); | ||
return anonymous; | ||
return class_1; | ||
}()); | ||
@@ -190,11 +171,8 @@ })(); | ||
} | ||
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 | ||
@@ -217,3 +195,2 @@ return Function('return this')(); | ||
} | ||
return function (callback) { return setTimeout(function () { return callback(Date.now()); }, 1000 / 60); }; | ||
@@ -224,3 +201,2 @@ })(); | ||
var trailingTimeout = 2; | ||
/** | ||
@@ -234,7 +210,4 @@ * Creates a wrapper function which ensures that provided callback will be | ||
*/ | ||
var throttle = function (callback, delay) { | ||
var leadingCall = false, | ||
trailingCall = false, | ||
lastCallTime = 0; | ||
function throttle (callback, delay) { | ||
var leadingCall = false, trailingCall = false, lastCallTime = 0; | ||
/** | ||
@@ -249,6 +222,4 @@ * Invokes the original callback function and schedules new invocation if | ||
leadingCall = false; | ||
callback(); | ||
} | ||
if (trailingCall) { | ||
@@ -258,3 +229,2 @@ proxy(); | ||
} | ||
/** | ||
@@ -270,3 +240,2 @@ * Callback invoked after the specified delay. It will further postpone | ||
} | ||
/** | ||
@@ -279,3 +248,2 @@ * Schedules invocation of the original function. | ||
var timeStamp = Date.now(); | ||
if (leadingCall) { | ||
@@ -286,3 +254,2 @@ // Reject immediately following calls. | ||
} | ||
// Schedule new call to be in invoked when the pending one is resolved. | ||
@@ -293,239 +260,219 @@ // 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 = 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); | ||
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); | ||
} | ||
// 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 | ||
/** | ||
* 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(); | ||
}); | ||
} else { | ||
document.addEventListener('DOMSubtreeModified', this.refresh); | ||
// 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; | ||
}()); | ||
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. | ||
@@ -538,5 +485,4 @@ * | ||
var defineConfigurable = (function (target, props) { | ||
for (var i = 0, list = Object.keys(props); i < list.length; i += 1) { | ||
var key = list[i]; | ||
for (var _i = 0, _a = Object.keys(props); _i < _a.length; _i++) { | ||
var key = _a[_i]; | ||
Object.defineProperty(target, key, { | ||
@@ -549,3 +495,2 @@ value: props[key], | ||
} | ||
return target; | ||
@@ -565,3 +510,2 @@ }); | ||
var ownerGlobal = target && target.ownerDocument && target.ownerDocument.defaultView; | ||
// Return the local global object if it's not possible extract one from | ||
@@ -574,3 +518,2 @@ // provided element. | ||
var emptyRect = createRectInit(0, 0, 0, 0); | ||
/** | ||
@@ -585,3 +528,2 @@ * Converts provided string to a number. | ||
} | ||
/** | ||
@@ -595,12 +537,11 @@ * Extracts borders size from provided styles. | ||
function getBordersSize(styles) { | ||
var positions = [], len = arguments.length - 1; | ||
while ( len-- > 0 ) positions[ len ] = arguments[ len + 1 ]; | ||
var positions = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
positions[_i - 1] = arguments[_i]; | ||
} | ||
return positions.reduce(function (size, position) { | ||
var value = styles['border-' + position + '-width']; | ||
return size + toFloat(value); | ||
}, 0); | ||
} | ||
/** | ||
@@ -615,14 +556,9 @@ * Extracts paddings sizes from provided styles. | ||
var paddings = {}; | ||
for (var i = 0, list = positions; i < list.length; i += 1) { | ||
var position = list[i]; | ||
for (var _i = 0, positions_1 = positions; _i < positions_1.length; _i++) { | ||
var position = positions_1[_i]; | ||
var value = styles['padding-' + position]; | ||
paddings[position] = toFloat(value); | ||
} | ||
return paddings; | ||
} | ||
/** | ||
@@ -637,6 +573,4 @@ * Calculates content rectangle of provided SVG element. | ||
var bbox = target.getBBox(); | ||
return createRectInit(0, 0, bbox.width, bbox.height); | ||
} | ||
/** | ||
@@ -651,5 +585,3 @@ * Calculates content rectangle of provided HTMLElement. | ||
// used exclusively as they provide rounded values. | ||
var clientWidth = target.clientWidth; | ||
var clientHeight = target.clientHeight; | ||
var clientWidth = target.clientWidth, clientHeight = target.clientHeight; | ||
// By this condition we can catch all non-replaced inline, hidden and | ||
@@ -666,3 +598,2 @@ // detached elements. Though elements with width & height properties less | ||
} | ||
var styles = getWindowOf(target).getComputedStyle(target); | ||
@@ -672,3 +603,2 @@ var paddings = getPaddings(styles); | ||
var vertPad = paddings.top + paddings.bottom; | ||
// Computed styles of width & height are being used because they are the | ||
@@ -678,5 +608,3 @@ // 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 | ||
@@ -694,3 +622,2 @@ // model is applied (except for IE). | ||
} | ||
if (Math.round(height + vertPad) !== clientHeight) { | ||
@@ -700,3 +627,2 @@ height -= getBordersSize(styles, 'top', 'bottom') + vertPad; | ||
} | ||
// Following steps can't be applied to the document's root element as its | ||
@@ -713,3 +639,2 @@ // 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. | ||
@@ -723,3 +648,2 @@ // E.g. for an element with content width of 314.2px it sometimes gives | ||
} | ||
if (Math.abs(horizScrollbar) !== 1) { | ||
@@ -729,6 +653,4 @@ height -= horizScrollbar; | ||
} | ||
return createRectInit(paddings.left, paddings.top, width, height); | ||
} | ||
/** | ||
@@ -746,9 +668,8 @@ * 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'); }; | ||
})(); | ||
/** | ||
@@ -763,3 +684,2 @@ * Checks whether provided element is a document element (<html>). | ||
} | ||
/** | ||
@@ -775,10 +695,7 @@ * Calculates an appropriate content rectangle for provided html or svg element. | ||
} | ||
if (isSVGGraphicsElement(target)) { | ||
return getSVGContentRect(target); | ||
} | ||
return getHTMLElementContentRect(target); | ||
} | ||
/** | ||
@@ -791,12 +708,7 @@ * Creates rectangle with an interface of the DOMRectReadOnly. | ||
*/ | ||
function createReadOnlyRect(ref) { | ||
var x = ref.x; | ||
var y = ref.y; | ||
var width = ref.width; | ||
var height = ref.height; | ||
function createReadOnlyRect(_a) { | ||
var x = _a.x, y = _a.y, width = _a.width, height = _a.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. | ||
@@ -810,6 +722,4 @@ defineConfigurable(rect, { | ||
}); | ||
return rect; | ||
} | ||
/** | ||
@@ -833,239 +743,225 @@ * Creates DOMRectInit object based on the provided dimensions and the x/y coordinates. | ||
*/ | ||
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.'); | ||
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; | ||
} | ||
/** | ||
* 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; | ||
}()); | ||
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.'); | ||
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 }); | ||
} | ||
return ResizeObserverEntry; | ||
}()); | ||
// Do nothing if current environment doesn't have the Element interface. | ||
if (typeof Element === 'undefined' || !(Element instanceof Object)) { | ||
return; | ||
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; | ||
} | ||
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) { | ||
/** | ||
* 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(); | ||
this.controller_.removeObserver(this); | ||
} | ||
}; | ||
/** | ||
* 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); | ||
}; | ||
/** | ||
* 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; | ||
} | ||
}); | ||
}; | ||
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 | ||
@@ -1075,3 +971,2 @@ // for the Map collection as it has all required methods and because WeakMap | ||
var observers = typeof WeakMap !== 'undefined' ? new WeakMap() : new MapShim(); | ||
/** | ||
@@ -1081,21 +976,31 @@ * ResizeObserver API. Encapsulates the ResizeObserver SPI implementation | ||
*/ | ||
var ResizeObserver = function(callback) { | ||
if (!(this instanceof ResizeObserver)) { | ||
throw new TypeError('Cannot call a class as a function.'); | ||
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); | ||
} | ||
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); | ||
}; | ||
return ResizeObserver; | ||
}()); | ||
// Expose public methods of ResizeObserver. | ||
['observe', 'unobserve', 'disconnect'].forEach(function (method) { | ||
[ | ||
'observe', | ||
'unobserve', | ||
'disconnect' | ||
].forEach(function (method) { | ||
ResizeObserver.prototype[method] = function () { | ||
return (ref = observers.get(this))[method].apply(ref, arguments); | ||
var ref; | ||
var _a; | ||
return (_a = observers.get(this))[method].apply(_a, arguments); | ||
}; | ||
@@ -1109,3 +1014,2 @@ }); | ||
} | ||
return ResizeObserver; | ||
@@ -1223,8 +1127,8 @@ })(); | ||
}); | ||
if (typeof _this.props.onResize === 'function') { | ||
_this.props.onResize(contentRect); | ||
} | ||
} | ||
}); | ||
if (typeof _this.props.onResize === 'function') { | ||
_this.props.onResize(contentRect); | ||
} | ||
}; | ||
@@ -1231,0 +1135,0 @@ |
{ | ||
"name": "react-measure", | ||
"version": "2.2.4", | ||
"version": "2.2.5", | ||
"description": "Compute measurements of React components.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js", |
@@ -66,8 +66,7 @@ import { Component, createElement } from 'react' | ||
this.setState({ contentRect }) | ||
if (typeof this.props.onResize === 'function') { | ||
this.props.onResize(contentRect) | ||
} | ||
} | ||
}) | ||
if (typeof this.props.onResize === 'function') { | ||
this.props.onResize(contentRect) | ||
} | ||
} | ||
@@ -74,0 +73,0 @@ |
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
79076
1723