2023-02-15 16:20:58 -05:00
|
|
|
/*
|
|
|
|
|
Copyright 2023 New Vector Ltd
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-01 11:32:10 -05:00
|
|
|
import { MutableRefObject, RefCallback, useCallback } from "react";
|
|
|
|
|
|
2023-02-13 22:38:27 -05:00
|
|
|
/**
|
|
|
|
|
* Combines multiple refs into one, useful for attaching multiple refs to the
|
|
|
|
|
* same DOM node.
|
|
|
|
|
*/
|
2023-02-01 11:32:10 -05:00
|
|
|
export const useMergedRefs = <T>(
|
|
|
|
|
...refs: (MutableRefObject<T | null> | RefCallback<T | null>)[]
|
|
|
|
|
): RefCallback<T | null> =>
|
|
|
|
|
useCallback(
|
|
|
|
|
(value) =>
|
|
|
|
|
refs.forEach((ref) => {
|
|
|
|
|
if (typeof ref === "function") {
|
|
|
|
|
ref(value);
|
|
|
|
|
} else {
|
|
|
|
|
ref.current = value;
|
|
|
|
|
}
|
|
|
|
|
}),
|
2023-02-13 21:57:57 -05:00
|
|
|
// Since this isn't an array literal, we can't use the static dependency
|
|
|
|
|
// checker, but that's okay
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-02-01 11:32:10 -05:00
|
|
|
refs
|
|
|
|
|
);
|