From a6f2697f725308709f7ebba886ad59f7cfc8bcc0 Mon Sep 17 00:00:00 2001 From: Zeya Peng Date: Thu, 16 Apr 2026 06:53:53 -0700 Subject: [PATCH] Append pseudo-element shadow nodes to root at commit (#56457) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56457 ## Changelog: [Internal] [Added] - Append pseudo-element shadow nodes to root at commit Append view transition pseudo-element shadow nodes to the root's children at commit time (`shadowTreeWillCommit`), so they are committed into the shadow tree and rendered by the platform. Key changes: - Add `getPseudoElementNodes(surfaceId)` virtual method to `UIManagerViewTransitionDelegate`, returning pseudo-element shadow nodes filtered by surface ID - Implement the method in `ViewTransitionModule`, iterating over `oldPseudoElementNodes_` and collecting nodes matching the given surface - In `shadowTreeWillCommit`, when view transitions are enabled, query the delegate for pseudo-element nodes and insert them at the end of `rootChildren` before committing This ensures pseudo-element nodes (created by `createViewTransitionInstance`) are included in the committed shadow tree and ultimately mounted as platform views that display old-element snapshots during transitions. ## alternatives considered we could also create pseudo element shadow node at React level, but (1) it doesn't make sense to have pseudo element Fiber node type in React just for RN use case, since web doesn't have it, (2) there's no React component mapped to the pseudo element on web, so it's more like something managed on the platform Reviewed By: sammy-SC Differential Revision: D98982122 fbshipit-source-id: 2c52ddf40ef9953d1c59592e001066c7c27fd530 --- .../react/renderer/scheduler/Scheduler.cpp | 5 +- .../react/renderer/scheduler/Scheduler.h | 2 +- .../viewtransition/ViewTransitionModule.cpp | 121 ++++++++++++++++-- .../viewtransition/ViewTransitionModule.h | 46 ++++++- 4 files changed, 156 insertions(+), 18 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.cpp b/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.cpp index 3a1393ef40b..477ecb8a882 100644 --- a/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.cpp +++ b/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.cpp @@ -160,9 +160,8 @@ Scheduler::Scheduler( // Initialize ViewTransitionModule if (ReactNativeFeatureFlags::viewTransitionEnabled()) { - viewTransitionModule_ = std::make_unique(); - viewTransitionModule_->setUIManager(uiManager_.get()); - uiManager_->setViewTransitionDelegate(viewTransitionModule_.get()); + viewTransitionModule_ = std::make_shared(); + viewTransitionModule_->initialize(uiManager_.get(), viewTransitionModule_); } uiManager->registerMountHook(*eventPerformanceLogger_); diff --git a/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.h b/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.h index ad16e3e4087..00ed0f43ed0 100644 --- a/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.h +++ b/packages/react-native/ReactCommon/react/renderer/scheduler/Scheduler.h @@ -147,7 +147,7 @@ class Scheduler final : public UIManagerDelegate { RuntimeScheduler *runtimeScheduler_{nullptr}; - std::unique_ptr viewTransitionModule_; + std::shared_ptr viewTransitionModule_; mutable std::shared_mutex onSurfaceStartCallbackMutex_; OnSurfaceStartCallback onSurfaceStartCallback_; diff --git a/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.cpp b/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.cpp index 5fd165304b3..30a891fd83c 100644 --- a/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.cpp +++ b/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.cpp @@ -7,14 +7,51 @@ #include "ViewTransitionModule.h" +#include #include #include +#include +#include #include namespace facebook::react { -void ViewTransitionModule::setUIManager(UIManager* uiManager) { +ViewTransitionModule::~ViewTransitionModule() { + if (uiManager_ != nullptr) { + if (uiManager_->getViewTransitionDelegate() == this) { + uiManager_->setViewTransitionDelegate(nullptr); + } + uiManager_->unregisterCommitHook(*this); + uiManager_ = nullptr; + } +} + +void ViewTransitionModule::initialize( + UIManager* uiManager, + std::weak_ptr weakThis) { + if (uiManager_ != nullptr) { + uiManager_->unregisterCommitHook(*this); + } uiManager_ = uiManager; + if (uiManager_ != nullptr) { + uiManager_->registerCommitHook(*this); + + // Register as MountingOverrideDelegate on existing surfaces + uiManager_->getShadowTreeRegistry().enumerate( + [weakThis](const ShadowTree& shadowTree, bool& /*stop*/) { + shadowTree.getMountingCoordinator()->setMountingOverrideDelegate( + weakThis); + }); + + // Register on surfaces started in the future + uiManager_->setOnSurfaceStartCallback( + [weakThis](const ShadowTree& shadowTree) { + shadowTree.getMountingCoordinator()->setMountingOverrideDelegate( + weakThis); + }); + + uiManager_->setViewTransitionDelegate(this); + } } void ViewTransitionModule::applyViewTransitionName( @@ -47,11 +84,9 @@ void ViewTransitionModule::applyViewTransitionName( // TODO: capture bitmap snapshot of old view via platform - if (auto it = oldPseudoElementNodesForNextTransition_.find(name); - it != oldPseudoElementNodesForNextTransition_.end()) { - auto pseudoElementNode = it->second; - oldPseudoElementNodes_[name] = pseudoElementNode; - oldPseudoElementNodesForNextTransition_.erase(it); + if (auto it = oldPseudoElementNodesRepository_.find(name); + it != oldPseudoElementNodesRepository_.end()) { + oldPseudoElementNodes_[name] = it->second.node; } } else { @@ -115,11 +150,81 @@ void ViewTransitionModule::createViewTransitionInstance( if (pseudoElementNode != nullptr) { if (!forNextTransition) { oldPseudoElementNodes_[name] = pseudoElementNode; - } else { - oldPseudoElementNodesForNextTransition_[name] = pseudoElementNode; + } + oldPseudoElementNodesRepository_[name] = InactivePseudoElement{ + .node = pseudoElementNode, .sourceTag = view.tag}; + } + } +} + +RootShadowNode::Unshared ViewTransitionModule::shadowTreeWillCommit( + const ShadowTree& shadowTree, + const RootShadowNode::Shared& /*oldRootShadowNode*/, + const RootShadowNode::Unshared& newRootShadowNode, + const ShadowTreeCommitOptions& /*commitOptions*/) noexcept { + if (oldPseudoElementNodes_.empty()) { + return newRootShadowNode; + } + + auto surfaceId = shadowTree.getSurfaceId(); + + // Collect pseudo-element nodes for this surface, skipping any that are + // already present in the children list (from a previous commit hook run). + const auto& existingChildren = newRootShadowNode->getChildren(); + std::unordered_set existingTags; + existingTags.reserve(existingChildren.size()); + for (const auto& child : existingChildren) { + existingTags.insert(child->getTag()); + } + + auto newChildren = + std::make_shared>>( + existingChildren); + bool appended = false; + for (const auto& [name, node] : oldPseudoElementNodes_) { + if (node->getSurfaceId() == surfaceId && + existingTags.find(node->getTag()) == existingTags.end()) { + newChildren->push_back(node); + appended = true; + } + } + + if (!appended) { + return newRootShadowNode; + } + + return std::make_shared( + *newRootShadowNode, + ShadowNodeFragment{ + .props = ShadowNodeFragment::propsPlaceholder(), + .children = newChildren, + }); +} + +bool ViewTransitionModule::shouldOverridePullTransaction() const { + return !oldPseudoElementNodesRepository_.empty(); +} + +std::optional ViewTransitionModule::pullTransaction( + SurfaceId surfaceId, + MountingTransaction::Number number, + const TransactionTelemetry& telemetry, + ShadowViewMutationList mutations) const { + for (const auto& mutation : mutations) { + if (mutation.type == ShadowViewMutation::Delete) { + auto tag = mutation.oldChildShadowView.tag; + for (auto it = oldPseudoElementNodesRepository_.begin(); + it != oldPseudoElementNodesRepository_.end();) { + if (it->second.sourceTag == tag) { + it = oldPseudoElementNodesRepository_.erase(it); + } else { + ++it; + } } } } + return MountingTransaction{ + surfaceId, number, std::move(mutations), telemetry}; } void ViewTransitionModule::cancelViewTransitionName( diff --git a/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.h b/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.h index a076e3796ba..3a340a96883 100644 --- a/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.h +++ b/packages/react-native/ReactCommon/react/renderer/viewtransition/ViewTransitionModule.h @@ -11,18 +11,25 @@ #include #include +#include #include +#include #include namespace facebook::react { +class ShadowTree; class UIManager; -class ViewTransitionModule : public UIManagerViewTransitionDelegate { +class ViewTransitionModule : public UIManagerViewTransitionDelegate, + public UIManagerCommitHook, + public MountingOverrideDelegate { public: - ~ViewTransitionModule() override = default; + ~ViewTransitionModule() override; - void setUIManager(UIManager *uiManager); + void initialize(UIManager *uiManager, std::weak_ptr weakThis); + +#pragma mark - UIManagerViewTransitionDelegate // will be called when a view will transition. if a view already has a view-transition-name, it may not be called // again until it's removed @@ -50,6 +57,25 @@ class ViewTransitionModule : public UIManagerViewTransitionDelegate { std::optional getViewTransitionInstance(const std::string &name, const std::string &pseudo) override; +#pragma mark - UIManagerCommitHook + + void commitHookWasRegistered(const UIManager & /*uiManager*/) noexcept override {} + void commitHookWasUnregistered(const UIManager & /*uiManager*/) noexcept override {} + RootShadowNode::Unshared shadowTreeWillCommit( + const ShadowTree &shadowTree, + const RootShadowNode::Shared &oldRootShadowNode, + const RootShadowNode::Unshared &newRootShadowNode, + const ShadowTreeCommitOptions &commitOptions) noexcept override; + +#pragma mark - MountingOverrideDelegate + + bool shouldOverridePullTransaction() const override; + std::optional pullTransaction( + SurfaceId surfaceId, + MountingTransaction::Number number, + const TransactionTelemetry &telemetry, + ShadowViewMutationList mutations) const override; + // Animation state structure for storing minimal view data struct AnimationKeyFrameViewLayoutMetrics { Point originFromRoot; @@ -76,10 +102,18 @@ class ViewTransitionModule : public UIManagerViewTransitionDelegate { // used for cancel/restore viewTransitionName std::unordered_map> cancelledNameRegistry_{}; - // pseudo-element nodes keyed by transition name + // pseudo-element nodes keyed by transition name, appended to/removed from root children at next ShadowTree commit + // TODO: T262559264 should be cleaned up from ShadowTree as soon as transition animation ends std::unordered_map> oldPseudoElementNodes_{}; - // will be restored into oldPseudoElementNodes_ in next transition - std::unordered_map> oldPseudoElementNodesForNextTransition_{}; + + struct InactivePseudoElement { + std::shared_ptr node; + Tag sourceTag{0}; // tag of the original view this was created from + }; + // pseudo-element nodes created for entering nodes, to be copied into + // oldPseudoElementNodes_ during the next applyViewTransitionName call. + // Mutable because pullTransaction (const) needs to erase unmounted entries. + mutable std::unordered_map oldPseudoElementNodesRepository_{}; LayoutMetrics captureLayoutMetricsFromRoot(const ShadowNode &shadowNode);