forked from juliankrispel/react-text-selection-popover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcenterAboveOrBelow.js
46 lines (39 loc) · 1.24 KB
/
centerAboveOrBelow.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
export default ({
// The gap between the text and the selection and the popover
// comes from `props.gap`
gap,
// by default frame === window but this might
// change in future. For example if we'd like to restrict
// placement to a container other than window
// if frame is window, frameLeft and frameTop are 0
/* frameHeight, */
frameWidth,
frameLeft,
frameTop,
// the dimensions of the popover
boxHeight,
boxWidth,
// the dimensions and position of the selected text
selectionTop,
selectionLeft,
selectionWidth,
selectionHeight
}) => {
const style = { position: "fixed" };
style.left = selectionLeft + (selectionWidth / 2) - (boxWidth / 2);
style.top = selectionTop - boxHeight - gap;
// If the popover is placed beyond the left edge of the screen align
// with left edge
if (style.left < frameLeft) {
style.left = frameLeft;
// if the popover is placed beyond the right edge align with the
// right edge of the sceen
} else if (style.left + boxWidth > frameWidth) {
style.left = frameWidth - boxWidth;
}
// if the popover is placed above the frame, position below selection instead
if (style.top < frameTop) {
style.top = selectionTop + selectionHeight + gap;
}
return style;
};