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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/*
* SonarQube
* Copyright (C) 2009-2023 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* eslint-disable prefer-destructuring */
import classNames from 'classnames';
import { throttle } from 'lodash';
import * as React from 'react';
import { createPortal, findDOMNode } from 'react-dom';
import ClickEventBoundary from '../controls/ClickEventBoundary';
import ScreenPositionFixer from '../controls/ScreenPositionFixer';
import './popups.css';
/**
* Positioning rules:
* - Bottom = below the block, horizontally centered
* - BottomLeft = below the block, horizontally left-aligned
* - BottomRight = below the block, horizontally right-aligned
* - LeftTop = on the left-side of the block, vertically top-aligned
* - RightTop = on the right-side of the block, vertically top-aligned
* - RightBottom = on the right-side of the block, vetically bottom-aligned
* - TopLeft = above the block, horizontally left-aligned
*/
export enum PopupPlacement {
Bottom = 'bottom',
BottomLeft = 'bottom-left',
BottomRight = 'bottom-right',
LeftTop = 'left-top',
RightTop = 'right-top',
RightBottom = 'right-bottom',
TopLeft = 'top-left',
}
interface PopupProps {
arrowStyle?: React.CSSProperties;
children?: React.ReactNode;
className?: string;
noPadding?: boolean;
placement?: PopupPlacement;
style?: React.CSSProperties;
}
function PopupBase(props: PopupProps, ref: React.Ref<HTMLDivElement>) {
const { placement = PopupPlacement.Bottom } = props;
return (
<ClickEventBoundary>
<div
className={classNames(
'popup',
`is-${placement}`,
{ 'no-padding': props.noPadding },
props.className
)}
ref={ref || React.createRef()}
style={props.style}
>
{props.children}
<PopupArrow style={props.arrowStyle} />
</div>
</ClickEventBoundary>
);
}
const PopupWithRef = React.forwardRef(PopupBase);
PopupWithRef.displayName = 'Popup';
export const Popup = PopupWithRef;
interface PopupArrowProps {
style?: React.CSSProperties;
}
export function PopupArrow(props: PopupArrowProps) {
return <div className="popup-arrow" style={props.style} />;
}
interface PortalPopupProps extends Omit<PopupProps, 'arrowStyle' | 'style'> {
arrowOffset?: number;
children: React.ReactNode;
overlay: React.ReactNode;
}
interface Measurements {
height: number;
left: number;
top: number;
width: number;
}
type State = Partial<Measurements>;
function isMeasured(state: State): state is Measurements {
return state.height !== undefined;
}
export class PortalPopup extends React.Component<PortalPopupProps, State> {
mounted = false;
popupNode = React.createRef<HTMLDivElement>();
throttledPositionTooltip: () => void;
constructor(props: PortalPopupProps) {
super(props);
this.state = {};
this.throttledPositionTooltip = throttle(this.positionPopup, 10);
}
componentDidMount() {
this.mounted = true;
this.positionPopup();
this.addEventListeners();
}
componentDidUpdate(prevProps: PortalPopupProps) {
if (this.props.placement !== prevProps.placement || this.props.overlay !== prevProps.overlay) {
this.positionPopup();
}
}
componentWillUnmount() {
this.mounted = false;
this.removeEventListeners();
}
addEventListeners = () => {
window.addEventListener('resize', this.throttledPositionTooltip);
window.addEventListener('scroll', this.throttledPositionTooltip);
};
removeEventListeners = () => {
window.removeEventListener('resize', this.throttledPositionTooltip);
window.removeEventListener('scroll', this.throttledPositionTooltip);
};
getPlacement = (): PopupPlacement => {
return this.props.placement || PopupPlacement.Bottom;
};
adjustArrowPosition = (
placement: PopupPlacement,
{ leftFix, topFix }: { leftFix: number; topFix: number }
) => {
const { arrowOffset = 0 } = this.props;
switch (placement) {
case PopupPlacement.Bottom:
case PopupPlacement.BottomLeft:
case PopupPlacement.BottomRight:
case PopupPlacement.TopLeft:
return { marginLeft: -leftFix + arrowOffset };
default:
return { marginTop: -topFix + arrowOffset };
}
};
positionPopup = () => {
// `findDOMNode(this)` will search for the DOM node for the current component
// first it will find a React.Fragment (see `render`),
// so it will get the DOM node of the first child, i.e. DOM node of `this.props.children`
// docs: https://reactjs.org/docs/refs-and-the-dom.html#exposing-dom-refs-to-parent-components
// eslint-disable-next-line react/no-find-dom-node
const toggleNode = findDOMNode(this);
if (toggleNode && toggleNode instanceof Element && this.popupNode.current) {
const toggleRect = toggleNode.getBoundingClientRect();
const { width, height } = this.popupNode.current.getBoundingClientRect();
let left = 0;
let top = 0;
switch (this.getPlacement()) {
case PopupPlacement.Bottom:
left = toggleRect.left + toggleRect.width / 2 - width / 2;
top = toggleRect.top + toggleRect.height;
break;
case PopupPlacement.BottomLeft:
left = toggleRect.left;
top = toggleRect.top + toggleRect.height;
break;
case PopupPlacement.BottomRight:
left = toggleRect.left + toggleRect.width - width;
top = toggleRect.top + toggleRect.height;
break;
case PopupPlacement.LeftTop:
left = toggleRect.left - width;
top = toggleRect.top;
break;
case PopupPlacement.RightTop:
left = toggleRect.left + toggleRect.width;
top = toggleRect.top;
break;
case PopupPlacement.RightBottom:
left = toggleRect.left + toggleRect.width;
top = toggleRect.top + toggleRect.height - height;
break;
case PopupPlacement.TopLeft:
left = toggleRect.left;
top = toggleRect.top - height;
break;
}
// save width and height (and later set in `render`) to avoid resizing the popup element,
// when it's placed close to the window edge
this.setState({
left: window.pageXOffset + left,
top: window.pageYOffset + top,
width,
height,
});
}
};
renderActual = ({ leftFix = 0, topFix = 0 }) => {
const { className, overlay, noPadding } = this.props;
const placement = this.getPlacement();
let arrowStyle;
let style;
if (isMeasured(this.state)) {
style = {
left: this.state.left + leftFix,
top: this.state.top + topFix,
width: this.state.width,
height: this.state.height,
};
arrowStyle = this.adjustArrowPosition(placement, { leftFix, topFix });
}
return (
<Popup
arrowStyle={arrowStyle}
className={className}
noPadding={noPadding}
placement={placement}
ref={this.popupNode}
style={style}
>
{overlay}
</Popup>
);
};
render() {
return (
<>
{this.props.children}
{this.props.overlay && (
<PortalWrapper>
<ScreenPositionFixer ready={isMeasured(this.state)}>
{this.renderActual}
</ScreenPositionFixer>
</PortalWrapper>
)}
</>
);
}
}
class PortalWrapper extends React.Component {
el: HTMLElement;
constructor(props: {}) {
super(props);
this.el = document.createElement('div');
this.el.classList.add('popup-portal');
}
componentDidMount() {
document.body.appendChild(this.el);
}
componentWillUnmount() {
document.body.removeChild(this.el);
}
render() {
return createPortal(this.props.children, this.el);
}
}
|