import { useRef, useEffect, type RefObject, type ForwardedRef } from 'react'; /** * Custom hook to combine a forwarded ref with an internal ref. * This is useful when you want to pass a ref to a component while also keeping a reference to the element internally. * @private * @param ref - The forwarded ref to combine with the internal ref. * @returns A ref object that combines the forwarded ref and the internal ref. * @example * const MyComponent = forwardRef((props, ref) => { * const combinedRef = useCombinedRef(ref); * return
Hello
; * }); */ export function useCombinedRef(ref?: ForwardedRef): RefObject { const innerRef = useRef(null); useEffect(() => { if (!ref) return; if (typeof ref === 'function') { ref(innerRef.current); } else if ('current' in ref) { // eslint-disable-next-line sonarjs/deprecation (ref as React.MutableRefObject).current = innerRef.current; } }, [ref]); return innerRef; }