Popover – Add position on left center
Browse files- demo/scripts/demo.js +3 -3
- src/core/popover.js +29 -0
- src/driver.scss +14 -6
demo/scripts/demo.js
CHANGED
@@ -8,21 +8,21 @@ 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: '
|
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: '
|
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: '
|
26 |
}
|
27 |
}, {
|
28 |
element: '#driver-demo-head',
|
|
|
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: 'left-center'
|
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: 'left-center',
|
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: 'left-center'
|
26 |
}
|
27 |
}, {
|
28 |
element: '#driver-demo-head',
|
src/core/popover.js
CHANGED
@@ -129,6 +129,9 @@ export default class Popover extends Element {
|
|
129 |
case 'left':
|
130 |
this.positionOnLeft(position);
|
131 |
break;
|
|
|
|
|
|
|
132 |
case 'right':
|
133 |
this.positionOnRight(position);
|
134 |
break;
|
@@ -200,6 +203,32 @@ export default class Popover extends Element {
|
|
200 |
this.tipNode.classList.add('right');
|
201 |
}
|
202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
/**
|
204 |
* Shows the popover on the right of the given position
|
205 |
* @param {Position} elementPosition
|
|
|
129 |
case 'left':
|
130 |
this.positionOnLeft(position);
|
131 |
break;
|
132 |
+
case 'left-center':
|
133 |
+
this.positionOnLeftCenter(position);
|
134 |
+
break;
|
135 |
case 'right':
|
136 |
this.positionOnRight(position);
|
137 |
break;
|
|
|
203 |
this.tipNode.classList.add('right');
|
204 |
}
|
205 |
|
206 |
+
/**
|
207 |
+
* Shows the popover on the left center of the given position
|
208 |
+
* @param {Position} elementPosition
|
209 |
+
* @private
|
210 |
+
*/
|
211 |
+
positionOnLeftCenter(elementPosition) {
|
212 |
+
const popoverDimensions = this.getSize();
|
213 |
+
|
214 |
+
const popoverWidth = popoverDimensions.width;
|
215 |
+
const popoverHeight = popoverDimensions.height;
|
216 |
+
const popoverCenter = popoverHeight / 2;
|
217 |
+
|
218 |
+
const popoverMargin = this.options.padding + 10; // adding 10 to give it a little distance from the element
|
219 |
+
|
220 |
+
const elementCenter = (elementPosition.bottom - elementPosition.top) / 2;
|
221 |
+
|
222 |
+
const centerPosition = (elementPosition.top - popoverCenter) + elementCenter;
|
223 |
+
|
224 |
+
this.node.style.left = `${elementPosition.left - popoverWidth - popoverMargin}px`;
|
225 |
+
this.node.style.top = `${centerPosition}px`;
|
226 |
+
this.node.style.right = '';
|
227 |
+
this.node.style.bottom = '';
|
228 |
+
|
229 |
+
this.tipNode.classList.add('right', 'position-center');
|
230 |
+
}
|
231 |
+
|
232 |
/**
|
233 |
* Shows the popover on the right of the given position
|
234 |
* @param {Position} elementPosition
|
src/driver.scss
CHANGED
@@ -56,6 +56,14 @@ div#driver-popover-item {
|
|
56 |
border-right-color: transparent;
|
57 |
border-bottom-color: transparent;
|
58 |
border-left-color: $popover-bg;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
}
|
60 |
|
61 |
&.top {
|
@@ -64,14 +72,14 @@ div#driver-popover-item {
|
|
64 |
border-right-color: transparent;
|
65 |
border-bottom-color: $popover-bg;
|
66 |
border-left-color: transparent;
|
67 |
-
}
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
|
74 |
-
|
|
|
75 |
}
|
76 |
}
|
77 |
|
|
|
56 |
border-right-color: transparent;
|
57 |
border-bottom-color: transparent;
|
58 |
border-left-color: $popover-bg;
|
59 |
+
|
60 |
+
&.position-center {
|
61 |
+
top: 46%;
|
62 |
+
}
|
63 |
+
|
64 |
+
&.position-bottom {
|
65 |
+
bottom: 20px;
|
66 |
+
}
|
67 |
}
|
68 |
|
69 |
&.top {
|
|
|
72 |
border-right-color: transparent;
|
73 |
border-bottom-color: $popover-bg;
|
74 |
border-left-color: transparent;
|
|
|
75 |
|
76 |
+
&.position-center {
|
77 |
+
left: 49%;
|
78 |
+
}
|
79 |
|
80 |
+
&.position-right {
|
81 |
+
right: 20px;
|
82 |
+
}
|
83 |
}
|
84 |
}
|
85 |
|