From 2426f32095af417f1cfc1789479c3d2ee7c72e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20Dr=C3=B3=C5=BCd=C5=BC?= <31368152+behenate@users.noreply.github.com> Date: Wed, 7 Jun 2023 19:50:48 +0200 Subject: [PATCH] Added getAnimatableRef function (#4533) ## Summary Added an option for a component to specify which other component should be animated when `createAnimatedComponent` is executed on it by adding `getAnimatableRef` function (we might want to think of a better name). This is useful for some 3rd party libraries which want to add support for animating their component, but it's wrapped inside of another component which is inaccessible to the user. ## Test plan Included a simple example screen where inner `Rect` component is animated when `createAnimatedComponent` is executed on the component containing it. https://github.com/software-mansion/react-native-reanimated/assets/31368152/96d2cd19-a4f9-48ac-bd23-f50746dc7e0b --- app/src/examples/AnimatableRefExample.tsx | 82 +++++++++++++++++++++++ app/src/examples/index.ts | 6 ++ src/createAnimatedComponent.tsx | 10 ++- 3 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 app/src/examples/AnimatableRefExample.tsx diff --git a/app/src/examples/AnimatableRefExample.tsx b/app/src/examples/AnimatableRefExample.tsx new file mode 100644 index 00000000000..0d9b20b385f --- /dev/null +++ b/app/src/examples/AnimatableRefExample.tsx @@ -0,0 +1,82 @@ +import React, { useState } from 'react'; +import { View, StyleSheet, Button } from 'react-native'; + +import { Rect, Svg } from 'react-native-svg'; +import Animated, { + useAnimatedProps, + useSharedValue, + withSpring, +} from 'react-native-reanimated'; + +type SwitchProps = { + y?: number; +}; + +class Switch extends React.Component { + rectRef: Rect | null = null; + + // When an animated version of the Switch is created we want to animate the inner Rect instead of the outer Svg component. + getAnimatableRef() { + return this.rectRef; + } + + render() { + return ( + + + { + this.rectRef = component; + }} + /> + + ); + } +} + +const AnimatedSwitch = Animated.createAnimatedComponent(Switch); +export default function AnimatableRefExample() { + const [isUp, setIsUp] = useState(true); + const sv = useSharedValue(0); + + const animatedProps = useAnimatedProps(() => { + return { + y: sv.value + 10, + }; + }); + + return ( + + +