overlay.js 2.77 KB
Newer Older
Dan Abramov's avatar
Dan Abramov committed
1
2
3
4
5
6
7
8
9
/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

10
11
/* @flow */
import { applyStyles } from '../utils/dom/css';
12
import { containerStyle, overlayStyle, headerStyle } from '../styles';
13
14
15
16
17
18
19
20
21
22
23
import { createClose } from './close';
import { createFrames } from './frames';
import { createFooter } from './footer';
import type { CloseCallback } from './close';
import type { StackFrame } from '../utils/stack-frame';
import { updateAdditional } from './additional';
import type { FrameSetting } from './frames';
import type { SwitchCallback } from './additional';

function createOverlay(
  document: Document,
24
  name: ?string,
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  message: string,
  frames: StackFrame[],
  contextSize: number,
  currentError: number,
  totalErrors: number,
  switchCallback: SwitchCallback,
  closeCallback: CloseCallback
): {
  overlay: HTMLDivElement,
  additional: HTMLDivElement,
} {
  const frameSettings: FrameSetting[] = frames.map(() => ({ compiled: false }));
  // Create overlay
  const overlay = document.createElement('div');
  applyStyles(overlay, overlayStyle);

  // Create container
  const container = document.createElement('div');
43
  applyStyles(container, containerStyle);
44
  overlay.appendChild(container);
45
  container.appendChild(createClose(document, closeCallback));
46

47
48
49
50
51
52
53
54
55
56
57
  // Create "Errors X of Y" in case of multiple errors
  const additional = document.createElement('div');
  updateAdditional(
    document,
    additional,
    currentError,
    totalErrors,
    switchCallback
  );
  container.appendChild(additional);

58
59
60
  // Create header
  const header = document.createElement('div');
  applyStyles(header, headerStyle);
61
62

  // Make message prettier
63
64
  let finalMessage =
    message.match(/^\w*:/) || !name ? message : name + ': ' + message;
65

66
67
68
  finalMessage = finalMessage
    // TODO: maybe remove this prefix from fbjs?
    // It's just scaring people
69
70
71
    .replace(/^Invariant Violation:\s*/, '')
    // This is not helpful either:
    .replace(/^Warning:\s*/, '')
72
73
    // Break the actionable part to the next line.
    // AFAIK React 16+ should already do this.
74
75
    .replace(' Check the render method', '\n\nCheck the render method')
    .replace(' Check your code at', '\n\nCheck your code at');
76
77
78

  // Put it in the DOM
  header.appendChild(document.createTextNode(finalMessage));
79
80
81
82
  container.appendChild(header);

  // Create trace
  container.appendChild(
83
    createFrames(document, frames, frameSettings, contextSize, name)
84
85
86
87
88
89
90
91
92
93
94
95
  );

  // Show message
  container.appendChild(createFooter(document));

  return {
    overlay,
    additional,
  };
}

export { createOverlay };