Debian Codegraph @

packages/popper/src/modifiers/flip.js

raw
1 import getOppositePlacement from '../utils/getOppositePlacement';
2 import getOppositeVariation from '../utils/getOppositeVariation';
3 import getPopperOffsets from '../utils/getPopperOffsets';
4 import runModifiers from '../utils/runModifiers';
5 import getBoundaries from '../utils/getBoundaries';
6 import isModifierEnabled from '../utils/isModifierEnabled';
7 import clockwise from '../utils/clockwise';
8
9 const BEHAVIORS = {
10 FLIP: 'flip',
11 CLOCKWISE: 'clockwise',
12 COUNTERCLOCKWISE: 'counterclockwise',
13 };
14
15 /**
16 * @function
17 * @memberof Modifiers
18 * @argument {Object} data - The data object generated by update method
19 * @argument {Object} options - Modifiers configuration and options
20 * @returns {Object} The data object, properly modified
21 */
22 export default function flip(data, options) {
23 // if `inner` modifier is enabled, we can't use the `flip` modifier
24 if (isModifierEnabled(data.instance.modifiers, 'inner')) {
25 return data;
26 }
27
28 if (data.flipped && data.placement === data.originalPlacement) {
29 // seems like flip is trying to loop, probably there's not enough space on any of the flippable sides
30 return data;
31 }
32
33 const boundaries = getBoundaries(
34 data.instance.popper,
35 data.instance.reference,
36 options.padding,
37 options.boundariesElement,
38 data.positionFixed
39 );
40
41 let placement = data.placement.split('-')[0];
42 let placementOpposite = getOppositePlacement(placement);
43 let variation = data.placement.split('-')[1] || '';
44
45 let flipOrder = [];
46
47 switch (options.behavior) {
48 case BEHAVIORS.FLIP:
49 flipOrder = [placement, placementOpposite];
50 break;
51 case BEHAVIORS.CLOCKWISE:
52 flipOrder = clockwise(placement);
53 break;
54 case BEHAVIORS.COUNTERCLOCKWISE:
55 flipOrder = clockwise(placement, true);
56 break;
57 default:
58 flipOrder = options.behavior;
59 }
60
61 flipOrder.forEach((step, index) => {
62 if (placement !== step || flipOrder.length === index + 1) {
63 return data;
64 }
65
66 placement = data.placement.split('-')[0];
67 placementOpposite = getOppositePlacement(placement);
68
69 const popperOffsets = data.offsets.popper;
70 const refOffsets = data.offsets.reference;
71
72 // using floor because the reference offsets may contain decimals we are not going to consider here
73 const floor = Math.floor;
74 const overlapsRef =
75 (placement === 'left' &&
76 floor(popperOffsets.right) > floor(refOffsets.left)) ||
77 (placement === 'right' &&
78 floor(popperOffsets.left) < floor(refOffsets.right)) ||
79 (placement === 'top' &&
80 floor(popperOffsets.bottom) > floor(refOffsets.top)) ||
81 (placement === 'bottom' &&
82 floor(popperOffsets.top) < floor(refOffsets.bottom));
83
84 const overflowsLeft = floor(popperOffsets.left) < floor(boundaries.left);
85 const overflowsRight = floor(popperOffsets.right) > floor(boundaries.right);
86 const overflowsTop = floor(popperOffsets.top) < floor(boundaries.top);
87 const overflowsBottom =
88 floor(popperOffsets.bottom) > floor(boundaries.bottom);
89
90 const overflowsBoundaries =
91 (placement === 'left' && overflowsLeft) ||
92 (placement === 'right' && overflowsRight) ||
93 (placement === 'top' && overflowsTop) ||
94 (placement === 'bottom' && overflowsBottom);
95
96 // flip the variation if required
97 const isVertical = ['top', 'bottom'].indexOf(placement) !== -1;
98
99 // flips variation if reference element overflows boundaries
100 const flippedVariationByRef =
101 !!options.flipVariations &&
102 ((isVertical && variation === 'start' && overflowsLeft) ||
103 (isVertical && variation === 'end' && overflowsRight) ||
104 (!isVertical && variation === 'start' && overflowsTop) ||
105 (!isVertical && variation === 'end' && overflowsBottom));
106
107 // flips variation if popper content overflows boundaries
108 const flippedVariationByContent =
109 !!options.flipVariationsByContent &&
110 ((isVertical && variation === 'start' && overflowsRight) ||
111 (isVertical && variation === 'end' && overflowsLeft) ||
112 (!isVertical && variation === 'start' && overflowsBottom) ||
113 (!isVertical && variation === 'end' && overflowsTop));
114
115 const flippedVariation = flippedVariationByRef || flippedVariationByContent;
116
117 if (overlapsRef || overflowsBoundaries || flippedVariation) {
118 // this boolean to detect any flip loop
119 data.flipped = true;
120
121 if (overlapsRef || overflowsBoundaries) {
122 placement = flipOrder[index + 1];
123 }
124
125 if (flippedVariation) {
126 variation = getOppositeVariation(variation);
127 }
128
129 data.placement = placement + (variation ? '-' + variation : '');
130
131 // this object contains `position`, we want to preserve it along with
132 // any additional property we may add in the future
133 data.offsets.popper = {
134 ...data.offsets.popper,
135 ...getPopperOffsets(
136 data.instance.popper,
137 data.offsets.reference,
138 data.placement
139 ),
140 };
141
142 data = runModifiers(data.instance.modifiers, data, 'flip');
143 }
144 });
145 return data;
146 }