Skip to content

Commit 1e7ff0c

Browse files
Bartlomiej Bloniarzfacebook-github-bot
authored andcommitted
Pass families to Native Animated (#54613)
Summary: This PR allows C++ Native Animated to use `ShadowNodeFamily` instances to use the `cloneMultiple` method when pushing updates through the `ShadowTree` in `AnimationBackend` # Changelog [General] [Added] - Add `connectAnimatedNodeToShadowNodeFamily` method to `NativeAnimatedModule` and `NativeAnimatedTurboModule` Differential Revision: D84055752
1 parent dba18bb commit 1e7ff0c

File tree

11 files changed

+226
-11
lines changed

11 files changed

+226
-11
lines changed

packages/react-native/Libraries/Animated/__tests__/AnimatedBackend-itest.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import ensureInstance from '../../../src/private/__tests__/utilities/ensureInsta
1717
import * as Fantom from '@react-native/fantom';
1818
import {createRef} from 'react';
1919
import {Animated, useAnimatedValue} from 'react-native';
20+
import {allowStyleProp} from 'react-native/Libraries/Animated/NativeAnimatedAllowlist';
2021
import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement';
2122

2223
test('animated opacity', () => {
@@ -73,3 +74,54 @@ test('animated opacity', () => {
7374
<rn-view opacity="0" />,
7475
);
7576
});
77+
78+
test('animate layout props', () => {
79+
const viewRef = createRef<HostInstance>();
80+
allowStyleProp('height');
81+
82+
let _animatedHeight;
83+
let _heightAnimation;
84+
85+
function MyApp() {
86+
const animatedHeight = useAnimatedValue(0);
87+
_animatedHeight = animatedHeight;
88+
return (
89+
<Animated.View
90+
ref={viewRef}
91+
style={[
92+
{
93+
width: 100,
94+
height: animatedHeight,
95+
},
96+
]}
97+
/>
98+
);
99+
}
100+
101+
const root = Fantom.createRoot();
102+
103+
Fantom.runTask(() => {
104+
root.render(<MyApp />);
105+
});
106+
107+
Fantom.runTask(() => {
108+
_heightAnimation = Animated.timing(_animatedHeight, {
109+
toValue: 100,
110+
duration: 10,
111+
useNativeDriver: true,
112+
}).start();
113+
});
114+
115+
Fantom.unstable_produceFramesForDuration(10);
116+
117+
// TODO: this shouldn't be neccessary since animation should be stopped after duration
118+
Fantom.runTask(() => {
119+
_heightAnimation?.stop();
120+
});
121+
122+
// TODO: getFabricUpdateProps is not working with the cloneMutliple method
123+
// expect(Fantom.unstable_getFabricUpdateProps(viewElement).height).toBe(100);
124+
expect(root.getRenderedOutput({props: ['height']}).toJSX()).toEqual(
125+
<rn-view height="100.000000" />,
126+
);
127+
});

packages/react-native/Libraries/Animated/nodes/AnimatedProps.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import type {AnimatedNodeConfig} from './AnimatedNode';
1414
import type {AnimatedStyleAllowlist} from './AnimatedStyle';
1515

1616
import NativeAnimatedHelper from '../../../src/private/animated/NativeAnimatedHelper';
17+
import * as ReactNativeFeatureFlags from '../../../src/private/featureflags/ReactNativeFeatureFlags';
1718
import {findNodeHandle} from '../../ReactNative/RendererProxy';
19+
import {getNodeFromPublicInstance} from '../../ReactPrivate/ReactNativePrivateInterface';
1820
import flattenStyle from '../../StyleSheet/flattenStyle';
1921
import {AnimatedEvent} from '../AnimatedEvent';
2022
import AnimatedNode from './AnimatedNode';
@@ -251,7 +253,9 @@ export default class AnimatedProps extends AnimatedNode {
251253
super.__setPlatformConfig(platformConfig);
252254

253255
if (this._target != null) {
254-
this.#connectAnimatedView(this._target);
256+
const target = this._target;
257+
this.#connectAnimatedView(target);
258+
this.#connectShadowNode(target);
255259
}
256260
}
257261
}
@@ -260,9 +264,10 @@ export default class AnimatedProps extends AnimatedNode {
260264
if (this._target?.instance === instance) {
261265
return;
262266
}
263-
this._target = {instance, connectedViewTag: null};
267+
const target = (this._target = {instance, connectedViewTag: null});
264268
if (this.__isNative) {
265-
this.#connectAnimatedView(this._target);
269+
this.#connectAnimatedView(target);
270+
this.#connectShadowNode(target);
266271
}
267272
}
268273

@@ -283,6 +288,27 @@ export default class AnimatedProps extends AnimatedNode {
283288
target.connectedViewTag = viewTag;
284289
}
285290

291+
#connectShadowNode(target: TargetView): void {
292+
if (
293+
!ReactNativeFeatureFlags.cxxNativeAnimatedEnabled() ||
294+
//eslint-disable-next-line
295+
!ReactNativeFeatureFlags.useSharedAnimatedBackend()
296+
) {
297+
return;
298+
}
299+
300+
invariant(this.__isNative, 'Expected node to be marked as "native"');
301+
// $FlowExpectedError[incompatible-type] - target.instance may be an HTMLElement but we need ReactNativeElement for Fabric
302+
const shadowNode = getNodeFromPublicInstance(target.instance);
303+
if (shadowNode == null) {
304+
return;
305+
}
306+
NativeAnimatedHelper.API.connectAnimatedNodeToShadowNodeFamily(
307+
this.__getNativeTag(),
308+
shadowNode,
309+
);
310+
}
311+
286312
#disconnectAnimatedView(target: TargetView): void {
287313
invariant(this.__isNative, 'Expected node to be marked as "native"');
288314
const viewTag = target.connectedViewTag;

packages/react-native/Libraries/NativeAnimation/RCTNativeAnimatedTurboModule.mm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <React/RCTInitializing.h>
1111
#import <React/RCTNativeAnimatedNodesManager.h>
1212
#import <React/RCTNativeAnimatedTurboModule.h>
13+
#import <react/debug/react_native_assert.h>
1314
#import <react/featureflags/ReactNativeFeatureFlags.h>
1415

1516
#import "RCTAnimationPlugins.h"
@@ -165,6 +166,12 @@ - (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter
165166
}];
166167
}
167168

169+
RCT_EXPORT_METHOD(connectAnimatedNodeToShadowNodeFamily : (double)nodeTag shadowNode : (NSDictionary *)shadowNode)
170+
{
171+
// This method should only be called when using CxxNativeAnimated
172+
react_native_assert(false);
173+
}
174+
168175
RCT_EXPORT_METHOD(disconnectAnimatedNodeFromView : (double)nodeTag viewTag : (double)viewTag)
169176
{
170177
[self queueOperationBlock:^(RCTNativeAnimatedNodesManager *nodesManager) {

packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
#include <glog/logging.h>
1111
#include <jsi/JSIDynamic.h>
12+
#include <react/renderer/bridging/bridging.h>
1213

1314
namespace facebook::react {
14-
1515
AnimatedModule::AnimatedModule(
1616
std::shared_ptr<CallInvoker> jsInvoker,
1717
std::shared_ptr<NativeAnimatedNodesManagerProvider> nodesManagerProvider)
@@ -160,6 +160,19 @@ void AnimatedModule::connectAnimatedNodeToView(
160160
ConnectAnimatedNodeToViewOp{.nodeTag = nodeTag, .viewTag = viewTag});
161161
}
162162

163+
void AnimatedModule::connectAnimatedNodeToShadowNodeFamily(
164+
jsi::Runtime& rt,
165+
Tag nodeTag,
166+
jsi::Object shadowNodeObj) {
167+
const auto& shadowNode = Bridging<std::shared_ptr<const ShadowNode>>::fromJs(
168+
rt, jsi::Value(rt, shadowNodeObj));
169+
170+
operations_.emplace_back(
171+
ConnectAnimatedNodeToShadowNodeFamilyOp{
172+
.nodeTag = nodeTag,
173+
.shadowNodeFamily = shadowNode->getFamilyShared()});
174+
}
175+
163176
void AnimatedModule::disconnectAnimatedNodeFromView(
164177
jsi::Runtime& /*rt*/,
165178
Tag nodeTag,
@@ -282,6 +295,11 @@ void AnimatedModule::executeOperation(
282295
DisconnectAnimatedNodeFromViewOp>) {
283296
nodesManager->disconnectAnimatedNodeFromView(
284297
op.nodeTag, op.viewTag);
298+
} else if constexpr (std::is_same_v<
299+
T,
300+
ConnectAnimatedNodeToShadowNodeFamilyOp>) {
301+
nodesManager->connectAnimatedNodeToShadowNodeFamily(
302+
op.nodeTag, op.shadowNodeFamily);
285303
} else if constexpr (std::is_same_v<T, RestoreDefaultValuesOp>) {
286304
nodesManager->restoreDefaultValues(op.nodeTag);
287305
} else if constexpr (std::is_same_v<T, DropAnimatedNodeOp>) {

packages/react-native/ReactCommon/react/renderer/animated/AnimatedModule.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class AnimatedModule : public NativeAnimatedModuleCxxSpec<AnimatedModule>, publi
8787
Tag viewTag{};
8888
};
8989

90+
struct ConnectAnimatedNodeToShadowNodeFamilyOp {
91+
Tag nodeTag{};
92+
std::shared_ptr<const ShadowNodeFamily> shadowNodeFamily{};
93+
};
94+
9095
struct DisconnectAnimatedNodeFromViewOp {
9196
Tag nodeTag{};
9297
Tag viewTag{};
@@ -124,6 +129,7 @@ class AnimatedModule : public NativeAnimatedModuleCxxSpec<AnimatedModule>, publi
124129
SetAnimatedNodeOffsetOp,
125130
SetAnimatedNodeValueOp,
126131
ConnectAnimatedNodeToViewOp,
132+
ConnectAnimatedNodeToShadowNodeFamilyOp,
127133
DisconnectAnimatedNodeFromViewOp,
128134
RestoreDefaultValuesOp,
129135
FlattenAnimatedNodeOffsetOp,
@@ -176,6 +182,8 @@ class AnimatedModule : public NativeAnimatedModuleCxxSpec<AnimatedModule>, publi
176182

177183
void connectAnimatedNodeToView(jsi::Runtime &rt, Tag nodeTag, Tag viewTag);
178184

185+
void connectAnimatedNodeToShadowNodeFamily(jsi::Runtime &rt, Tag nodeTag, jsi::Object shadowNode);
186+
179187
void disconnectAnimatedNodeFromView(jsi::Runtime &rt, Tag nodeTag, Tag viewTag);
180188

181189
void restoreDefaultValues(jsi::Runtime &rt, Tag nodeTag);

packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.cpp

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,20 @@ void NativeAnimatedNodesManager::connectAnimatedNodeToView(
238238
}
239239
}
240240

241+
void NativeAnimatedNodesManager::connectAnimatedNodeToShadowNodeFamily(
242+
Tag propsNodeTag,
243+
std::shared_ptr<const ShadowNodeFamily> family) noexcept {
244+
react_native_assert(propsNodeTag);
245+
auto node = getAnimatedNode<PropsAnimatedNode>(propsNodeTag);
246+
if (node != nullptr) {
247+
node->connectToFamily(family);
248+
updatedNodeTags_.insert(node->tag());
249+
} else {
250+
LOG(WARNING)
251+
<< "Cannot ConnectAnimatedNodeToShadowNodeFamily, animated node has to be props type";
252+
}
253+
}
254+
241255
void NativeAnimatedNodesManager::disconnectAnimatedNodeFromView(
242256
Tag propsNodeTag,
243257
Tag viewTag) noexcept {
@@ -886,10 +900,14 @@ void NativeAnimatedNodesManager::schedulePropsCommit(
886900
Tag viewTag,
887901
const folly::dynamic& props,
888902
bool layoutStyleUpdated,
889-
bool forceFabricCommit) noexcept {
903+
bool forceFabricCommit,
904+
std::shared_ptr<const ShadowNodeFamily> family) noexcept {
890905
if (ReactNativeFeatureFlags::useSharedAnimatedBackend()) {
891906
if (layoutStyleUpdated) {
892907
mergeObjects(updateViewProps_[viewTag], props);
908+
if (family) {
909+
tagToShadowNodeFamily_[viewTag] = std::move(family);
910+
}
893911
} else {
894912
mergeObjects(updateViewPropsDirect_[viewTag], props);
895913
}
@@ -997,7 +1015,31 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
9971015
AnimationMutation{tag, nullptr, propsBuilder.get()});
9981016
containsChange = true;
9991017
}
1000-
updateViewPropsDirect_.clear();
1018+
for (auto& [tag, props] : updateViewProps_) {
1019+
auto familyIt = tagToShadowNodeFamily_.find(tag);
1020+
if (familyIt != tagToShadowNodeFamily_.end()) {
1021+
if (props.find("width") != props.items().end()) {
1022+
propsBuilder.setWidth(
1023+
yoga::Style::SizeLength::points(props["width"].asDouble()));
1024+
}
1025+
if (props.find("height") != props.items().end()) {
1026+
propsBuilder.setHeight(
1027+
yoga::Style::SizeLength::points(props["height"].asDouble()));
1028+
}
1029+
// propsBuilder.storeDynamic(props);
1030+
mutations.push_back(
1031+
AnimationMutation{
1032+
.tag = tag,
1033+
.family = familyIt->second.get(),
1034+
.props = propsBuilder.get()});
1035+
}
1036+
containsChange = true;
1037+
}
1038+
if (containsChange) {
1039+
updateViewPropsDirect_.clear();
1040+
updateViewProps_.clear();
1041+
tagToShadowNodeFamily_.clear();
1042+
}
10011043
}
10021044

10031045
if (!containsChange) {
@@ -1017,7 +1059,8 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
10171059
}
10181060
}
10191061

1020-
// Step 2: update all nodes that are connected to the finished animations.
1062+
// Step 2: update all nodes that are connected to the finished
1063+
// animations.
10211064
updateNodes(finishedAnimationValueNodes);
10221065

10231066
isEventAnimationInProgress_ = false;
@@ -1028,6 +1071,17 @@ AnimationMutations NativeAnimatedNodesManager::pullAnimationMutations() {
10281071
mutations.push_back(
10291072
AnimationMutation{tag, nullptr, propsBuilder.get()});
10301073
}
1074+
for (auto& [tag, props] : updateViewProps_) {
1075+
auto familyIt = tagToShadowNodeFamily_.find(tag);
1076+
if (familyIt != tagToShadowNodeFamily_.end()) {
1077+
propsBuilder.storeDynamic(props);
1078+
mutations.push_back(
1079+
AnimationMutation{
1080+
.tag = tag,
1081+
.family = familyIt->second.get(),
1082+
.props = propsBuilder.get()});
1083+
}
1084+
}
10311085
}
10321086
} else {
10331087
// There is no active animation. Stop the render callback.
@@ -1107,7 +1161,8 @@ void NativeAnimatedNodesManager::onRender() {
11071161
}
11081162
}
11091163

1110-
// Step 2: update all nodes that are connected to the finished animations.
1164+
// Step 2: update all nodes that are connected to the finished
1165+
// animations.
11111166
updateNodes(finishedAnimationValueNodes);
11121167

11131168
isEventAnimationInProgress_ = false;

packages/react-native/ReactCommon/react/renderer/animated/NativeAnimatedNodesManager.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <react/renderer/animationbackend/AnimationBackend.h>
2222
#endif
2323
#include <react/renderer/core/ReactPrimitives.h>
24+
#include <react/renderer/core/ShadowNode.h>
2425
#include <react/renderer/uimanager/UIManagerAnimationBackend.h>
2526
#include <chrono>
2627
#include <memory>
@@ -101,6 +102,8 @@ class NativeAnimatedNodesManager {
101102

102103
void connectAnimatedNodeToView(Tag propsNodeTag, Tag viewTag) noexcept;
103104

105+
void connectAnimatedNodeToShadowNodeFamily(Tag propsNodeTag, std::shared_ptr<const ShadowNodeFamily> family) noexcept;
106+
104107
void disconnectAnimatedNodes(Tag parentTag, Tag childTag) noexcept;
105108

106109
void disconnectAnimatedNodeFromView(Tag propsNodeTag, Tag viewTag) noexcept;
@@ -150,7 +153,8 @@ class NativeAnimatedNodesManager {
150153
Tag viewTag,
151154
const folly::dynamic &props,
152155
bool layoutStyleUpdated,
153-
bool forceFabricCommit) noexcept;
156+
bool forceFabricCommit,
157+
std::shared_ptr<const ShadowNodeFamily> family = nullptr) noexcept;
154158

155159
/**
156160
* Commits all pending animated property updates to their respective views.
@@ -257,6 +261,7 @@ class NativeAnimatedNodesManager {
257261

258262
std::unordered_map<Tag, folly::dynamic> updateViewProps_{};
259263
std::unordered_map<Tag, folly::dynamic> updateViewPropsDirect_{};
264+
std::unordered_map<Tag, std::shared_ptr<const ShadowNodeFamily>> tagToShadowNodeFamily_{};
260265

261266
/*
262267
* Sometimes a view is not longer connected to a PropsAnimatedNode, but

packages/react-native/ReactCommon/react/renderer/animated/nodes/PropsAnimatedNode.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ void PropsAnimatedNode::connectToView(Tag viewTag) {
6262
connectedViewTag_ = viewTag;
6363
}
6464

65+
void PropsAnimatedNode::connectToFamily(
66+
std::shared_ptr<const ShadowNodeFamily>& family) {
67+
viewShadowNodeFamily_ = family;
68+
}
69+
6570
void PropsAnimatedNode::disconnectFromView(Tag viewTag) {
6671
react_native_assert(
6772
connectedViewTag_ == viewTag &&
@@ -148,7 +153,11 @@ void PropsAnimatedNode::update(bool forceFabricCommit) {
148153
layoutStyleUpdated_ = isLayoutStyleUpdated(getConfig()["props"], *manager_);
149154

150155
manager_->schedulePropsCommit(
151-
connectedViewTag_, props_, layoutStyleUpdated_, forceFabricCommit);
156+
connectedViewTag_,
157+
props_,
158+
layoutStyleUpdated_,
159+
forceFabricCommit,
160+
viewShadowNodeFamily_.lock());
152161
}
153162

154163
} // namespace facebook::react

0 commit comments

Comments
 (0)