Skip to content

Commit 9cd7de1

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

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-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

+44
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@ import {
1616
legacySendAccessibilityEvent,
1717
getNodeFromPublicInstance,
1818
getNativeTagFromPublicInstance,
19+
getInternalInstanceHandleFromPublicInstance,
1920
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';
2021

2122
import {
2223
findHostInstance,
2324
findHostInstanceWithWarning,
2425
} from 'react-reconciler/src/ReactFiberReconciler';
26+
import {doesFiberContain} from 'react-reconciler/src/ReactFiberTreeReflection';
2527
import ReactSharedInternals from 'shared/ReactSharedInternals';
2628
import getComponentNameFromType from 'shared/getComponentNameFromType';
2729

30+
import {PublicInstance as FabricPublicInstance} from './ReactFiberConfigFabric';
31+
import {PublicInstance as PaperPublicInstance} from './ReactFiberConfigNative';
32+
2833
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
2934

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

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)