Skip to content

Commit

Permalink
feat[Fabric/Paper]: support getIsOneInstanceAncestorOfAnother api method
Browse files Browse the repository at this point in the history
  • Loading branch information
hoxyq committed Dec 4, 2023
1 parent 87cb0bf commit 9cd7de1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/react-native-renderer/src/ReactFabric.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
dispatchCommand,
sendAccessibilityEvent,
getNodeFromInternalInstanceHandle,
getIsOneInstanceAncestorOfAnother,
} from './ReactNativePublicCompat';
import {getPublicInstanceFromInternalInstanceHandle} from './ReactFiberConfigFabric';

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

injectIntoDevTools({
Expand Down
44 changes: 44 additions & 0 deletions packages/react-native-renderer/src/ReactNativePublicCompat.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ import {
legacySendAccessibilityEvent,
getNodeFromPublicInstance,
getNativeTagFromPublicInstance,
getInternalInstanceHandleFromPublicInstance,
} from 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface';

import {
findHostInstance,
findHostInstanceWithWarning,
} from 'react-reconciler/src/ReactFiberReconciler';
import {doesFiberContain} from 'react-reconciler/src/ReactFiberTreeReflection';
import ReactSharedInternals from 'shared/ReactSharedInternals';
import getComponentNameFromType from 'shared/getComponentNameFromType';

import {PublicInstance as FabricPublicInstance} from './ReactFiberConfigFabric';
import {PublicInstance as PaperPublicInstance} from './ReactFiberConfigNative';

const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;

export function findHostInstance_DEPRECATED<TElementType: ElementType>(
Expand Down Expand Up @@ -218,3 +223,42 @@ export function getNodeFromInternalInstanceHandle(
internalInstanceHandle.stateNode.node
);
}

// Remove this once Paper is no longer supported and DOM Node API are enabled by default in RN.
export function getIsOneInstanceAncestorOfAnother(
parentInstance: FabricPublicInstance | PaperPublicInstance,
childInstance: FabricPublicInstance | PaperPublicInstance,
): boolean {
if (__DEV__) {
const parentInternalInstanceHandle =
getInternalInstanceHandleFromPublicInstance(parentInstance);
const childInternalInstanceHandle =
getInternalInstanceHandleFromPublicInstance(childInstance);

// Fabric
if (
parentInternalInstanceHandle != null &&
childInternalInstanceHandle != null
) {
return doesFiberContain(
parentInternalInstanceHandle,
childInternalInstanceHandle,
);
}

// Paper
if (
parentInstance._internalFiberInstanceHandleDEV != null &&
childInstance._internalFiberInstanceHandleDEV != null
) {
return doesFiberContain(
parentInstance._internalFiberInstanceHandleDEV,
childInstance._internalFiberInstanceHandleDEV,
);
}

return false;
} else {
throw new Error('getIs() is not available in production.');
}
}
3 changes: 3 additions & 0 deletions packages/react-native-renderer/src/ReactNativeRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
findNodeHandle,
dispatchCommand,
sendAccessibilityEvent,
getIsOneInstanceAncestorOfAnother,
} from './ReactNativePublicCompat';

// $FlowFixMe[missing-local-annot]
Expand Down Expand Up @@ -137,6 +138,8 @@ export {
// This export is typically undefined in production builds.
// See the "enableGetInspectorDataForInstanceInProduction" flag.
getInspectorDataForInstance,
// DEV-only:
getIsOneInstanceAncestorOfAnother,
};

injectIntoDevTools({
Expand Down
3 changes: 3 additions & 0 deletions scripts/flow/react-native-host-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ declare module 'react-native/Libraries/ReactPrivate/ReactNativePrivateInterface'
declare export function createPublicTextInstance(
internalInstanceHandle: mixed,
): PublicTextInstance;
declare export function getInternalInstanceHandleFromPublicInstance(
publicInstance: PublicInstance,
): mixed;
}

declare module 'react-native/Libraries/ReactPrivate/ReactNativePrivateInitializeCore' {
Expand Down

0 comments on commit 9cd7de1

Please sign in to comment.