Debian Codegraph @

packages/popper/src/modifiers/computeStyle.js

raw
1 import getSupportedPropertyName from '../utils/getSupportedPropertyName';
2 import find from '../utils/find';
3 import getOffsetParent from '../utils/getOffsetParent';
4 import getBoundingClientRect from '../utils/getBoundingClientRect';
5 import getRoundedOffsets from '../utils/getRoundedOffsets';
6 import isBrowser from '../utils/isBrowser';
7
8 const isFirefox = isBrowser && /Firefox/i.test(navigator.userAgent);
9
10 /**
11 * @function
12 * @memberof Modifiers
13 * @argument {Object} data - The data object generated by `update` method
14 * @argument {Object} options - Modifiers configuration and options
15 * @returns {Object} The data object, properly modified
16 */
17 export default function computeStyle(data, options) {
18 const { x, y } = options;
19 const { popper } = data.offsets;
20
21 // Remove this legacy support in Popper.js v2
22 const legacyGpuAccelerationOption = find(
23 data.instance.modifiers,
24 modifier => modifier.name === 'applyStyle'
25 ).gpuAcceleration;
26 if (legacyGpuAccelerationOption !== undefined) {
27 console.warn(
28 'WARNING: `gpuAcceleration` option moved to `computeStyle` modifier and will not be supported in future versions of Popper.js!'
29 );
30 }
31 const gpuAcceleration =
32 legacyGpuAccelerationOption !== undefined
33 ? legacyGpuAccelerationOption
34 : options.gpuAcceleration;
35
36 const offsetParent = getOffsetParent(data.instance.popper);
37 const offsetParentRect = getBoundingClientRect(offsetParent);
38
39 // Styles
40 const styles = {
41 position: popper.position,
42 };
43
44 const offsets = getRoundedOffsets(
45 data,
46 window.devicePixelRatio < 2 || !isFirefox
47 );
48
49 const sideA = x === 'bottom' ? 'top' : 'bottom';
50 const sideB = y === 'right' ? 'left' : 'right';
51
52 // if gpuAcceleration is set to `true` and transform is supported,
53 // we use `translate3d` to apply the position to the popper we
54 // automatically use the supported prefixed version if needed
55 const prefixedProperty = getSupportedPropertyName('transform');
56
57 // now, let's make a step back and look at this code closely (wtf?)
58 // If the content of the popper grows once it's been positioned, it
59 // may happen that the popper gets misplaced because of the new content
60 // overflowing its reference element
61 // To avoid this problem, we provide two options (x and y), which allow
62 // the consumer to define the offset origin.
63 // If we position a popper on top of a reference element, we can set
64 // `x` to `top` to make the popper grow towards its top instead of
65 // its bottom.
66 let left, top;
67 if (sideA === 'bottom') {
68 // when offsetParent is <html> the positioning is relative to the bottom of the screen (excluding the scrollbar)
69 // and not the bottom of the html element
70 if (offsetParent.nodeName === 'HTML') {
71 top = -offsetParent.clientHeight + offsets.bottom;
72 } else {
73 top = -offsetParentRect.height + offsets.bottom;
74 }
75 } else {
76 top = offsets.top;
77 }
78 if (sideB === 'right') {
79 if (offsetParent.nodeName === 'HTML') {
80 left = -offsetParent.clientWidth + offsets.right;
81 } else {
82 left = -offsetParentRect.width + offsets.right;
83 }
84 } else {
85 left = offsets.left;
86 }
87 if (gpuAcceleration && prefixedProperty) {
88 styles[prefixedProperty] = `translate3d(${left}px, ${top}px, 0)`;
89 styles[sideA] = 0;
90 styles[sideB] = 0;
91 styles.willChange = 'transform';
92 } else {
93 // othwerise, we use the standard `top`, `left`, `bottom` and `right` properties
94 const invertTop = sideA === 'bottom' ? -1 : 1;
95 const invertLeft = sideB === 'right' ? -1 : 1;
96 styles[sideA] = top * invertTop;
97 styles[sideB] = left * invertLeft;
98 styles.willChange = `${sideA}, ${sideB}`;
99 }
100
101 // Attributes
102 const attributes = {
103 'x-placement': data.placement,
104 };
105
106 // Update `data` attributes, styles and arrowStyles
107 data.attributes = { ...attributes, ...data.attributes };
108 data.styles = { ...styles, ...data.styles };
109 data.arrowStyles = { ...data.offsets.arrow, ...data.arrowStyles };
110
111 return data;
112 }