From a4f78b3d0748fbc5e2acf2fc29ce6a2cf603527e Mon Sep 17 00:00:00 2001 From: Zeya Peng Date: Thu, 16 Apr 2026 08:53:31 -0700 Subject: [PATCH] Allow waiting for user land Animated animations before firing transition complete callback (#56459) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56459 ## Changelog: [Internal] [Added] - Allow waiting for user land Animated animations before firing transition complete callback Instead of calling `onCompleteCallback` synchronously at the end of `startViewTransition`, defer it until all animations have finished. Since transition animation can be kicked off in user land from view transition event handlers, we need extra APIs to signal to native side when animation starts and finishes. This ensures the transition lifecycle properly waits for native animated transitions to complete. - Add `waitForTransitionAnimation` / `transitionAnimationFinished` to the delegate interface and expose via `NativeViewTransition` TurboModule - JS calls `waitForTransitionAnimation(animationId)` before starting each animation and `transitionAnimationFinished(animationId)` in the completion callback - `ViewTransitionModule` tracks pending animation IDs in a `std::set` and fires `onCompleteCallback_` only when all animations are done - only track and wait for animations started during transition-ready callback - if no animation requests to be waited for, resolve complete promise immediately when transition-ready callback finishes Reviewed By: sammy-SC Differential Revision: D99366980 fbshipit-source-id: 6313bdd2b3f7b90a5aee24e0faec905bce75d715 --- .../viewtransition/NativeViewTransition.cpp | 22 +++++++++++++++ .../viewtransition/NativeViewTransition.h | 4 +++ .../UIManagerViewTransitionDelegate.h | 4 +++ .../viewtransition/ViewTransitionModule.cpp | 28 ++++++++++++++++--- .../viewtransition/ViewTransitionModule.h | 10 +++++++ .../specs/NativeViewTransition.js | 2 ++ 6 files changed, 66 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactCommon/react/nativemodule/viewtransition/NativeViewTransition.cpp b/packages/react-native/ReactCommon/react/nativemodule/viewtransition/NativeViewTransition.cpp index 8190c7c2f76..6add3a25cdf 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/viewtransition/NativeViewTransition.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/viewtransition/NativeViewTransition.cpp @@ -67,4 +67,26 @@ jsi::Value NativeViewTransition::findPseudoElementShadowNodeByTag( return jsi::Value::null(); } +void NativeViewTransition::waitForTransitionAnimation( + jsi::Runtime& rt, + double animationId) { + auto& uiManager = UIManagerBinding::getBinding(rt)->getUIManager(); + auto* viewTransitionDelegate = uiManager.getViewTransitionDelegate(); + if (viewTransitionDelegate != nullptr) { + viewTransitionDelegate->waitForTransitionAnimation( + static_cast(animationId)); + } +} + +void NativeViewTransition::transitionAnimationFinished( + jsi::Runtime& rt, + double animationId) { + auto& uiManager = UIManagerBinding::getBinding(rt)->getUIManager(); + auto* viewTransitionDelegate = uiManager.getViewTransitionDelegate(); + if (viewTransitionDelegate != nullptr) { + viewTransitionDelegate->transitionAnimationFinished( + static_cast(animationId)); + } +} + } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/viewtransition/NativeViewTransition.h b/packages/react-native/ReactCommon/react/nativemodule/viewtransition/NativeViewTransition.h index f3311f40bef..55593f7c2f6 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/viewtransition/NativeViewTransition.h +++ b/packages/react-native/ReactCommon/react/nativemodule/viewtransition/NativeViewTransition.h @@ -28,6 +28,10 @@ class NativeViewTransition : public NativeViewTransitionCxxSpec onCompleteCallback; }; std::queue pendingTransitions_{}; + + // Tracks animation IDs that must complete before onCompleteCallback_ fires. + // Animations are registered via waitForTransitionAnimation (called from JS + // after connectAnimatedNodeToView) and removed via transitionAnimationFinished. + std::unordered_set pendingAnimationIds_{}; + std::function onCompleteCallback_{nullptr}; }; } // namespace facebook::react diff --git a/packages/react-native/src/private/viewtransition/specs/NativeViewTransition.js b/packages/react-native/src/private/viewtransition/specs/NativeViewTransition.js index c9fa79cd363..c3bebd47352 100644 --- a/packages/react-native/src/private/viewtransition/specs/NativeViewTransition.js +++ b/packages/react-native/src/private/viewtransition/specs/NativeViewTransition.js @@ -24,6 +24,8 @@ export interface Spec extends TurboModule { nativeTag: number, }; +findPseudoElementShadowNodeByTag: (reactTag: number) => ?unknown /* Node */; + +waitForTransitionAnimation: (animationId: number) => void; + +transitionAnimationFinished: (animationId: number) => void; } export default TurboModuleRegistry.get(