88 */
99
1010import type { Node , HostComponent } from './ReactNativeTypes' ;
11+ import type { PublicInstance as FabricPublicInstance } from './ReactFiberConfigFabric' ;
12+ import type { PublicInstance as PaperPublicInstance } from './ReactFiberConfigNative' ;
1113import 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
2124import {
2225 findHostInstance ,
2326 findHostInstanceWithWarning ,
2427} from 'react-reconciler/src/ReactFiberReconciler' ;
28+ import { doesFiberContain } from 'react-reconciler/src/ReactFiberTreeReflection' ;
2529import ReactSharedInternals from 'shared/ReactSharedInternals' ;
2630import 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+ }
0 commit comments