forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopover.js
More file actions
176 lines (139 loc) · 4.44 KB
/
Popover.js
File metadata and controls
176 lines (139 loc) · 4.44 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
import React, { Component } from "react";
import ReactDOM from "react-dom";
import classNames from "classnames";
import BracketArrow from "./BracketArrow";
import "./Popover.css";
type Props = {
target: Object,
targetPosition: Object,
children: Object,
onMouseLeave?: () => void,
type?: string
};
class Popover extends Component {
props: Props;
static defaultProps = {
onMouseLeave: () => {},
type: "popover"
};
constructor() {
super();
this.onMouseLeave = this.onMouseLeave.bind(this);
this.state = {
left: 0,
top: 0
};
}
componentDidMount() {
const { type } = this.props;
const { left, top, orientation, targetMid } =
type == "popover" ? this.getPopoverCoords() : this.getTooltipCoords();
// eslint-disable-next-line react/no-did-mount-set-state
this.setState({ left, top, orientation, targetMid });
}
calculateLeft(target, editor, popover) {
const estimatedLeft = target.left;
const estimatedRight = estimatedLeft + popover.width;
const isOverflowingRight = estimatedRight > editor.right;
if (isOverflowingRight) {
const adjustedLeft = editor.right - popover.width - 8;
return adjustedLeft;
}
return estimatedLeft;
}
calculateVerticalOrientation(target, editor, popover) {
const estimatedBottom = target.bottom + popover.height;
return estimatedBottom > editor.bottom ? "up" : "down";
}
getPopoverCoords() {
const popover = ReactDOM.findDOMNode(this);
const popoverRect = popover.getBoundingClientRect();
const editor = document.querySelector(".editor-wrapper");
const editorRect = editor.getBoundingClientRect();
const targetRect = this.props.targetPosition;
const popoverLeft = this.calculateLeft(targetRect, editorRect, popoverRect);
const orientation = this.calculateVerticalOrientation(
targetRect,
editorRect,
popoverRect
);
const top =
orientation == "down"
? targetRect.bottom
: targetRect.top - popoverRect.height;
const targetMid = targetRect.left - popoverLeft + targetRect.width / 2 - 8;
return { left: popoverLeft, top, orientation, targetMid };
}
getTooltipCoords() {
const tooltip = ReactDOM.findDOMNode(this);
const tooltipRect = tooltip.getBoundingClientRect();
const targetRect = this.props.targetPosition;
const editor = document.querySelector(".editor-wrapper");
const editorRect = editor.getBoundingClientRect();
const left = this.calculateLeft(targetRect, editorRect, tooltipRect);
const top = targetRect.top - tooltipRect.height;
return { left, top, orientation: "up", targetMid: 0 };
}
getChildren() {
const { children } = this.props;
const { orientation } = this.state;
const gap = <div className="gap" key="gap" />;
return orientation === "up" ? [children, gap] : [gap, children];
}
getPopoverArrow(orientation, left) {
const arrowOrientation = orientation === "up" ? "down" : "up";
const arrowProp = arrowOrientation === "up" ? "top" : "bottom";
const arrowPropValue = arrowOrientation === "up" ? -7 : 5;
const arrowProps = {
orientation: arrowOrientation,
left,
[arrowProp]: arrowPropValue
};
return <BracketArrow {...arrowProps} />;
}
onMouseLeave(e) {
const { onMouseLeave } = this.props;
if (e.target.className.match(/(bracket-arrow|gap)/)) {
return;
}
onMouseLeave();
}
renderPopover() {
const { top, left, orientation, targetMid } = this.state;
const arrow = this.getPopoverArrow(orientation, targetMid);
return (
<div
className={classNames("popover", { up: orientation === "up" })}
onMouseLeave={this.onMouseLeave}
style={{ top, left }}
>
{arrow}
{this.getChildren()}
</div>
);
}
renderTooltip() {
const { onMouseLeave } = this.props;
const { top, left } = this.state;
return (
<div
className="tooltip"
onMouseLeave={onMouseLeave}
style={{ top, left }}
>
{this.getChildren()}
</div>
);
}
render() {
const { type } = this.props;
if (type === "tooltip") {
return this.renderTooltip();
}
return this.renderPopover();
}
}
export default Popover;