MCPcopy Create free account
hub / github.com/adobe/react-spectrum / mergeRefs

Function mergeRefs

packages/react-aria/src/utils/mergeRefs.ts:18–46  ·  view source on GitHub ↗
(
  ...refs: Array<Ref<T> | MutableRefObject<T> | null | undefined>
)

Source from the content-addressed store, hash-verified

16 * Merges multiple refs into one. Works with either callback or object refs.
17 */
18export function mergeRefs<T>(
19 ...refs: Array<Ref<T> | MutableRefObject<T> | null | undefined>
20): Ref<T> {
21 if (refs.length === 1 && refs[0]) {
22 return refs[0];
23 }
24
25 return (value: T | null) => {
26 let hasCleanup = false;
27
28 const cleanups = refs.map(ref => {
29 const cleanup = setRef(ref, value);
30 hasCleanup ||= typeof cleanup == 'function';
31 return cleanup;
32 });
33
34 if (hasCleanup) {
35 return () => {
36 cleanups.forEach((cleanup, i) => {
37 if (typeof cleanup === 'function') {
38 cleanup();
39 } else {
40 setRef(refs[i], null);
41 }
42 });
43 };
44 }
45 };
46}
47
48function setRef<T>(ref: Ref<T> | MutableRefObject<T> | null | undefined, value: T) {
49 if (typeof ref === 'function') {

Callers 15

TextFieldFunction · 0.90
Pressable.tsxFile · 0.90
useFocusable.tsxFile · 0.90
mergePropsFunction · 0.90
useAutocompleteFunction · 0.90
Disclosure.tsxFile · 0.90
ModalContentFunction · 0.90
RadioGroup.tsxFile · 0.90
TableInnerFunction · 0.90
Switch.tsxFile · 0.90
useCheckboxAriaFunction · 0.90
TokenField.tsxFile · 0.90

Calls 2

setRefFunction · 0.85
cleanupFunction · 0.85

Tested by 1

TextFieldFunction · 0.72