@@ -23,26 +23,17 @@ export const isElementInViewport = (element, container = null, position) => {
23
23
const viewportWidth = window . innerWidth || document . documentElement . clientWidth ;
24
24
const viewportHeight = window . innerHeight || document . documentElement . clientHeight ;
25
25
26
- let isInsideViewport = (
27
- rect . bottom > 0 &&
28
- rect . top < viewportHeight &&
29
- rect . right > 0 &&
30
- rect . left < viewportWidth
31
- ) ;
26
+ let isInsideViewport = rect . bottom > 0 && rect . top < viewportHeight && rect . right > 0 && rect . left < viewportWidth ;
32
27
33
28
if ( container ) {
34
29
const containerRect = container . getBoundingClientRect ( ) ;
35
30
36
31
if ( position === 'top' || position === 'bottom' ) {
37
- isInsideViewport = (
38
- ( containerRect . bottom + containerRect . height ) < viewportHeight &&
39
- containerRect . top < viewportHeight
40
- ) ;
32
+ isInsideViewport =
33
+ containerRect . bottom + containerRect . height < viewportHeight && containerRect . top < viewportHeight ;
41
34
} else {
42
- isInsideViewport = (
43
- ( containerRect . right + containerRect . width ) < viewportWidth &&
44
- containerRect . left < viewportWidth
45
- ) ;
35
+ isInsideViewport =
36
+ containerRect . right + containerRect . width < viewportWidth && containerRect . left < viewportWidth ;
46
37
}
47
38
48
39
return isInsideViewport ;
@@ -56,25 +47,55 @@ export const computeTooltipPosition = (containerRef, tooltipRef, position, coord
56
47
return coords ;
57
48
}
58
49
50
+ let cumulativeOffsetTop = 0 ;
51
+ let cumulativeOffsetLeft = 0 ;
52
+
53
+ let fixedOffsetTop = 0 ;
54
+ let stickyOffsetTop = 0 ;
55
+ let fixedOffsetLeft = 0 ;
56
+
57
+ let currentElement = containerRef ;
58
+
59
+ while ( currentElement !== document . body ) {
60
+ const computedStyle = window . getComputedStyle ( currentElement ) ;
61
+ const elementPosition = computedStyle . position ;
62
+
63
+ if ( elementPosition === 'fixed' ) {
64
+ fixedOffsetTop += currentElement . getBoundingClientRect ( ) . top + window . scrollY ;
65
+ fixedOffsetLeft += currentElement . getBoundingClientRect ( ) . left + window . scrollX ;
66
+ } else if ( elementPosition === 'sticky' ) {
67
+ stickyOffsetTop += currentElement . getBoundingClientRect ( ) . top ;
68
+ fixedOffsetLeft += currentElement . getBoundingClientRect ( ) . left + window . scrollX ;
69
+ } else if ( elementPosition === 'absolute' || elementPosition === 'relative' ) {
70
+ cumulativeOffsetTop -= parseFloat ( computedStyle . top ) || 0 ;
71
+ cumulativeOffsetLeft -= parseFloat ( computedStyle . left ) || 0 ;
72
+ }
73
+
74
+ currentElement = currentElement . parentElement ;
75
+ }
76
+
59
77
const containerRect = containerRef . getBoundingClientRect ( ) ;
60
78
const tooltipRect = tooltipRef . getBoundingClientRect ( ) ;
61
79
80
+ let finalTop = containerRect . top + cumulativeOffsetTop + stickyOffsetTop - fixedOffsetTop ;
81
+ let finalLeft = containerRect . left + cumulativeOffsetLeft - fixedOffsetLeft ;
82
+
62
83
switch ( position ) {
63
84
case 'top' :
64
- coords . top = containerRect . top ;
65
- coords . left = containerRect . left + ( containerRect . width / 2 ) ;
85
+ coords . top = finalTop ;
86
+ coords . left = finalLeft + containerRect . width / 2 ;
66
87
break ;
67
88
case 'bottom' :
68
- coords . top = containerRect . top - tooltipRect . height ;
69
- coords . left = containerRect . left + ( containerRect . width / 2 ) ;
89
+ coords . top = finalTop - tooltipRect . height ;
90
+ coords . left = finalLeft + containerRect . width / 2 ;
70
91
break ;
71
92
case 'left' :
72
- coords . left = containerRect . left ;
73
- coords . top = containerRect . top + ( containerRect . height / 2 ) ;
93
+ coords . left = finalLeft ;
94
+ coords . top = finalTop + containerRect . height / 2 ;
74
95
break ;
75
96
case 'right' :
76
- coords . left = containerRect . right - tooltipRect . width ;
77
- coords . top = containerRect . top + ( containerRect . height / 2 ) ;
97
+ coords . left = finalLeft + containerRect . width - tooltipRect . width ;
98
+ coords . top = finalTop + containerRect . height / 2 ;
78
99
break ;
79
100
}
80
101
0 commit comments