computeAutoPlacement.js
1.67 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
import getBoundaries from '../utils/getBoundaries';
function getArea({ width, height }) {
return width * height;
}
/**
* Utility used to transform the `auto` placement to the placement with more
* available space.
* @method
* @memberof Popper.Utils
* @argument {Object} data - The data object generated by update method
* @argument {Object} options - Modifiers configuration and options
* @returns {Object} The data object, properly modified
*/
export default function computeAutoPlacement(
placement,
refRect,
popper,
reference,
boundariesElement,
padding = 0
) {
if (placement.indexOf('auto') === -1) {
return placement;
}
const boundaries = getBoundaries(
popper,
reference,
padding,
boundariesElement
);
const rects = {
top: {
width: boundaries.width,
height: refRect.top - boundaries.top,
},
right: {
width: boundaries.right - refRect.right,
height: boundaries.height,
},
bottom: {
width: boundaries.width,
height: boundaries.bottom - refRect.bottom,
},
left: {
width: refRect.left - boundaries.left,
height: boundaries.height,
},
};
const sortedAreas = Object.keys(rects)
.map(key => ({
key,
...rects[key],
area: getArea(rects[key]),
}))
.sort((a, b) => b.area - a.area);
const filteredAreas = sortedAreas.filter(
({ width, height }) =>
width >= popper.clientWidth && height >= popper.clientHeight
);
const computedPlacement = filteredAreas.length > 0
? filteredAreas[0].key
: sortedAreas[0].key;
const variation = placement.split('-')[1];
return computedPlacement + (variation ? `-${variation}` : '');
}