Skip to content

Commit

Permalink
Trigger rerender on animation complete (#37836)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #37836

When using the native driver for animations that involve layout changes (ie. translateY and other transforms, but not styles such as opacity), because it bypasses Fabric, the new coordinates are not updated so the Pressability responder region/tap target is incorrect.

Prior diffs ensure that upon completion of natively driven animations, the final values are synced to the JS side AnimatedValue nodes. In this diff, on completion of a natively driven animation, AnimatedProps.update() is called, which in turn calls the value update callback on AnimatedProps, which [triggers a rerender (via setState)](https://www.internalfb.com/code/fbsource/[566daad5db45807260a8af1f85385ca86aebf894]/xplat/js/react-native-github/packages/react-native/Libraries/Animated/useAnimatedProps.js?lines=80) which has the effect of pushing the latest animated values to Fabric.

Alternative considered was using setNativeProps, but that approach was dropped as setNativeProps was only introduced to make migration easier and should not be used for new code, as per sammy-SC.

Changelog:
[General][Fixed] - When animating using native driver, trigger rerender on animation completion in order to update Pressability responder regions

Reviewed By: javache

Differential Revision: D46655246

fbshipit-source-id: b008c24f9d016be4b145ba799fffae5f55fab787
  • Loading branch information
genkikondo authored and facebook-github-bot committed Jun 14, 2023
1 parent 0c520e0 commit c870a52
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1027,21 +1027,17 @@ describe('Native Animated', () => {
}).start();
jest.runAllTimers();

Animated.timing(opacity, {
toValue: 4,
duration: 500,
useNativeDriver: false,
}).start();
try {
process.env.NODE_ENV = 'development';
expect(jest.runAllTimers).toThrow(
'Attempting to run JS driven animation on animated node that has ' +
'been moved to "native" earlier by starting an animation with ' +
'`useNativeDriver: true`',
);
} finally {
process.env.NODE_ENV = 'test';
}
expect(
Animated.timing(opacity, {
toValue: 4,
duration: 500,
useNativeDriver: false,
}).start,
).toThrow(
'Attempting to run JS driven animation on animated node that has ' +
'been moved to "native" earlier by starting an animation with ' +
'`useNativeDriver: true`',
);
});

it('fails for unsupported styles', () => {
Expand Down
23 changes: 23 additions & 0 deletions packages/react-native/Libraries/Animated/animations/Animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
'use strict';

import type {PlatformConfig} from '../AnimatedPlatformConfig';
import type AnimatedNode from '../nodes/AnimatedNode';
import type AnimatedValue from '../nodes/AnimatedValue';

import NativeAnimatedHelper from '../NativeAnimatedHelper';
import AnimatedProps from '../nodes/AnimatedProps';

export type EndResult = {finished: boolean, value?: number, ...};
export type EndCallback = (result: EndResult) => void;
Expand Down Expand Up @@ -65,6 +67,21 @@ export default class Animation {
onEnd && onEnd(result);
}

__findAnimatedPropsNodes(node: AnimatedNode): Array<AnimatedProps> {
const result = [];

if (node instanceof AnimatedProps) {
result.push(node);
return result;
}

for (const child of node.__getChildren()) {
result.push(...this.__findAnimatedPropsNodes(child));
}

return result;
}

__startNativeAnimation(animatedValue: AnimatedValue): void {
const startNativeAnimationWaitId = `${startNativeAnimationNextId}:startAnimation`;
startNativeAnimationNextId += 1;
Expand All @@ -88,6 +105,12 @@ export default class Animation {
const {value} = result;
if (value != null) {
animatedValue.__onAnimatedValueUpdateReceived(value);

// Once the JS side node is synced with the updated values, trigger an
// update on the AnimatedProps nodes to call any registered callbacks.
this.__findAnimatedPropsNodes(animatedValue).forEach(node =>
node.update(),
);
}
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ export default class DecayAnimation extends Animation {
this._onUpdate = onUpdate;
this.__onEnd = onEnd;
this._startTime = Date.now();

if (!this._useNativeDriver && animatedValue.__isNative === true) {
throw new Error(
'Attempting to run JS driven animation on animated node ' +
'that has been moved to "native" earlier by starting an ' +
'animation with `useNativeDriver: true`',
);
}

if (this._useNativeDriver) {
this.__startNativeAnimation(animatedValue);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ export default class SpringAnimation extends Animation {
}

const start = () => {
if (!this._useNativeDriver && animatedValue.__isNative === true) {
throw new Error(
'Attempting to run JS driven animation on animated node ' +
'that has been moved to "native" earlier by starting an ' +
'animation with `useNativeDriver: true`',
);
}

if (this._useNativeDriver) {
this.__startNativeAnimation(animatedValue);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ export default class TimingAnimation extends Animation {
this.__onEnd = onEnd;

const start = () => {
if (!this._useNativeDriver && animatedValue.__isNative === true) {
throw new Error(
'Attempting to run JS driven animation on animated node ' +
'that has been moved to "native" earlier by starting an ' +
'animation with `useNativeDriver: true`',
);
}

// Animations that sometimes have 0 duration and sometimes do not
// still need to use the native driver when duration is 0 so as to
// not cause intermixed JS and native animations.
Expand Down
10 changes: 3 additions & 7 deletions packages/react-native/Libraries/Animated/useAnimatedProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export default function useAnimatedProps<TProps: {...}, TInstance>(
// changes), but `setNativeView` already optimizes for that.
node.setNativeView(instance);

// NOTE: This callback is only used by the JavaScript animation driver.
// NOTE: When using the JS animation driver, this callback is called on
// every animation frame. When using the native driver, this callback is
// called when the animation completes.
onUpdateRef.current = () => {
if (
process.env.NODE_ENV === 'test' ||
Expand All @@ -82,12 +84,6 @@ export default function useAnimatedProps<TProps: {...}, TInstance>(
// $FlowIgnore[not-a-function] - Assume it's still a function.
// $FlowFixMe[incompatible-use]
instance.setNativeProps(node.__getAnimatedValue());
} else {
throw new Error(
'Attempting to run JS driven animation on animated node ' +
'that has been moved to "native" earlier by starting an ' +
'animation with `useNativeDriver: true`',
);
}
};

Expand Down

0 comments on commit c870a52

Please sign in to comment.