@coreui/react-pro
Version:
UI Components Library for React.js
51 lines (47 loc) • 1.49 kB
JavaScript
;
var React = require('react');
// code borrowed from https://github.com/reach/reach-ui
// problem described https://github.com/facebook/react/issues/13029
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function useForkedRef(...refs) {
return React.useMemo(() => {
if (refs.every((ref) => ref == null)) {
return null;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (node) => {
refs.forEach((ref) => {
assignRef(ref, node);
});
};
},
// useForkedRef accepts a variable number of refs, so the dependency list
// cannot be a static array literal; the refs are compared element-wise.
// eslint-disable-next-line react-hooks/use-memo, react-hooks/exhaustive-deps
refs);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function assignRef(ref,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value) {
if (ref == null)
return;
if (isFunction(ref)) {
ref(value);
}
else {
try {
ref.current = value;
}
catch (_a) {
throw new Error(`Cannot assign value "${value}" to ref "${ref}"`);
}
}
}
function isFunction(value) {
return typeof value === 'function';
}
exports.assignRef = assignRef;
exports.isFunction = isFunction;
exports.useForkedRef = useForkedRef;
//# sourceMappingURL=useForkedRef.js.map