Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getAnimatableRef function #4533

Merged
merged 4 commits into from
Jun 7, 2023
Merged

Added getAnimatableRef function #4533

merged 4 commits into from
Jun 7, 2023

Conversation

behenate
Copy link
Member

@behenate behenate commented Jun 5, 2023

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.

Simulator.Screen.Recording.-.Old.Orientation.-.2023-06-05.at.16.41.04.mp4

Copy link
Member

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works like a charm. Thanks for the PR! 👏

Tested on iOS with the following example:

App.tsx
import { StyleSheet, View } from 'react-native';

import React, { useEffect } from 'react';
import Animated, {
  useAnimatedStyle,
  useSharedValue,
  withRepeat,
  withTiming,
} from 'react-native-reanimated';

class MyClassComponent extends React.Component {
  innerRef: View | null = null;

  getAnimatableRef() {
    return this.innerRef;
  }

  render() {
    return (
      <View style={styles.outer}>
        <View
          ref={(component) => {
            this.innerRef = component;
          }}
          style={styles.inner}
        />
      </View>
    );
  }
}

const AnimatedMyClassComponent =
  Animated.createAnimatedComponent(MyClassComponent);

export default function App() {
  const sv = useSharedValue(0);

  useEffect(() => {
    sv.value = 0;
    sv.value = withRepeat(withTiming(1), -1, true);
  }, [sv]);

  const animatedStyle = useAnimatedStyle(() => {
    return {
      width: sv.value * 200,
    };
  });

  return (
    <View style={styles.container}>
      <AnimatedMyClassComponent style={animatedStyle} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  outer: {
    backgroundColor: 'black',
    width: 200,
    height: 200,
  },
  inner: {
    backgroundColor: 'cyan',
    width: 100,
    height: 100,
  },
});

width="60"
height="300"
fill="none"
stroke={'black'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
stroke={'black'}
stroke="black"

@tomekzaw
Copy link
Member

tomekzaw commented Jun 7, 2023

Doesn't work on web though 😢

edit: It also didn't work before so it's not a regression.

edit 2: Fixed with the following patch in js-reanimated/index.ts:

+import { findNodeHandle } from 'react-native';
+import { Component } from 'react';

 interface JSReanimatedComponent {
   // ...
+  getAnimatableRef?: () => Component;
 }

-    const component = viewRef._component;
+    const ref = viewRef._component.getAnimatableRef
+        ? viewRef._component.getAnimatableRef()
+        : viewRef._component;
+    const component = findNodeHandle(ref);

I think it's okay to merge this PR now so it will be included in 3.3.0.

@tomekzaw tomekzaw requested a review from piaskowyk June 7, 2023 15:42
@tomekzaw tomekzaw added this pull request to the merge queue Jun 7, 2023
Merged via the queue into main with commit 2426f32 Jun 7, 2023
behenate added a commit to expo/expo that referenced this pull request Jun 23, 2023
# Why

Our demo screen for `BlurView` has stopped working.

# How

> NOTE: `getAnimatableRef` relies on this reanimated PR:
software-mansion/react-native-reanimated#4533.
Don't merge this PR until the Reanimated feature gets approved by the
Reanimated team.

On iOS and Android added the `getAnimatableRef` function, which passes
the `NativeBlurView` to `reanimated` as the animatable component when
user calls the `Animated.createAnimatedComponent`. On web used
`setNativeProps` to add reanimated support (it wasn't working correctly
before). It would be too complicated to support both `Animated API` and
`react-native-reanimated`, so support for animating intensity with
`Animated API` has been dropped.

# Test Plan

Tested on Android, iOS and web

> Demo video might not work on Safari due to my recording settings, it
works ok on Google Chrome


https://github.com/expo/expo/assets/31368152/a5095c99-12a4-4be2-9e04-0e6b80e7ecb3

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
github-merge-queue bot pushed a commit that referenced this pull request Jun 11, 2024
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary
#4533
added support for `getAnimatableRef`. Tried testing it for this
[issue](expo/expo#29197) but found it was not
working for web.
<!-- Explain the motivation for this PR. Include "Fixes #<number>" if
applicable. -->

## Test plan
`getAnimatableRef` should work on web.
<!-- Provide a minimal but complete code snippet that can be used to
test out this change along with instructions how to run it and a
description of the expected behavior. -->

---------

Co-authored-by: Krzysztof Piaskowy <krzysztof.piaskowy@swmansion.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants