Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use public instance in Fiber renderer and expose it from getInspectorDataForViewAtPoint #31068

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions packages/react-devtools-shared/src/backend/fiber/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -763,16 +763,30 @@ const hostResourceToDevToolsInstanceMap: Map<
Set<DevToolsInstance>,
> = new Map();

// Ideally, this should be injected from Reconciler config
function getPublicInstance(instance: HostInstance): HostInstance {
// Typically the PublicInstance and HostInstance is the same thing but not in Fabric.
// So we need to detect this and use that as the public instance.
return typeof instance === 'object' &&
instance !== null &&
typeof instance.canonical === 'object'
? (instance.canonical: any)
: typeof instance._nativeTag === 'number'
? instance._nativeTag
: instance;

// React Native. Modern. Fabric.
if (typeof instance === 'object' && instance !== null) {
if (typeof instance.canonical === 'object' && instance.canonical !== null) {
if (
typeof instance.canonical.publicInstance === 'object' &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: you can reduce nesting here combining the conditions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I've decided to do nesting instead, because combining will produce a very long line, looks a bit uglier :(

instance.canonical.publicInstance !== null
Comment on lines +775 to +776
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a non-object? In Legacy Fabric, for example?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not AFAIK. And canonical is only available in Fabric.

) {
return instance.canonical.publicInstance;
}
}

// React Native. Legacy. Paper.
if (typeof instance._nativeTag === 'number') {
return instance._nativeTag;
}
}

// React Web. Usually a DOM element.
return instance;
}

function aquireHostInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ function getInspectorDataForViewAtPoint(

closestInstance =
internalInstanceHandle.stateNode.canonical.internalInstanceHandle;
const closestPublicInstance =
internalInstanceHandle.stateNode.canonical.publicInstance;

// Note: this is deprecated and we want to remove it ASAP. Keeping it here for React DevTools compatibility for now.
const nativeViewTag =
Expand All @@ -224,6 +226,7 @@ function getInspectorDataForViewAtPoint(
pointerY: locationY,
frame: {left: pageX, top: pageY, width, height},
touchedViewTag: nativeViewTag,
closestPublicInstance,
});
},
);
Expand All @@ -243,6 +246,7 @@ function getInspectorDataForViewAtPoint(
pointerY: locationY,
frame: {left, top, width, height},
touchedViewTag: nativeViewTag,
closestPublicInstance: nativeViewTag,
});
},
);
Expand Down
1 change: 1 addition & 0 deletions packages/react-native-renderer/src/ReactNativeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export type TouchedViewDataAtPoint = $ReadOnly<{
width: number,
height: number,
}>,
closestPublicInstance?: PublicInstance,
...InspectorData,
}>;

Expand Down
Loading