-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
useAnimatedRef.ts
86 lines (76 loc) · 2.73 KB
/
useAnimatedRef.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'use strict';
import type { Component } from 'react';
import { useRef } from 'react';
import { useSharedValue } from './useSharedValue';
import type { AnimatedRef, AnimatedRefOnUI } from './commonTypes';
import type { ShadowNodeWrapper } from '../commonTypes';
import { getShadowNodeWrapperFromRef } from '../fabricUtils';
import { makeShareableCloneRecursive } from '../shareables';
import { shareableMappingCache } from '../shareableMappingCache';
import { Platform, findNodeHandle } from 'react-native';
import type { ScrollView, FlatList } from 'react-native';
import { isFabric } from '../PlatformChecker';
const IS_FABRIC = isFabric();
interface MaybeScrollableComponent extends Component {
getNativeScrollRef?: FlatList['getNativeScrollRef'];
getScrollableNode?:
| ScrollView['getScrollableNode']
| FlatList['getScrollableNode'];
viewConfig?: {
uiViewClassName?: string;
};
}
function getComponentOrScrollable(component: MaybeScrollableComponent) {
if (IS_FABRIC && component.getNativeScrollRef) {
return component.getNativeScrollRef();
} else if (!IS_FABRIC && component.getScrollableNode) {
return component.getScrollableNode();
}
return component;
}
const getTagValueFunction = IS_FABRIC
? getShadowNodeWrapperFromRef
: findNodeHandle;
/**
* Lets you get a reference of a view that you can use inside a worklet.
*
* @returns An object with a `.current` property which contains an instance of a component.
* @see https://docs.swmansion.com/react-native-reanimated/docs/core/useAnimatedRef
*/
export function useAnimatedRef<
TComponent extends Component
>(): AnimatedRef<TComponent> {
const tag = useSharedValue<number | ShadowNodeWrapper | null>(-1);
const viewName = useSharedValue<string | null>(null);
const ref = useRef<AnimatedRef<TComponent>>();
if (!ref.current) {
const fun: AnimatedRef<TComponent> = <AnimatedRef<TComponent>>((
component
) => {
// enters when ref is set by attaching to a component
if (component) {
tag.value = getTagValueFunction(getComponentOrScrollable(component));
fun.current = component;
// viewName is required only on iOS with Paper
if (Platform.OS === 'ios' && !IS_FABRIC) {
viewName.value =
(component as MaybeScrollableComponent)?.viewConfig
?.uiViewClassName || 'RCTView';
}
}
return tag.value;
});
fun.current = null;
const animatedRefShareableHandle = makeShareableCloneRecursive({
__init: () => {
'worklet';
const f: AnimatedRefOnUI = () => tag.value;
f.viewName = viewName;
return f;
},
});
shareableMappingCache.set(fun, animatedRefShareableHandle);
ref.current = fun;
}
return ref.current;
}