cloudhubui
Version:
Various components to use in react projects
52 lines (41 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = composeRefs;
function composeRefs() {
for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
refs[_key] = arguments[_key];
}
if (refs.length === 2) {
// micro-optimize the hot path
return composeTwoRefs(refs[0], refs[1]);
}
return refs.slice(1).reduce(function (semiCombinedRef, refToInclude) {
return composeTwoRefs(semiCombinedRef, refToInclude);
}, refs[0]);
}
var composedRefCache = new WeakMap();
function composeTwoRefs(ref1, ref2) {
if (ref1 && ref2) {
var ref1Cache = composedRefCache.get(ref1) || new WeakMap();
composedRefCache.set(ref1, ref1Cache);
var composedRef = ref1Cache.get(ref2) || function (instance) {
updateRef(ref1, instance);
updateRef(ref2, instance);
};
ref1Cache.set(ref2, composedRef);
return composedRef;
}
if (!ref1) {
return ref2;
}
return ref1;
}
function updateRef(ref, instance) {
if (typeof ref === 'function') {
ref(instance);
} else {
ref.current = instance;
}
}