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
This commit is contained in:
Zeya Peng authored and meta-codesync[bot] committed 2026-04-16 06:53:53 -07:00
1 parent a9d567c2d9
commit a6f2697f72
4 files changed
+156 -18

No files matched your search

@@ -160,9 +160,8 @@ Scheduler::Scheduler(
// Initialize ViewTransitionModule
if (ReactNativeFeatureFlags::viewTransitionEnabled()) {
viewTransitionModule_ = std::make_unique<ViewTransitionModule>();
viewTransitionModule_->setUIManager(uiManager_.get());
uiManager_->setViewTransitionDelegate(viewTransitionModule_.get());
viewTransitionModule_ = std::make_shared<ViewTransitionModule>();
viewTransitionModule_->initialize(uiManager_.get(), viewTransitionModule_);
}
uiManager->registerMountHook(*eventPerformanceLogger_);
@@ -147,7 +147,7 @@ class Scheduler final : public UIManagerDelegate {
RuntimeScheduler *runtimeScheduler_{nullptr};
std::unique_ptr<ViewTransitionModule> viewTransitionModule_;
std::shared_ptr<ViewTransitionModule> viewTransitionModule_;
mutable std::shared_mutex onSurfaceStartCallbackMutex_;
OnSurfaceStartCallback onSurfaceStartCallback_;
@@ -7,14 +7,51 @@
#include "ViewTransitionModule.h"
#include <react/renderer/components/root/RootShadowNode.h>
#include <react/renderer/core/LayoutableShadowNode.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/mounting/MountingTransaction.h>
#include <react/renderer/mounting/ShadowTree.h>
#include <react/renderer/uimanager/UIManager.h>
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<ViewTransitionModule> 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<Tag> existingTags;
existingTags.reserve(existingChildren.size());
for (const auto& child : existingChildren) {
existingTags.insert(child->getTag());
}
auto newChildren =
std::make_shared<std::vector<std::shared_ptr<const ShadowNode>>>(
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<RootShadowNode>(
*newRootShadowNode,
ShadowNodeFragment{
.props = ShadowNodeFragment::propsPlaceholder(),
.children = newChildren,
});
}
bool ViewTransitionModule::shouldOverridePullTransaction() const {
return !oldPseudoElementNodesRepository_.empty();
}
std::optional<MountingTransaction> 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(
@@ -11,18 +11,25 @@
#include <react/renderer/core/LayoutMetrics.h>
#include <react/renderer/core/ShadowNode.h>
#include <react/renderer/mounting/MountingOverrideDelegate.h>
#include <react/renderer/mounting/ShadowViewMutation.h>
#include <react/renderer/uimanager/UIManagerCommitHook.h>
#include <react/renderer/uimanager/UIManagerViewTransitionDelegate.h>
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<ViewTransitionModule> 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<ViewTransitionInstance> 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<MountingTransaction> 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<Tag, std::unordered_set<std::string>> 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<std::string, std::shared_ptr<const ShadowNode>> oldPseudoElementNodes_{};
// will be restored into oldPseudoElementNodes_ in next transition
std::unordered_map<std::string, std::shared_ptr<const ShadowNode>> oldPseudoElementNodesForNextTransition_{};
struct InactivePseudoElement {
std::shared_ptr<const ShadowNode> 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<std::string, InactivePseudoElement> oldPseudoElementNodesRepository_{};
LayoutMetrics captureLayoutMetricsFromRoot(const ShadowNode &shadowNode);