kamrify commited on
Commit
88eeb96
·
1 Parent(s): ee16eca

Add functionality to position the popover on mid center

Browse files
demo/scripts/demo.js CHANGED
@@ -10,6 +10,7 @@ document.addEventListener('DOMContentLoaded', function () {
10
  title: 'Before we start',
11
  description: 'This is just one use-case, make sure to check out the rest of the docs below.',
12
  nextBtnText: 'Okay, Start!',
 
13
  },
14
  }, {
15
  element: '#logo_img',
 
10
  title: 'Before we start',
11
  description: 'This is just one use-case, make sure to check out the rest of the docs below.',
12
  nextBtnText: 'Okay, Start!',
13
+ position: 'mid-center',
14
  },
15
  }, {
16
  element: '#logo_img',
src/core/popover.js CHANGED
@@ -167,6 +167,9 @@ export default class Popover extends Element {
167
  case 'bottom-right':
168
  this.positionOnBottomRight(position);
169
  break;
 
 
 
170
  case 'auto':
171
  default:
172
  this.autoPosition(position);
@@ -434,6 +437,30 @@ export default class Popover extends Element {
434
  this.tipNode.classList.add('top', 'position-right');
435
  }
436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
437
  /**
438
  * Automatically positions the popover around the given position
439
  * such that the element and popover remain in view
 
167
  case 'bottom-right':
168
  this.positionOnBottomRight(position);
169
  break;
170
+ case 'mid-center':
171
+ this.positionOnMidCenter(position);
172
+ break;
173
  case 'auto':
174
  default:
175
  this.autoPosition(position);
 
437
  this.tipNode.classList.add('top', 'position-right');
438
  }
439
 
440
+ /**
441
+ * Shows the popover on the mid-center of the given position
442
+ * @param {Position} elementPosition
443
+ * @private
444
+ */
445
+ positionOnMidCenter(elementPosition) {
446
+ const popoverDimensions = this.getSize();
447
+ const popoverHeight = popoverDimensions.height;
448
+ const popoverWidth = popoverDimensions.width / 2;
449
+ const popoverCenter = popoverHeight / 2;
450
+
451
+ const elementCenter = (elementPosition.bottom - elementPosition.top) / 2;
452
+ const topCenterPosition = (elementPosition.top - popoverCenter) + elementCenter + this.options.offset;
453
+ const nodeCenter = this.options.offset + elementPosition.left + ((elementPosition.right - elementPosition.left) / 2);
454
+
455
+ this.node.style.top = `${topCenterPosition}px`;
456
+ this.node.style.left = `${nodeCenter - popoverWidth - this.options.padding}px`;
457
+ this.node.style.right = '';
458
+ this.node.style.bottom = '';
459
+
460
+ // Add the tip at the top center
461
+ this.tipNode.classList.add('mid-center');
462
+ }
463
+
464
  /**
465
  * Automatically positions the popover around the given position
466
  * such that the element and popover remain in view
src/driver.scss CHANGED
@@ -99,6 +99,10 @@ div#driver-popover-item {
99
  right: 20px;
100
  }
101
  }
 
 
 
 
102
  }
103
 
104
  .driver-popover-footer {
 
99
  right: 20px;
100
  }
101
  }
102
+
103
+ &.mid-center {
104
+ display: none;
105
+ }
106
  }
107
 
108
  .driver-popover-footer {
types/index.d.ts CHANGED
@@ -555,6 +555,12 @@ declare module 'driver.js' {
555
  */
556
  private positionOnBottomRight(position: Driver.Position): void;
557
 
 
 
 
 
 
 
558
  /**
559
  * Positions the popover automatically around the element position
560
  * @param {Driver.Position} position
 
555
  */
556
  private positionOnBottomRight(position: Driver.Position): void;
557
 
558
+ /**
559
+ * Positions the popover to the middle center of the given element position
560
+ * @param {Driver.Position} position
561
+ */
562
+ private positionOnMidCenter(position: Driver.Position): void;
563
+
564
  /**
565
  * Positions the popover automatically around the element position
566
  * @param {Driver.Position} position