remeasure
Version:
Get position and size of the DOM element for any React Component
149 lines (122 loc) • 4.2 kB
JavaScript
;
exports.__esModule = true;
exports.isElementVoidTag = exports.getStateKeys = exports.getNaturalDimensionValue = exports.getMeasureKeys = exports.getComponentName = void 0;
var _constants = require("./constants");
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; }
/**
* @private
*
* @function getComponentName
*
* @description
* get the name of the component from displayName, the internal name, or fallback
*
* @param {Component} Component component to get the display name from
* @returns {string} Component name
*/
var getComponentName = function getComponentName(Component) {
if (Component.displayName) {
return Component.displayName;
}
if (Component.name) {
return Component.name;
}
var match = Component.toString().match(_constants.FUNCTION_NAME_REGEXP);
return match && match[1] || 'Component';
};
/**
* @private
*
* @function getMeasureKeys
*
* @description
* based on the passed keys and options, get the keys that will be measured
*
* @param {Array<string>|string} keys the keys passed to the decorator
* @returns {Array<string>} the keys to measure
*/
exports.getComponentName = getComponentName;
var getMeasureKeys = function getMeasureKeys(keys) {
if (Array.isArray(keys)) {
return _constants.KEY_NAMES.reduce(function (validKeys, key) {
if (~keys.indexOf(key)) {
validKeys.push(key);
}
return validKeys;
}, []);
}
if (typeof keys === 'string' && ~_constants.KEY_NAMES.indexOf(keys)) {
return [keys];
}
return _constants.KEY_NAMES;
};
/**
* @private
*
* @function getNaturalDimensionValue
*
* @description
* For naturalHeight and naturalWidth, coalesce the values
* with scrollHeight and scrollWIdth if the element does not
* natively support it
*
* @param {HTMLElement} source the element to get the size / position value from
* @param {string} key the size / position value to retrieve from source
* @returns {number}
*/
exports.getMeasureKeys = getMeasureKeys;
var getNaturalDimensionValue = function getNaturalDimensionValue(source, key) {
return source.hasOwnProperty(key) ? source[key] : source[key.replace(_constants.NATURAL_REGEXP, 'scroll')];
};
/**
* @private
*
* @function getStateKeys
*
* @description
* get the keys to be used in state
*
* @param {Object} props the instance props
* @returns {Array<Object>} the keys to base the state object off of
*/
exports.getNaturalDimensionValue = getNaturalDimensionValue;
var getStateKeys = function getStateKeys(props) {
var childrenIgnored = props.children,
debounceIgnored = props.debounce,
flattenIgnored = props.flatten,
inheritedMethodsIgnored = props.inheritedMethods,
keys = props.keys,
namespaceIgnored = props.namespace,
renderOnResizeIgnored = props.renderOnResize,
renderOnWindowResizeIgnored = props.renderOnWindowResize,
specificProperties = _objectWithoutPropertiesLoose(props, ["children", "debounce", "flatten", "inheritedMethods", "keys", "namespace", "renderOnResize", "renderOnWindowResize"]);
var specificKeys = Array.isArray(keys) ? keys : Object.keys(specificProperties).filter(function (property) {
return specificProperties[property];
});
if (specificKeys.length) {
return specificKeys.reduce(function (requestedKeys, key) {
var indexOfKey = _constants.KEY_NAMES.indexOf(key);
if (~indexOfKey) {
requestedKeys.push(_constants.KEYS[indexOfKey]);
}
return requestedKeys;
}, []);
}
return _constants.KEYS;
};
/**
* @private
*
* @function isElementVoidTag
*
* @description
* is the element passed a void tag name
*
* @param {HTMLElement} element
* @returns {boolean}
*/
exports.getStateKeys = getStateKeys;
var isElementVoidTag = function isElementVoidTag(element) {
return !!~_constants.VOID_ELEMENT_TAG_NAMES.indexOf(element.tagName);
};
exports.isElementVoidTag = isElementVoidTag;