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,15 +18,19 @@ 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
32+ import ReactNativeFiberHostComponent from './ReactNativeFiberHostComponent' ;
33+
2834const ReactCurrentOwner = ReactSharedInternals . ReactCurrentOwner ;
2935
3036export function findHostInstance_DEPRECATED < TElementType : ElementType > (
@@ -218,3 +224,44 @@ export function getNodeFromInternalInstanceHandle(
218224 internalInstanceHandle . stateNode . node
219225 ) ;
220226}
227+
228+ // Remove this once Paper is no longer supported and DOM Node API are enabled by default in RN.
229+ export function isChildPublicInstance (
230+ parentInstance : FabricPublicInstance | PaperPublicInstance ,
231+ childInstance : FabricPublicInstance | PaperPublicInstance ,
232+ ) : boolean {
233+ if ( __DEV__ ) {
234+ const parentInternalInstanceHandle =
235+ // $FlowExpectedError[incompatible-call] Prioritizing early return for Fabric case, without explicitly checking with instanceof to make Flow happy.
236+ getInternalInstanceHandleFromPublicInstance ( parentInstance ) ;
237+ const childInternalInstanceHandle =
238+ // $FlowExpectedError[incompatible-call] Prioritizing early return for Fabric case, without explicitly checking with instanceof to make Flow happy.
239+ getInternalInstanceHandleFromPublicInstance ( childInstance ) ;
240+
241+ // Fabric
242+ if (
243+ parentInternalInstanceHandle != null &&
244+ childInternalInstanceHandle != null
245+ ) {
246+ return doesFiberContain (
247+ parentInternalInstanceHandle ,
248+ childInternalInstanceHandle ,
249+ ) ;
250+ }
251+
252+ // Paper
253+ if (
254+ parentInstance instanceof ReactNativeFiberHostComponent &&
255+ childInstance instanceof ReactNativeFiberHostComponent
256+ ) {
257+ return doesFiberContain (
258+ parentInstance . _internalFiberInstanceHandleDEV ,
259+ childInstance . _internalFiberInstanceHandleDEV ,
260+ ) ;
261+ }
262+
263+ return false ;
264+ } else {
265+ throw new Error ( 'isChildPublicInstance() is not available in production.' ) ;
266+ }
267+ }
0 commit comments