Debian Codegraph @

packages/popper/tests/functional/flips.js

raw
1 import Popper from '../../src/index.js';
2 import getOppositePlacement from '../../src/utils/getOppositePlacement';
3
4 // Utils
5 import appendNewPopper from '@popperjs/test-utils/utils/appendNewPopper';
6 import appendNewRef from '@popperjs/test-utils/utils/appendNewRef';
7 import simulateScroll from '@popperjs/test-utils/utils/simulateScroll';
8 import getRect from '../utils/getRect';
9 const jasmineWrapper = document.getElementById('jasmineWrapper');
10
11 const isIPHONE = window.navigator.userAgent.match(/iPhone/i);
12 [true, false].forEach((positionFixed) => {
13
14 describe('[flipping]' + (positionFixed ? ' Fixed' : ''), () => {
15
16 beforeEach(function(){
17 Popper.Defaults.positionFixed = positionFixed;
18 });
19
20 it('should flip from top to bottom', done => {
21 const ref = appendNewRef(1, 'ref', jasmineWrapper);
22 ref.style.marginLeft = '100px';
23 const popper = appendNewPopper(2, 'popper');
24 new Popper(ref, popper, {
25 placement: 'top',
26 onCreate: data => {
27 expect(data.placement).toBe('bottom');
28 data.instance.destroy();
29 done();
30 },
31 });
32 });
33
34 const flippingDefault = [
35 'top',
36 'top-start',
37 'top-end',
38 'bottom',
39 'bottom-start',
40 'bottom-end',
41 'left',
42 'left-start',
43 'left-end',
44 'right',
45 'right-start',
46 'right-end',
47 ];
48
49 const flippingVariations = {
50 'top-start': 'top-end',
51 'top-end': 'top-start',
52 'bottom-start': 'bottom-end',
53 'bottom-end': 'bottom-start',
54 'left-start': 'left-end',
55 'left-end': 'left-start',
56 'right-start': 'right-end',
57 'right-end': 'right-start',
58 };
59
60 flippingDefault.forEach(val => {
61 it(`should flip from ${val} to ${getOppositePlacement(
62 val
63 )} if boundariesElement is set`, done => {
64 const relative = document.createElement('div');
65 relative.style.margin = '100px 300px';
66 relative.style.height = '100px';
67 relative.style.width = '100px';
68 relative.style.background = '#ffff00';
69 jasmineWrapper.appendChild(relative);
70
71 const ref = appendNewRef(1, 'ref', relative);
72 ref.style.width = '70px';
73 ref.style.height = '70px';
74 ref.style.background = 'green';
75 // ref.style.marginTop = '100px';
76 const popper = appendNewPopper(2, 'popper');
77
78 new Popper(ref, popper, {
79 placement: val,
80 modifiers: {
81 flip: { boundariesElement: relative },
82 },
83 onCreate: data => {
84 expect(data.flipped).toBe(true);
85 expect(data.placement).toBe(getOppositePlacement(val));
86 expect(data.originalPlacement).toBe(val);
87 data.instance.destroy();
88 done();
89 },
90 });
91 });
92
93 it('should NOT flip if there is no boundariesElement', done => {
94 const relative = document.createElement('div');
95 relative.style.margin = '100px 300px';
96 relative.style.height = '100px';
97 relative.style.width = '100px';
98 relative.style.background = '#ffff00';
99 jasmineWrapper.appendChild(relative);
100
101 const ref = appendNewRef(1, 'ref', relative);
102 ref.style.width = '70px';
103 ref.style.height = '70px';
104 ref.style.background = 'green';
105 // ref.style.marginTop = '100px';
106 const popper = appendNewPopper(3, 'popper');
107
108 new Popper(ref, popper, {
109 placement: val,
110 onCreate: data => {
111 expect(data.flipped).not.toBe(true);
112 expect(data.placement).toBe(val);
113 expect(data.originalPlacement).toBe(val);
114 data.instance.destroy();
115 done();
116 },
117 });
118 });
119 });
120
121 function getSecondaryMarginByReference(val) {
122 return (val === 'start' ? '-' : '') + '100px';
123 }
124
125 function getSecondaryMarginByContent(val) {
126 return val === 'start' ? '200px' : '50px';
127 }
128
129 Object.keys(flippingVariations).forEach(val => {
130 it(`(variations)(by reference) should flip from ${val} to ${flippingVariations[
131 val
132 ]} if boundariesElement is set`, done => {
133 const relative = document.createElement('div');
134 relative.style.margin = '100px 300px';
135 relative.style.height = '300px';
136 relative.style.width = '300px';
137 relative.style.background = '#ffff00';
138 relative.style.position = 'relative';
139 jasmineWrapper.appendChild(relative);
140
141 const ref = appendNewRef(1, 'ref', relative);
142 ref.style.width = '200px';
143 ref.style.height = '200px';
144 ref.style.background = 'green';
145 ref.style.position = 'absolute';
146 ref.style.zIndex = '10';
147 const valElems = val.split('-');
148
149 switch (valElems[0]) {
150 case 'top':
151 ref.style.top = '100px';
152 ref.style.left = getSecondaryMarginByReference(valElems[1]);
153 break;
154 case 'bottom':
155 ref.style.bottom = '100px';
156 ref.style.left = getSecondaryMarginByReference(valElems[1]);
157 break;
158 case 'left':
159 ref.style.top = getSecondaryMarginByReference(valElems[1]);
160 ref.style.left = '200px';
161 break;
162 case 'right':
163 ref.style.top = getSecondaryMarginByReference(valElems[1]);
164 ref.style.right = '200px';
165 break;
166 }
167
168 const popper = appendNewPopper(2, 'popper');
169
170 new Popper(ref, popper, {
171 placement: val,
172 modifiers: {
173 preventOverflow: {
174 enabled: true,
175 escapeWithReference: true,
176 },
177 flip: {
178 flipVariations: true,
179 boundariesElement: relative,
180 },
181 },
182 onCreate: data => {
183 expect(data.flipped).toBe(true);
184 expect(data.placement).toBe(flippingVariations[val]);
185 expect(data.originalPlacement).toBe(val);
186 data.instance.destroy();
187 done();
188 },
189 });
190 });
191 });
192
193 Object.keys(flippingVariations).forEach(val => {
194 it(`(variations)(by content) should flip from ${val} to ${flippingVariations[
195 val
196 ]} if boundariesElement is set`, done => {
197 const relative = document.createElement('div');
198 relative.style.margin = '100px 300px';
199 relative.style.height = '300px';
200 relative.style.width = '300px';
201 relative.style.background = '#ffff00';
202 relative.style.position = 'relative';
203 jasmineWrapper.appendChild(relative);
204
205 const ref = appendNewRef(1, 'ref', relative);
206 ref.style.width = '50px';
207 ref.style.height = '50px';
208 ref.style.background = 'green';
209 ref.style.position = 'absolute';
210 ref.style.zIndex = '10';
211 const valElems = val.split('-');
212
213 switch (valElems[0]) {
214 case 'top':
215 ref.style.bottom = '20px';
216 ref.style.left = getSecondaryMarginByContent(valElems[1]);
217 break;
218 case 'bottom':
219 ref.style.top = '20px';
220 ref.style.left = getSecondaryMarginByContent(valElems[1]);
221 break;
222 case 'left':
223 ref.style.top = getSecondaryMarginByContent(valElems[1]);
224 ref.style.left = '200px';
225 break;
226 case 'right':
227 ref.style.top = getSecondaryMarginByContent(valElems[1]);
228 ref.style.right = '200px';
229 break;
230 }
231
232 const large = document.createElement('div');
233 large.style.width = '150px';
234 large.style.height = '150px';
235 large.style.backgroundColor = 'blue';
236
237 const popper = appendNewPopper(2, 'popper');
238 popper.appendChild(large);
239
240 new Popper(ref, popper, {
241 placement: val,
242 modifiers: {
243 preventOverflow: {
244 enabled: true,
245 escapeWithReference: true,
246 },
247 flip: {
248 flipVariationsByContent: true,
249 boundariesElement: relative,
250 },
251 },
252 onCreate: data => {
253 expect(data.flipped).toBe(true);
254 expect(data.placement).toBe(flippingVariations[val]);
255 expect(data.originalPlacement).toBe(val);
256 data.instance.destroy();
257 done();
258 },
259 });
260 });
261 });
262
263 it('flips to opposite side when rendered inside a positioned parent', done => {
264 const page = document.createElement('div');
265 page.style.paddingTop = '110vh'; // Simulates page content
266 page.style.background = 'lightskyblue';
267 jasmineWrapper.appendChild(page);
268
269 const parent = document.createElement('div');
270 parent.style.position = 'relative';
271 parent.style.background = 'yellow';
272 page.appendChild(parent);
273
274 const ref = appendNewRef(1, 'reference', parent);
275 const popper = appendNewPopper(2, 'popper', parent);
276
277 new Popper(ref, popper, {
278 onCreate: data => {
279 simulateScroll(page, { scrollTop: '110vh', delay: 10 });
280
281 const popperRect = popper.getBoundingClientRect();
282 const refRect = ref.getBoundingClientRect();
283 const arrowSize = 5;
284
285 expect(data.flipped).toBe(true);
286 expect(popperRect.bottom + arrowSize).toBeApprox(refRect.top);
287
288 data.instance.destroy();
289 done();
290 },
291 });
292 });
293
294 it('flips to bottom when hits top viewport edge', done => {
295 if (isIPHONE) {
296 pending();
297 }
298
299 jasmineWrapper.innerHTML = `
300 <div id="s1" style="height: 3000px; background: red;">
301 <div id="reference" style="background: pink; margin-top: 200px">reference</div>
302 <div id="popper" style="background: purple">popper</div>
303 </div>
304 `;
305
306 const reference = document.getElementById('reference');
307 const popper = document.getElementById('popper');
308
309 new Popper(reference, popper, {
310 placement: 'top',
311 onCreate() {
312 simulateScroll(document.body, { scrollTop: 200 });
313 },
314 onUpdate(data) {
315 expect(getRect(popper).top).toBeApprox(getRect(reference).bottom);
316 data.instance.destroy();
317 done();
318 },
319 });
320 });
321
322 it('flip properly with large popper width', done => {
323 jasmineWrapper.innerHTML = `
324 <style>body { margin: 0; }</style>
325 <div style="background: grey; height: 100vh;">
326 <div
327 id="reference"
328 style="background: yellow; width: 200px; height: 80vh;"
329 >
330 ref
331 </div>
332 <div
333 id="popper"
334 style="background: purple; width: 95vw; height: 30px;"
335 >
336 popper
337 </div>
338 </div>
339 `;
340
341 const reference = document.getElementById('reference');
342 const popper = document.getElementById('popper');
343
344 new Popper(reference, popper, {
345 placement: 'auto',
346 onCreate(data) {
347 expect(data.placement).toBe('bottom');
348 expect(getRect(reference).bottom).toBeApprox(getRect(popper).top);
349 data.instance.destroy();
350 done();
351 },
352 });
353 });
354
355 it('init popper on fixed reference aligned to left and flips to right', done => {
356 jasmineWrapper.innerHTML = `
357 <div id="reference" style="position: fixed; top: 50px; left: 1px; background: pink">reference</div>
358 <div id="popper" style="background: purple; width: 5px">popper</div>
359 `;
360
361 const reference = document.getElementById('reference');
362 const popper = document.getElementById('popper');
363
364 new Popper(reference, popper, {
365 placement: 'left-start',
366 onCreate() {
367 expect(popper.getBoundingClientRect().top).toBeApprox(
368 reference.getBoundingClientRect().top
369 );
370 expect(popper.getBoundingClientRect().left).toBeApprox(
371 reference.getBoundingClientRect().right
372 );
373 done();
374 },
375 });
376 });
377
378 it('init popper on fixed reference aligned to right and flips to left', done => {
379 jasmineWrapper.innerHTML = `
380 <div id="reference" style="position: fixed; top: 50px; right: 1px; background: pink">reference</div>
381 <div id="popper" style="background: purple;">popper</div>
382 `;
383
384 const reference = document.getElementById('reference');
385 const popper = document.getElementById('popper');
386
387 new Popper(reference, popper, {
388 placement: 'right-start',
389 onCreate() {
390 expect(popper.getBoundingClientRect().top).toBeApprox(
391 reference.getBoundingClientRect().top
392 );
393 expect(popper.getBoundingClientRect().right).toBeApprox(
394 reference.getBoundingClientRect().left
395 );
396 done();
397 },
398 });
399 });
400
401 it('init popper on fixed reference aligned to top and flips to bottom', done => {
402 jasmineWrapper.innerHTML = `
403 <div id="reference" style="position: fixed; top: 1px; left: 50px; background: pink">reference</div>
404 <div id="popper" style="background: purple;">popper</div>
405 `;
406
407 const reference = document.getElementById('reference');
408 const popper = document.getElementById('popper');
409
410 new Popper(reference, popper, {
411 placement: 'top-start',
412 onCreate() {
413 expect(popper.getBoundingClientRect().top).toBeApprox(
414 reference.getBoundingClientRect().bottom
415 );
416 expect(popper.getBoundingClientRect().left).toBeApprox(
417 reference.getBoundingClientRect().left
418 );
419 done();
420 },
421 });
422 });
423
424 it('init popper on fixed reference aligned to bottom and flips to top', done => {
425 jasmineWrapper.innerHTML = `
426 <div id="reference" style="position: fixed; bottom: 1px; right: 50px; background: pink">reference</div>
427 <div id="popper" style="background: purple;">popper</div>
428 `;
429
430 const reference = document.getElementById('reference');
431 const popper = document.getElementById('popper');
432
433 new Popper(reference, popper, {
434 placement: 'bottom-start',
435 onCreate() {
436 expect(popper.getBoundingClientRect().bottom).toBeApprox(
437 reference.getBoundingClientRect().top
438 );
439 expect(popper.getBoundingClientRect().left).toBeApprox(
440 reference.getBoundingClientRect().left
441 );
442 done();
443 },
444 });
445 });
446
447 it('init popper on transformed parent flips to top', done => {
448 jasmineWrapper.innerHTML = `
449 <div id="container" style="position: relative; margin-left: 100px; margin-top:100px; height: 100vh; transform: translateZ(0)">
450 <div style="height:100%"></div>
451 <div id="reference" style="background: pink">reference</div>
452 <div id="popper" style="background: purple;">popper</div>
453 <div style="height:200%"></div>
454 </div>
455 `;
456
457 const reference = document.getElementById('reference');
458 const popper = document.getElementById('popper');
459 new Popper(reference, popper, {
460 placement: 'bottom-start',
461 onCreate() {
462 expect(popper.getBoundingClientRect().bottom).toBeApprox(
463 reference.getBoundingClientRect().top
464 );
465 expect(popper.getBoundingClientRect().left).toBeApprox(
466 reference.getBoundingClientRect().left
467 );
468 done();
469 },
470 });
471 });
472
473 // This one will fail on IE10 - See #211
474 xit('properly positions a bottom popper inside very high body', done => {
475 jasmineWrapper.innerHTML = `
476 <div id="reference" style="background: pink; margin-top: 100vh; width: 100px; height: 100px;">reference</div>
477 <div id="popper" style="background: purple; width: 100px; height: 100px;">popper</div>
478 `;
479 document.body.style.height = '200vh';
480
481 const reference = document.getElementById('reference');
482 const popper = document.getElementById('popper');
483
484 new Popper(reference, popper, {
485 onCreate() {
486 simulateScroll(document.body, {
487 scrollTop: getRect(document.body).height,
488 delay: 50,
489 });
490 },
491 onUpdate(data) {
492 expect(getRect(popper).top).toBeApprox(getRect(reference).bottom);
493 data.instance.destroy();
494 document.body.style.cssText = null;
495 done();
496 },
497 });
498 });
499 });
500 });