@appbuckets/react-ui-core
Version:
Core utilities built for AppBuckets React UI Framework
73 lines (66 loc) • 1.96 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var invariant = require('tiny-invariant');
function _interopDefaultLegacy(e) {
return e && typeof e === 'object' && 'default' in e ? e : { default: e };
}
var invariant__default = /*#__PURE__*/ _interopDefaultLegacy(invariant);
/**
* A function to correctly handles passing refs.
*
* @param ref The ref Object or Function
* @param node The node that should by passed by ref
*/
function handleRef(ref, node) {
// Check Ref is not a deprecated string
// This check is necessary for JavaScript only usage
if (process.env.NODE_ENV !== 'production') {
invariant__default['default'](
typeof ref !== 'string',
[
'String ref are not supported anymore. This is a Legacy API and will be likely to be removed',
'in one of the future release of React',
].join(' ')
);
}
if (typeof ref === 'function') {
ref(node);
return;
}
if (ref !== null && typeof ref === 'object') {
// The current property is defined as readonly
// but this ia valid way to assign ref, because is a mutable object
ref.current = node;
}
}
var refObjectsStore = new WeakMap();
var nullRefObject = { current: null };
function toRefObject(node) {
// If no node is passed, return null ref
if (node === null) {
return nullRefObject;
}
// If node has been previously stored, get cached
if (refObjectsStore.has(node)) {
return refObjectsStore.get(node);
}
// Cache the ref
var refObject = { current: node };
refObjectsStore.set(node, refObject);
return refObject;
}
/**
* Check if a thing is a valid React Ref Object
*
* @param ref Thing to Check
*/
function isRefObject(ref) {
return (
ref !== null &&
typeof ref === 'object' &&
Object.prototype.hasOwnProperty.call(ref, 'current')
);
}
exports.handleRef = handleRef;
exports.isRefObject = isRefObject;
exports.toRefObject = toRefObject;