Skip to content

Commit 852de7a

Browse files
committed
feat[Fabric/Paper]: support getIsOneInstanceAncestorOfAnother api method
1 parent 87cb0bf commit 852de7a

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

packages/react-native-renderer/src/ReactFabric.js

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
dispatchCommand,
4040
sendAccessibilityEvent,
4141
getNodeFromInternalInstanceHandle,
42+
getIsOneInstanceAncestorOfAnother,
4243
} from './ReactNativePublicCompat';
4344
import {getPublicInstanceFromInternalInstanceHandle} from './ReactFiberConfigFabric';
4445

@@ -128,6 +129,8 @@ export {
128129
// instance handles we use to dispatch events. This provides a way to access
129130
// the public instances we created from them (potentially created lazily).
130131
getPublicInstanceFromInternalInstanceHandle,
132+
// DEV-only:
133+
getIsOneInstanceAncestorOfAnother,
131134
};
132135

133136
injectIntoDevTools({

packages/react-native-renderer/src/ReactNativePublicCompat.js

+45
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
*/
99

1010
import type {Node, HostComponent} from './ReactNativeTypes';
11+
import type {PublicInstance as FabricPublicInstance} from './ReactFiberConfigFabric';
12+
import type {PublicInstance as PaperPublicInstance} from './ReactFiberConfigNative';
1113
import type {ElementRef, ElementType} from 'react';
1214

1315
// Modules provided by RN:
@@ -16,12 +18,14 @@ import {
1618
legacySendAccessibilityEvent,
1719
getNodeFromPublicInstance,
1820
getNativeTagFromPublicInstance,
21+
getInternalInstanceHandleFromPublicInstance,
1922
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
2023

2124
import {
2225
findHostInstance,
2326
findHostInstanceWithWarning,
2427
} from 'react-reconciler/src/ReactFiberReconciler';
28+
import {doesFiberContain} from 'react-reconciler/src/ReactFiberTreeReflection';
2529
import ReactSharedInternals from 'shared/ReactSharedInternals';
2630
import getComponentNameFromType from 'shared/getComponentNameFromType';
2731

@@ -218,3 +222,44 @@ export function getNodeFromInternalInstanceHandle(
218222
internalInstanceHandle.stateNode.node
219223
);
220224
}
225+
226+
// Remove this once Paper is no longer supported and DOM Node API are enabled by default in RN.
227+
export function getIsOneInstanceAncestorOfAnother(
228+
parentInstance: FabricPublicInstance | PaperPublicInstance,
229+
childInstance: FabricPublicInstance | PaperPublicInstance,
230+
): boolean {
231+
if (__DEV__) {
232+
const parentInternalInstanceHandle =
233+
getInternalInstanceHandleFromPublicInstance(parentInstance);
234+
const childInternalInstanceHandle =
235+
getInternalInstanceHandleFromPublicInstance(childInstance);
236+
237+
// Fabric
238+
if (
239+
parentInternalInstanceHandle != null &&
240+
childInternalInstanceHandle != null
241+
) {
242+
return doesFiberContain(
243+
parentInternalInstanceHandle,
244+
childInternalInstanceHandle,
245+
);
246+
}
247+
248+
// Paper
249+
if (
250+
parentInstance._internalFiberInstanceHandleDEV != null &&
251+
childInstance._internalFiberInstanceHandleDEV != null
252+
) {
253+
return doesFiberContain(
254+
parentInstance._internalFiberInstanceHandleDEV,
255+
childInstance._internalFiberInstanceHandleDEV,
256+
);
257+
}
258+
259+
return false;
260+
} else {
261+
throw new Error(
262+
'getIsOneInstanceAncestorOfAnother() is not available in production.',
263+
);
264+
}
265+
}

packages/react-native-renderer/src/ReactNativeRenderer.js

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
findNodeHandle,
4545
dispatchCommand,
4646
sendAccessibilityEvent,
47+
getIsOneInstanceAncestorOfAnother,
4748
} from './ReactNativePublicCompat';
4849

4950
// $FlowFixMe[missing-local-annot]
@@ -137,6 +138,8 @@ export {
137138
// This export is typically undefined in production builds.
138139
// See the "enableGetInspectorDataForInstanceInProduction" flag.
139140
getInspectorDataForInstance,
141+
// DEV-only:
142+
getIsOneInstanceAncestorOfAnother,
140143
};
141144

142145
injectIntoDevTools({

scripts/flow/react-native-host-hooks.js

+3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ declare module 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface'
159159
declare export function createPublicTextInstance(
160160
internalInstanceHandle: mixed,
161161
): PublicTextInstance;
162+
declare export function getInternalInstanceHandleFromPublicInstance(
163+
publicInstance: PublicInstance,
164+
): mixed;
162165
}
163166

164167
declare module 'react-native/Libraries/ReactPrivate/ReactNativePrivateInitializeCore' {

0 commit comments

Comments
 (0)