8
8
*/
9
9
10
10
import type { Node , HostComponent } from './ReactNativeTypes' ;
11
+ import type { PublicInstance as FabricPublicInstance } from './ReactFiberConfigFabric' ;
12
+ import type { PublicInstance as PaperPublicInstance } from './ReactFiberConfigNative' ;
11
13
import type { ElementRef , ElementType } from 'react' ;
12
14
13
15
// Modules provided by RN:
@@ -16,15 +18,19 @@ import {
16
18
legacySendAccessibilityEvent ,
17
19
getNodeFromPublicInstance ,
18
20
getNativeTagFromPublicInstance ,
21
+ getInternalInstanceHandleFromPublicInstance ,
19
22
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface' ;
20
23
21
24
import {
22
25
findHostInstance ,
23
26
findHostInstanceWithWarning ,
24
27
} from 'react-reconciler/src/ReactFiberReconciler' ;
28
+ import { doesFiberContain } from 'react-reconciler/src/ReactFiberTreeReflection' ;
25
29
import ReactSharedInternals from 'shared/ReactSharedInternals' ;
26
30
import getComponentNameFromType from 'shared/getComponentNameFromType' ;
27
31
32
+ import ReactNativeFiberHostComponent from './ReactNativeFiberHostComponent' ;
33
+
28
34
const ReactCurrentOwner = ReactSharedInternals . ReactCurrentOwner ;
29
35
30
36
export function findHostInstance_DEPRECATED < TElementType : ElementType > (
@@ -218,3 +224,46 @@ export function getNodeFromInternalInstanceHandle(
218
224
internalInstanceHandle . stateNode . node
219
225
) ;
220
226
}
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 (
266
+ 'isChildPublicInstance() is not available in production.' ,
267
+ ) ;
268
+ }
269
+ }
0 commit comments