forked from facebook/create-react-app
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathoverlay.js
88 lines (77 loc) · 2.48 KB
/
overlay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* @flow */
import { applyStyles } from '../utils/dom/css';
import { overlayStyle, headerStyle, additionalStyle } from '../styles';
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,
name: ?string,
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);
overlay.appendChild(createClose(document, closeCallback));
// Create container
const container = document.createElement('div');
container.className = 'cra-container';
overlay.appendChild(container);
// Create additional
const additional = document.createElement('div');
applyStyles(additional, additionalStyle);
container.appendChild(additional);
updateAdditional(
document,
additional,
currentError,
totalErrors,
switchCallback
);
// Create header
const header = document.createElement('div');
applyStyles(header, headerStyle);
// Make message prettier
let finalMessage = message.match(/^\w*:/) || !name
? message
: name + ': ' + message;
finalMessage = finalMessage
// TODO: maybe remove this prefix from fbjs?
// It's just scaring people
.replace(/^Invariant Violation:\s*/, '')
// This is not helpful either:
.replace(/^Warning:\s*/, '')
// Break the actionable part to the next line.
// AFAIK React 16+ should already do this.
.replace(' Check the render method', '\n\nCheck the render method')
.replace(' Check your code at', '\n\nCheck your code at');
// Put it in the DOM
header.appendChild(document.createTextNode(finalMessage));
container.appendChild(header);
// Create trace
container.appendChild(
createFrames(document, frames, frameSettings, contextSize, name)
);
// Show message
container.appendChild(createFooter(document));
return {
overlay,
additional,
};
}
export { createOverlay };