Port 3cc36b98c084f8e5c602e56a486f89cfaa34db0b to glitch-soc Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: diondiondion <mail@diondiondion.com> Signed-off-by: Claire <claire.github-309c@sitedethib.com>
30 lines
764 B
TypeScript
30 lines
764 B
TypeScript
import { useCallback } from 'react';
|
|
|
|
export function useMergedRefs<T>(...refs: React.Ref<T>[]) {
|
|
const setRef: React.RefCallback<T> = useCallback(
|
|
(node: T | null) => {
|
|
const cleanups: (() => void)[] = [];
|
|
for (const ref of refs) {
|
|
if (typeof ref === 'function') {
|
|
const cleanup = ref(node);
|
|
if (cleanup) {
|
|
cleanups.push(cleanup);
|
|
}
|
|
} else if (ref) {
|
|
// eslint-disable-next-line react-hooks/immutability -- Refs can be mutated
|
|
ref.current = node;
|
|
}
|
|
}
|
|
|
|
return () => {
|
|
for (const cleanup of cleanups) {
|
|
cleanup();
|
|
}
|
|
};
|
|
},
|
|
[refs],
|
|
);
|
|
// eslint-disable-next-line react-hooks/immutability
|
|
return setRef;
|
|
}
|