Skip to content

Commit

Permalink
Highlight elements on press + hove
Browse files Browse the repository at this point in the history
Summary:
Changelog:
[General][Added] - Highlight elements on hover while mouse down for React DevTools element inspection.

Since there is probably no mouse hover events for RN, this diff implements something that works similar like hover for RN: user keeps the mouse down and moves the cursor around, and the elements under the mouse is highlighted just like Web.

Reviewed By: lunaruan

Differential Revision: D40369733

fbshipit-source-id: ef223ee0f31f4e0372674fc39dd13bad8c15aa92
  • Loading branch information
tyao1 authored and facebook-github-bot committed Oct 14, 2022
1 parent 4664292 commit 94429fb
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Libraries/Inspector/DevtoolsOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function DevtoolsOverlay({
inspectedView: ?HostRef,
}): React.Node {
const [inspected, setInspected] = useState<null | {
frame: {height: any, left: any, top: any, width: any},
frame: {+height: any, +left: any, +top: any, +width: any},
}>(null);
const [isInspecting, setIsInspecting] = useState(false);
const devToolsAgentRef = useRef(null);
Expand Down Expand Up @@ -124,16 +124,17 @@ export default function DevtoolsOverlay({
locationX,
locationY,
viewData => {
const {touchedViewTag, closestInstance} = viewData;
const {touchedViewTag, closestInstance, frame} = viewData;
if (closestInstance != null || touchedViewTag != null) {
if (closestInstance != null) {
// Fabric
agent.selectNode(closestInstance);
} else {
agent.selectNode(findNodeHandle(touchedViewTag));
}
agent.stopInspectingNative(true);
setIsInspecting(false);
setInspected({
frame,
});
return true;
}
return false;
Expand All @@ -143,6 +144,16 @@ export default function DevtoolsOverlay({
[inspectedView],
);

const onResponderRelease = useCallback(() => {
const agent = devToolsAgentRef.current;
if (agent == null) {
return;
}
agent.stopInspectingNative(true);
setIsInspecting(false);
setInspected(null);
}, []);

const shouldSetResponser = useCallback(
(e: PressEvent): boolean => {
findViewForTouchEvent(e);
Expand All @@ -157,6 +168,7 @@ export default function DevtoolsOverlay({
<View
onStartShouldSetResponder={shouldSetResponser}
onResponderMove={findViewForTouchEvent}
onResponderRelease={onResponderRelease}
nativeID="devToolsInspectorOverlay"
style={[styles.inspector, {height: Dimensions.get('window').height}]}>
{highlight}
Expand Down

0 comments on commit 94429fb

Please sign in to comment.