Home / Class/ Overlay Class — react Architecture

Overlay Class — react Architecture

Architecture documentation for the Overlay class in Overlay.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  76519fd3_f246_fb28_c5c7_e3e31c4d0bc6["Overlay"]
  5b39fc3a_1731_1aaf_f727_b3222027f445["Overlay.js"]
  76519fd3_f246_fb28_c5c7_e3e31c4d0bc6 -->|defined in| 5b39fc3a_1731_1aaf_f727_b3222027f445
  2aefb676_663c_c1e6_d265_8493cbc5b1ae["constructor()"]
  76519fd3_f246_fb28_c5c7_e3e31c4d0bc6 -->|method| 2aefb676_663c_c1e6_d265_8493cbc5b1ae
  e80924d1_f4b0_04e5_a2b1_2f5d36230466["remove()"]
  76519fd3_f246_fb28_c5c7_e3e31c4d0bc6 -->|method| e80924d1_f4b0_04e5_a2b1_2f5d36230466
  ca452625_274e_0064_ef00_337501859e94["inspect()"]
  76519fd3_f246_fb28_c5c7_e3e31c4d0bc6 -->|method| ca452625_274e_0064_ef00_337501859e94

Relationship Graph

Source Code

packages/react-devtools-shared/src/backend/views/Highlighter/Overlay.js lines 150–271

export default class Overlay {
  window: any;
  tipBoundsWindow: any;
  container: HTMLElement;
  tip: OverlayTip;
  rects: Array<OverlayRect>;
  agent: Agent;

  constructor(agent: Agent) {
    // Find the root window, because overlays are positioned relative to it.
    const currentWindow = window.__REACT_DEVTOOLS_TARGET_WINDOW__ || window;
    this.window = currentWindow;

    // When opened in shells/dev, the tooltip should be bound by the app iframe, not by the topmost window.
    const tipBoundsWindow = window.__REACT_DEVTOOLS_TARGET_WINDOW__ || window;
    this.tipBoundsWindow = tipBoundsWindow;

    const doc = currentWindow.document;
    this.container = doc.createElement('div');
    this.container.style.zIndex = '10000000';

    this.tip = new OverlayTip(doc, this.container);
    this.rects = [];

    this.agent = agent;

    doc.body.appendChild(this.container);
  }

  remove() {
    this.tip.remove();
    this.rects.forEach(rect => {
      rect.remove();
    });
    this.rects.length = 0;
    if (this.container.parentNode) {
      this.container.parentNode.removeChild(this.container);
    }
  }

  inspect(nodes: $ReadOnlyArray<HTMLElement | Text>, name?: ?string) {
    // We can't get the size of text nodes or comment nodes. React as of v15
    // heavily uses comment nodes to delimit text.
    // TODO: We actually can measure text nodes. We should.
    const elements: $ReadOnlyArray<HTMLElement> = (nodes.filter(
      node => node.nodeType === Node.ELEMENT_NODE,
    ): any);

    while (this.rects.length > elements.length) {
      const rect = this.rects.pop();
      // $FlowFixMe[incompatible-use]
      rect.remove();
    }
    if (elements.length === 0) {
      return;
    }

    while (this.rects.length < elements.length) {
      this.rects.push(new OverlayRect(this.window.document, this.container));
    }

    const outerBox = {
      top: Number.POSITIVE_INFINITY,
      right: Number.NEGATIVE_INFINITY,
      bottom: Number.NEGATIVE_INFINITY,
      left: Number.POSITIVE_INFINITY,
    };
    elements.forEach((element, index) => {
      const box = getNestedBoundingClientRect(element, this.window);
      const dims = getElementDimensions(element);

      outerBox.top = Math.min(outerBox.top, box.top - dims.marginTop);
      outerBox.right = Math.max(
        outerBox.right,
        box.left + box.width + dims.marginRight,
      );
      outerBox.bottom = Math.max(
        outerBox.bottom,
        box.top + box.height + dims.marginBottom,
      );
      outerBox.left = Math.min(outerBox.left, box.left - dims.marginLeft);

Domain

Frequently Asked Questions

What is the Overlay class?
Overlay is a class in the react codebase, defined in packages/react-devtools-shared/src/backend/views/Highlighter/Overlay.js.
Where is Overlay defined?
Overlay is defined in packages/react-devtools-shared/src/backend/views/Highlighter/Overlay.js at line 150.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free