kamrify commited on
Commit
5ddc2a6
·
1 Parent(s): d5be768

Add position on top-center and top-right

Browse files
Files changed (3) hide show
  1. demo/scripts/demo.js +1 -4
  2. src/core/popover.js +51 -2
  3. src/driver.scss +8 -0
demo/scripts/demo.js CHANGED
@@ -8,21 +8,18 @@ document.addEventListener('DOMContentLoaded', function () {
8
  title: 'Before we start',
9
  description: 'This is just one use-case, make sure to check out the rest of the docs below.',
10
  nextBtnText: 'Okay, Start!',
11
- position: 'right-bottom'
12
  },
13
  }, {
14
  element: '#logo_img',
15
  popover: {
16
  title: 'Focus Anything',
17
  description: 'You can use it to highlight literally anything, images, text, div, span, li etc.',
18
- position: 'right-bottom',
19
  },
20
  }, {
21
  element: '#name_driver',
22
  popover: {
23
  title: 'Why Driver?',
24
  description: 'Because it lets you drive the user across the page',
25
- position: 'right-bottom'
26
  }
27
  }, {
28
  element: '#driver-demo-head',
@@ -35,7 +32,7 @@ document.addEventListener('DOMContentLoaded', function () {
35
  element: '#highlight_feature',
36
  popover: {
37
  title: 'Highlight Feature',
38
- description: 'You may use it to highlight single elements (with or without popover) e.g. like facebook does while creating posts'
39
  }
40
  }, {
41
  element: '#feature_introductions_feature',
 
8
  title: 'Before we start',
9
  description: 'This is just one use-case, make sure to check out the rest of the docs below.',
10
  nextBtnText: 'Okay, Start!',
 
11
  },
12
  }, {
13
  element: '#logo_img',
14
  popover: {
15
  title: 'Focus Anything',
16
  description: 'You can use it to highlight literally anything, images, text, div, span, li etc.',
 
17
  },
18
  }, {
19
  element: '#name_driver',
20
  popover: {
21
  title: 'Why Driver?',
22
  description: 'Because it lets you drive the user across the page',
 
23
  }
24
  }, {
25
  element: '#driver-demo-head',
 
32
  element: '#highlight_feature',
33
  popover: {
34
  title: 'Highlight Feature',
35
+ description: 'You may use it to highlight single elements (with or without popover) e.g. like facebook does while creating posts',
36
  }
37
  }, {
38
  element: '#feature_introductions_feature',
src/core/popover.js CHANGED
@@ -147,8 +147,15 @@ export default class Popover extends Element {
147
  this.positionOnRightBottom(position);
148
  break;
149
  case 'top':
 
150
  this.positionOnTop(position);
151
  break;
 
 
 
 
 
 
152
  case 'bottom':
153
  case 'bottom-left':
154
  this.positionOnBottom(position);
@@ -329,6 +336,47 @@ export default class Popover extends Element {
329
  this.tipNode.classList.add('bottom');
330
  }
331
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332
  /**
333
  * Shows the popover on the bottom of the given position
334
  * @param {Position} elementPosition
@@ -370,11 +418,12 @@ export default class Popover extends Element {
370
  * @private
371
  */
372
  positionOnBottomRight(elementPosition) {
 
373
  const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
374
 
375
  this.node.style.top = `${elementPosition.bottom + popoverMargin}px`;
376
- this.node.style.right = `${elementPosition.left - this.options.padding}px`;
377
- this.node.style.left = '';
378
  this.node.style.bottom = '';
379
 
380
  // Add the tip at the top center
 
147
  this.positionOnRightBottom(position);
148
  break;
149
  case 'top':
150
+ case 'top-left':
151
  this.positionOnTop(position);
152
  break;
153
+ case 'top-center':
154
+ this.positionOnTopCenter(position);
155
+ break;
156
+ case 'top-right':
157
+ this.positionOnTopRight(position);
158
+ break;
159
  case 'bottom':
160
  case 'bottom-left':
161
  this.positionOnBottom(position);
 
336
  this.tipNode.classList.add('bottom');
337
  }
338
 
339
+ /**
340
+ * Shows the popover on the top center of the given position
341
+ * @param {Position} elementPosition
342
+ * @private
343
+ */
344
+ positionOnTopCenter(elementPosition) {
345
+ const dimensions = this.getSize();
346
+ const popoverHeight = dimensions.height;
347
+ const popoverWidth = dimensions.width / 2;
348
+
349
+ const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
350
+ const nodeCenter = elementPosition.left + ((elementPosition.right - elementPosition.left) / 2);
351
+
352
+ this.node.style.top = `${elementPosition.top - popoverHeight - popoverMargin}px`;
353
+ this.node.style.left = `${nodeCenter - popoverWidth - this.options.padding}px`;
354
+ this.node.style.right = '';
355
+ this.node.style.bottom = '';
356
+
357
+ // Add the tip at the top center
358
+ this.tipNode.classList.add('bottom', 'position-center');
359
+ }
360
+
361
+ /**
362
+ * Shows the popover on the top right of the given position
363
+ * @param {Position} elementPosition
364
+ * @private
365
+ */
366
+ positionOnTopRight(elementPosition) {
367
+ const dimensions = this.getSize();
368
+ const popoverHeight = dimensions.height;
369
+ const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
370
+
371
+ this.node.style.top = `${elementPosition.top - popoverHeight - popoverMargin}px`;
372
+ this.node.style.left = `${(elementPosition.right + this.options.padding) - dimensions.width}px`;
373
+ this.node.style.right = '';
374
+ this.node.style.bottom = '';
375
+
376
+ // Add the tip at the top center
377
+ this.tipNode.classList.add('bottom', 'position-right');
378
+ }
379
+
380
  /**
381
  * Shows the popover on the bottom of the given position
382
  * @param {Position} elementPosition
 
418
  * @private
419
  */
420
  positionOnBottomRight(elementPosition) {
421
+ const dimensions = this.getSize();
422
  const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
423
 
424
  this.node.style.top = `${elementPosition.bottom + popoverMargin}px`;
425
+ this.node.style.left = `${(elementPosition.right + this.options.padding) - dimensions.width}px`;
426
+ this.node.style.right = '';
427
  this.node.style.bottom = '';
428
 
429
  // Add the tip at the top center
src/driver.scss CHANGED
@@ -38,6 +38,14 @@ div#driver-popover-item {
38
  border-right-color: transparent;
39
  border-bottom-color: transparent;
40
  border-left-color: transparent;
 
 
 
 
 
 
 
 
41
  }
42
 
43
  &.left {
 
38
  border-right-color: transparent;
39
  border-bottom-color: transparent;
40
  border-left-color: transparent;
41
+
42
+ &.position-center {
43
+ left: 49%;
44
+ }
45
+
46
+ &.position-right {
47
+ right: 20px;
48
+ }
49
  }
50
 
51
  &.left {