UNPKG

3.04 kBPlain TextView Raw
1'use strict';
2import type { ShadowNodeWrapper, StyleProps } from '../commonTypes';
3import {
4 isChromeDebugger,
5 isFabric,
6 isJest,
7 shouldBeUseWeb,
8} from '../PlatformChecker';
9import type {
10 AnimatedRef,
11 AnimatedRefOnJS,
12 AnimatedRefOnUI,
13} from '../hook/commonTypes';
14import type { Component } from 'react';
15import { processColorsInProps } from '../Colors';
16
17type SetNativeProps = <T extends Component>(
18 animatedRef: AnimatedRef<T>,
19 updates: StyleProps
20) => void;
21/**
22 * Lets you imperatively update component properties. You should always reach for [useAnimatedStyle](https://docs.swmansion.com/react-native-reanimated/docs/core/useAnimatedStyle) and [useAnimatedProps](https://docs.swmansion.com/react-native-reanimated/docs/core/useAnimatedProps) first when animating styles or properties.
23 *
24 * @param animatedRef - An [animated ref](https://docs.swmansion.com/react-native-reanimated/docs/core/useAnimatedRef#returns) connected to the component you'd want to update.
25 * @param updates - An object with properties you want to update.
26 * @see https://docs.swmansion.com/react-native-reanimated/docs/advanced/setNativeProps
27 */
28export let setNativeProps: SetNativeProps;
29
30function setNativePropsFabric(
31 animatedRef: AnimatedRefOnJS | AnimatedRefOnUI,
32 updates: StyleProps
33) {
34 'worklet';
35 if (!_WORKLET) {
36 console.warn(
37 '[Reanimated] setNativeProps() can only be used on the UI runtime.'
38 );
39 return;
40 }
41 const shadowNodeWrapper = animatedRef() as ShadowNodeWrapper;
42 processColorsInProps(updates);
43 global._updatePropsFabric!([{ shadowNodeWrapper, updates }]);
44}
45
46function setNativePropsPaper(
47 animatedRef: AnimatedRefOnJS | AnimatedRefOnUI,
48 updates: StyleProps
49) {
50 'worklet';
51 if (!_WORKLET) {
52 console.warn(
53 '[Reanimated] setNativeProps() can only be used on the UI runtime.'
54 );
55 return;
56 }
57 const tag = animatedRef() as number;
58 const name = (animatedRef as AnimatedRefOnUI).viewName.value;
59 processColorsInProps(updates);
60 global._updatePropsPaper!([{ tag, name, updates }]);
61}
62
63function setNativePropsJest() {
64 console.warn('[Reanimated] setNativeProps() is not supported with Jest.');
65}
66
67function setNativePropsChromeDebugger() {
68 console.warn(
69 '[Reanimated] setNativeProps() is not supported with Chrome Debugger.'
70 );
71}
72
73function setNativePropsDefault() {
74 console.warn(
75 '[Reanimated] setNativeProps() is not supported on this configuration.'
76 );
77}
78
79if (!shouldBeUseWeb()) {
80 // Those assertions are actually correct since on Native platforms `AnimatedRef` is
81 // mapped as a different function in `shareableMappingCache` and
82 // TypeScript is not able to infer that.
83 if (isFabric()) {
84 setNativeProps = setNativePropsFabric as unknown as SetNativeProps;
85 } else {
86 setNativeProps = setNativePropsPaper as unknown as SetNativeProps;
87 }
88} else if (isJest()) {
89 setNativeProps = setNativePropsJest;
90} else if (isChromeDebugger()) {
91 setNativeProps = setNativePropsChromeDebugger;
92} else {
93 setNativeProps = setNativePropsDefault;
94}