@mui/utils
Version:
Utility functions for React components.
65 lines (64 loc) • 1.98 kB
JavaScript
'use client';
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useForkRef;
var React = _interopRequireWildcard(require("react"));
/**
* Merges refs into a single memoized callback ref or `null`.
*
* ```tsx
* const rootRef = React.useRef<Instance>(null);
* const refFork = useForkRef(rootRef, props.ref);
*
* return (
* <Root {...props} ref={refFork} />
* );
* ```
*
* @param {Array<React.Ref<Instance> | undefined>} refs The ref array.
* @returns {React.RefCallback<Instance> | null} The new ref callback.
*/
function useForkRef(...refs) {
const cleanupRef = React.useRef(undefined);
const refEffect = React.useCallback(instance => {
const cleanups = refs.map(ref => {
if (ref == null) {
return null;
}
if (typeof ref === 'function') {
const refCallback = ref;
const refCleanup = refCallback(instance);
return typeof refCleanup === 'function' ? refCleanup : () => {
refCallback(null);
};
}
ref.current = instance;
return () => {
ref.current = null;
};
});
return () => {
cleanups.forEach(refCleanup => refCleanup?.());
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, refs);
return React.useMemo(() => {
if (refs.every(ref => ref == null)) {
return null;
}
return value => {
if (cleanupRef.current) {
cleanupRef.current();
cleanupRef.current = undefined;
}
if (value != null) {
cleanupRef.current = refEffect(value);
}
};
// TODO: uncomment once we enable eslint-plugin-react-compiler // eslint-disable-next-line react-compiler/react-compiler -- intentionally ignoring that the dependency array must be an array literal
// eslint-disable-next-line react-hooks/exhaustive-deps
}, refs);
}
;