main
js 83 lines 1.81 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 import type Agent from 'react-devtools-shared/src/backend/agent';
11 import type {HostInstance} from '../../types';
12
13 import {isReactNativeEnvironment} from 'react-devtools-shared/src/backend/utils';
14
15 import Overlay from './Overlay';
16
17 const SHOW_DURATION = 2000;
18
19 let timeoutID: TimeoutID | null = null;
20 let overlay: Overlay | null = null;
21
22 function hideOverlayNative(agent: Agent): void {
23 agent.emit('hideNativeHighlight');
24 }
25
26 function hideOverlayWeb(): void {
27 timeoutID = null;
28
29 if (overlay !== null) {
30 overlay.remove();
31 overlay = null;
32 }
33 }
34
35 export function hideOverlay(agent: Agent): void {
36 return isReactNativeEnvironment()
37 ? hideOverlayNative(agent)
38 : hideOverlayWeb();
39 }
40
41 function showOverlayNative(
42 elements: $ReadOnlyArray<HostInstance>,
43 agent: Agent,
44 ): void {
45 agent.emit('showNativeHighlight', elements);
46 }
47
48 function showOverlayWeb(
49 elements: $ReadOnlyArray<HTMLElement>,
50 componentName: string | null,
51 agent: Agent,
52 hideAfterTimeout: boolean,
53 ): void {
54 if (timeoutID !== null) {
55 clearTimeout(timeoutID);
56 }
57
58 if (overlay === null) {
59 overlay = new Overlay(agent);
60 }
61
62 overlay.inspect(elements, componentName);
63
64 if (hideAfterTimeout) {
65 timeoutID = setTimeout(() => hideOverlay(agent), SHOW_DURATION);
66 }
67 }
68
69 export function showOverlay(
70 elements: $ReadOnlyArray<HostInstance>,
71 componentName: string | null,
72 agent: Agent,
73 hideAfterTimeout: boolean,
74 ): void {
75 return isReactNativeEnvironment()
76 ? showOverlayNative(elements, agent)
77 : showOverlayWeb(
78 elements as $ReadOnlyArray<any>,
79 componentName,
80 agent,
81 hideAfterTimeout,
82 );
83 }