1
+ function dimension ( el ) {
2
+ const rect = el . getBoundingClientRect ( ) ;
3
+ return { width : rect . width , height : rect . height } ;
4
+ }
5
+
1
6
/**
2
7
* Returns a position of given DOM element.
3
8
*
@@ -25,29 +30,29 @@ export function position(el) {
25
30
* Calculates a position of the tooltip.
26
31
*
27
32
* @param {string } place - 'top', 'right', 'bottom', or 'left'.
28
- * @param {Object } tooltip - DOM element.
33
+ * @param {Object } content - DOM element that contains a content .
29
34
* @param {Object } origin - DOM element.
30
35
* @return {Object } contains 'top', 'left', and extra keys.
31
36
*/
32
- export function placement ( place , tooltip , origin ) {
37
+ export function placement ( place , content , origin ) {
33
38
const gap = 12 ;
34
- const tip = position ( tooltip ) ;
39
+ const dim = dimension ( content ) ;
35
40
const pos = position ( origin ) ;
36
41
37
- let offset = { width : tip . width , height : tip . height } ;
42
+ let offset = { width : dim . width , height : dim . height } ;
38
43
39
44
switch ( place ) {
40
45
case 'top' : case 'bottom' :
41
- offset . left = `${ pos . left + ( pos . width * 0.5 ) - ( tip . width * 0.5 ) } px` ;
46
+ offset . left = `${ pos . left + ( pos . width * 0.5 ) - ( dim . width * 0.5 ) } px` ;
42
47
break ;
43
48
case 'left' : case 'right' :
44
- offset . top = `${ pos . top + ( pos . height * 0.5 ) - ( tip . height * 0.5 ) } px` ;
49
+ offset . top = `${ pos . top + ( pos . height * 0.5 ) - ( dim . height * 0.5 ) } px` ;
45
50
break ;
46
51
}
47
52
48
53
switch ( place ) {
49
54
case 'top' :
50
- offset . top = `${ pos . top - tip . height - gap } px` ;
55
+ offset . top = `${ pos . top - dim . height - gap } px` ;
51
56
break ;
52
57
case 'right' :
53
58
offset . left = `${ pos . right + gap } px` ;
@@ -56,7 +61,7 @@ export function placement(place, tooltip, origin) {
56
61
offset . top = `${ pos . top + pos . height + gap } px` ;
57
62
break ;
58
63
case 'left' :
59
- offset . left = `${ pos . left - tip . width - gap } px` ;
64
+ offset . left = `${ pos . left - dim . width - gap } px` ;
60
65
break ;
61
66
}
62
67
@@ -185,12 +190,11 @@ export function overDirs(tip, el) {
185
190
/**
186
191
* Places and adjusts a tooltip.
187
192
*
188
- * @param {Object } tooltip - DOM element.
189
- * @param {string|Array } place
190
- * @param {Object } origin - DOM element.
193
+ * @param {Object } content - DOM element which contans a content.
194
+ * @param {Object } props
191
195
* @return {Object } 'offset': style data to locate, 'place': final direction of the tooltip
192
196
*/
193
- export function adjust ( tooltip , props ) {
197
+ export function adjust ( content , props ) {
194
198
const { el : origin , auto, within } = props ;
195
199
let { place } = props ;
196
200
if ( typeof place === 'string' ) {
@@ -204,7 +208,7 @@ export function adjust(tooltip, props) {
204
208
const tries = [ ...place ] ;
205
209
while ( 0 < tries . length ) {
206
210
current = tries . shift ( ) ;
207
- pos = placement ( current , tooltip , origin ) ;
211
+ pos = placement ( current , content , origin ) ;
208
212
if ( typeof first === 'undefined' ) {
209
213
first = { offset : pos , place : current } ;
210
214
}
0 commit comments