Skip to content

Commit fbc9b68

Browse files
authored
refactor[devtools]: highlight an array of elements for native (#27734)
We are currently just pass the first element, which diverges from the implementation for web. This is especially bad if you are inspecting something like a list, where host fiber can represent multiple elements. This part runs on the backend of React DevTools, so it should not affect cases for React Native when frontend version can be more up-to-date than backend's. I will double-check it before merging. Once version of `react-devtools-core` is updated in React Native, this should be supported, I will work on that later.
1 parent a3172e9 commit fbc9b68

File tree

3 files changed

+62
-34
lines changed

3 files changed

+62
-34
lines changed

packages/react-devtools-shared/src/backend/utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,9 @@ export function gt(a: string = '', b: string = ''): boolean {
283283
export function gte(a: string = '', b: string = ''): boolean {
284284
return compareVersions(a, b) > -1;
285285
}
286+
287+
export const isReactNativeEnvironment = (): boolean => {
288+
// We've been relying on this for such a long time
289+
// We should probably define the client for DevTools on the backend side and share it with the frontend
290+
return window.document == null;
291+
};

packages/react-devtools-shared/src/backend/views/Highlighter/Highlighter.js

+31-19
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@
99

1010
import type Agent from 'react-devtools-shared/src/backend/agent';
1111

12+
import {isReactNativeEnvironment} from 'react-devtools-shared/src/backend/utils';
13+
1214
import Overlay from './Overlay';
1315

1416
const SHOW_DURATION = 2000;
1517

1618
let timeoutID: TimeoutID | null = null;
1719
let overlay: Overlay | null = null;
1820

19-
export function hideOverlay(agent: Agent) {
20-
if (window.document == null) {
21-
agent.emit('hideNativeHighlight');
22-
return;
23-
}
21+
function hideOverlayNative(agent: Agent): void {
22+
agent.emit('hideNativeHighlight');
23+
}
24+
25+
function hideOverlayWeb(): void {
2426
timeoutID = null;
2527

2628
if (overlay !== null) {
@@ -29,27 +31,26 @@ export function hideOverlay(agent: Agent) {
2931
}
3032
}
3133

32-
export function showOverlay(
33-
elements: Array<HTMLElement> | null,
34+
export function hideOverlay(agent: Agent): void {
35+
return isReactNativeEnvironment()
36+
? hideOverlayNative(agent)
37+
: hideOverlayWeb();
38+
}
39+
40+
function showOverlayNative(elements: Array<HTMLElement>, agent: Agent): void {
41+
agent.emit('showNativeHighlight', elements);
42+
}
43+
44+
function showOverlayWeb(
45+
elements: Array<HTMLElement>,
3446
componentName: string | null,
3547
agent: Agent,
3648
hideAfterTimeout: boolean,
37-
) {
38-
if (window.document == null) {
39-
if (elements != null && elements[0] != null) {
40-
agent.emit('showNativeHighlight', elements[0]);
41-
}
42-
return;
43-
}
44-
49+
): void {
4550
if (timeoutID !== null) {
4651
clearTimeout(timeoutID);
4752
}
4853

49-
if (elements == null) {
50-
return;
51-
}
52-
5354
if (overlay === null) {
5455
overlay = new Overlay(agent);
5556
}
@@ -60,3 +61,14 @@ export function showOverlay(
6061
timeoutID = setTimeout(() => hideOverlay(agent), SHOW_DURATION);
6162
}
6263
}
64+
65+
export function showOverlay(
66+
elements: Array<HTMLElement>,
67+
componentName: string | null,
68+
agent: Agent,
69+
hideAfterTimeout: boolean,
70+
): void {
71+
return isReactNativeEnvironment()
72+
? showOverlayNative(elements, agent)
73+
: showOverlayWeb(elements, componentName, agent, hideAfterTimeout);
74+
}

packages/react-devtools-shared/src/backend/views/TraceUpdates/canvas.js

+25-15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import type {Rect} from '../utils';
1212
import type {NativeType} from '../../types';
1313
import type Agent from '../../agent';
1414

15+
import {isReactNativeEnvironment} from 'react-devtools-shared/src/backend/utils';
16+
1517
const OUTLINE_COLOR = '#f0f0f0';
1618

1719
// Note these colors are in sync with DevTools Profiler chart colors.
@@ -30,17 +32,16 @@ const COLORS = [
3032

3133
let canvas: HTMLCanvasElement | null = null;
3234

33-
export function draw(nodeToData: Map<NativeType, Data>, agent: Agent): void {
34-
if (window.document == null) {
35-
const nodesToDraw = [];
36-
iterateNodes(nodeToData, (_, color, node) => {
37-
nodesToDraw.push({node, color});
38-
});
39-
40-
agent.emit('drawTraceUpdates', nodesToDraw);
41-
return;
42-
}
35+
function drawNative(nodeToData: Map<NativeType, Data>, agent: Agent) {
36+
const nodesToDraw = [];
37+
iterateNodes(nodeToData, (_, color, node) => {
38+
nodesToDraw.push({node, color});
39+
});
4340

41+
agent.emit('drawTraceUpdates', nodesToDraw);
42+
}
43+
44+
function drawWeb(nodeToData: Map<NativeType, Data>) {
4445
if (canvas === null) {
4546
initialize();
4647
}
@@ -58,6 +59,12 @@ export function draw(nodeToData: Map<NativeType, Data>, agent: Agent): void {
5859
});
5960
}
6061

62+
export function draw(nodeToData: Map<NativeType, Data>, agent: Agent): void {
63+
return isReactNativeEnvironment()
64+
? drawNative(nodeToData, agent)
65+
: drawWeb(nodeToData);
66+
}
67+
6168
function iterateNodes(
6269
nodeToData: Map<NativeType, Data>,
6370
execute: (rect: Rect | null, color: string, node: NativeType) => void,
@@ -97,12 +104,11 @@ function drawBorder(
97104
context.setLineDash([0]);
98105
}
99106

100-
export function destroy(agent: Agent): void {
101-
if (window.document == null) {
102-
agent.emit('disableTraceUpdates');
103-
return;
104-
}
107+
function destroyNative(agent: Agent) {
108+
agent.emit('disableTraceUpdates');
109+
}
105110

111+
function destroyWeb() {
106112
if (canvas !== null) {
107113
if (canvas.parentNode != null) {
108114
canvas.parentNode.removeChild(canvas);
@@ -111,6 +117,10 @@ export function destroy(agent: Agent): void {
111117
}
112118
}
113119

120+
export function destroy(agent: Agent): void {
121+
return isReactNativeEnvironment() ? destroyNative(agent) : destroyWeb();
122+
}
123+
114124
function initialize(): void {
115125
canvas = window.document.createElement('canvas');
116126
canvas.style.cssText = `

0 commit comments

Comments
 (0)