Allow pseudo-element shadow node lookup in JS (#56455)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/56455

## Changelog:

[Internal] [Added] - Allow pseudo-element shadow node lookup in JS

Add `findPseudoElementShadowNodeByTag` to look up pseudo-element shadow nodes by tag from JS, and update the animation helper to connect animated nodes to pseudo-element shadow nodes for old-element animations.

Key changes:
- Add `findPseudoElementShadowNodeByTag(tag)` virtual method to `UIManagerViewTransitionDelegate` and implement in `ViewTransitionModule` (linear scan over `oldPseudoElementNodes_`)
- Expose the method via `NativeViewTransition` TurboModule so JS can resolve a pseudo-element's shadow node by its native tag
- Add `findPseudoElementShadowNodeByTag` to `NativeViewTransition.js` specs

Reviewed By: sammy-SC

Differential Revision: D98982251

fbshipit-source-id: c4abaae5f5a62d2b28ea9e2831f469abcf58fe30
This commit is contained in:
Zeya Peng authored and meta-codesync[bot] committed 2026-04-16 08:53:31 -07:00
1 parent a6df56e22e
commit 986be4cc71
6 files changed
+63 -2

No files matched your search

@@ -51,4 +51,20 @@ std::optional<jsi::Object> NativeViewTransition::getViewTransitionInstance(
return result;
}
jsi::Value NativeViewTransition::findPseudoElementShadowNodeByTag(
jsi::Runtime& rt,
double reactTag) {
auto& uiManager = UIManagerBinding::getBinding(rt)->getUIManager();
auto* viewTransitionDelegate = uiManager.getViewTransitionDelegate();
if (viewTransitionDelegate != nullptr) {
auto shadowNode = viewTransitionDelegate->findPseudoElementShadowNodeByTag(
static_cast<Tag>(reactTag));
if (shadowNode) {
return Bridging<std::shared_ptr<const ShadowNode>>::toJs(rt, shadowNode);
}
}
return jsi::Value::null();
}
} // namespace facebook::react
@@ -26,6 +26,8 @@ class NativeViewTransition : public NativeViewTransitionCxxSpec<NativeViewTransi
std::optional<jsi::Object>
getViewTransitionInstance(jsi::Runtime &rt, const std::string &name, const std::string &pseudo);
jsi::Value findPseudoElementShadowNodeByTag(jsi::Runtime &rt, double reactTag);
};
} // namespace facebook::react
@@ -57,6 +57,16 @@ class UIManagerViewTransitionDelegate {
{
return std::nullopt;
}
// Similar to UIManager::findShadowNodeByTag, but searches all direct children
// of the root node (where pseudo-element nodes live) rather than just the
// first child. Pseudo-element nodes are appended as additional children of the
// root node, rather than inserted into the main React tree, to avoid
// disrupting the user-created component tree.
virtual std::shared_ptr<const ShadowNode> findPseudoElementShadowNodeByTag(Tag /*tag*/) const
{
return nullptr;
}
};
} // namespace facebook::react
@@ -227,6 +227,36 @@ std::optional<MountingTransaction> ViewTransitionModule::pullTransaction(
surfaceId, number, std::move(mutations), telemetry};
}
std::shared_ptr<const ShadowNode>
ViewTransitionModule::findPseudoElementShadowNodeByTag(Tag tag) const {
if (uiManager_ == nullptr) {
return nullptr;
}
auto shadowNode = std::shared_ptr<const ShadowNode>{};
uiManager_->getShadowTreeRegistry().enumerate(
[&](const ShadowTree& shadowTree, bool& stop) {
const auto rootShadowNode =
shadowTree.getCurrentRevision().rootShadowNode;
if (rootShadowNode != nullptr) {
const auto& children = rootShadowNode->getChildren();
// Pseudo element nodes are appended after the first child (the main
// React tree), so iterate from index 1 onwards.
for (size_t i = 1; i < children.size(); ++i) {
if (children[i]->getTag() == tag) {
shadowNode = children[i];
stop = true;
return;
}
}
}
});
return shadowNode;
}
void ViewTransitionModule::cancelViewTransitionName(
const ShadowNode& shadowNode,
const std::string& name) {
@@ -76,6 +76,8 @@ class ViewTransitionModule : public UIManagerViewTransitionDelegate,
const TransactionTelemetry &telemetry,
ShadowViewMutationList mutations) const override;
std::shared_ptr<const ShadowNode> findPseudoElementShadowNodeByTag(Tag tag) const override;
// Animation state structure for storing minimal view data
struct AnimationKeyFrameViewLayoutMetrics {
Point originFromRoot;
@@ -102,8 +104,8 @@ 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, appended to/removed from root children at next ShadowTree commit
// TODO: T262559264 should be cleaned up from ShadowTree as soon as transition animation ends
// pseudo-element nodes keyed by transition name, appended to root children via UIManagerCommitHook
// TODO: T262559264 pseudo elements should be cleaned up as soon as transition animation ends
std::unordered_map<std::string, std::shared_ptr<const ShadowNode>> oldPseudoElementNodes_{};
struct InactivePseudoElement {
@@ -23,6 +23,7 @@ export interface Spec extends TurboModule {
height: number,
nativeTag: number,
};
+findPseudoElementShadowNodeByTag: (reactTag: number) => ?unknown /* Node */;
}
export default TurboModuleRegistry.get<Spec>(