@@ -43,46 +43,32 @@ export default function Tooltip({children, label}: any) {
43
43
) ;
44
44
}
45
45
46
+ const TOOLTIP_OFFSET = 5 ;
47
+
46
48
// Method used to find the position of the tooltip based on current mouse position
47
49
function getTooltipPosition ( element , mousePosition ) {
48
50
const { height, mouseX, mouseY, width} = mousePosition ;
49
- const TOOLTIP_OFFSET_X = 5 ;
50
- const TOOLTIP_OFFSET_Y = 15 ;
51
51
let top = 0 ;
52
52
let left = 0 ;
53
53
54
- // Let's check the vertical position.
55
- if ( mouseY + TOOLTIP_OFFSET_Y + element . offsetHeight >= height ) {
56
- // The tooltip doesn't fit below the mouse cursor (which is our
57
- // default strategy). Therefore we try to position it either above the
58
- // mouse cursor or finally aligned with the window's top edge.
59
- if ( mouseY - TOOLTIP_OFFSET_Y - element . offsetHeight > 0 ) {
60
- // We position the tooltip above the mouse cursor if it fits there.
61
- top = `${ mouseY - element . offsetHeight - TOOLTIP_OFFSET_Y } px` ;
54
+ if ( mouseY + TOOLTIP_OFFSET + element . offsetHeight >= height ) {
55
+ if ( mouseY - TOOLTIP_OFFSET - element . offsetHeight > 0 ) {
56
+ top = `${ mouseY - element . offsetHeight - TOOLTIP_OFFSET } px` ;
62
57
} else {
63
- // Otherwise we align the tooltip with the window's top edge.
64
58
top = '0px' ;
65
59
}
66
60
} else {
67
- top = `${ mouseY + TOOLTIP_OFFSET_Y } px` ;
61
+ top = `${ mouseY + TOOLTIP_OFFSET } px` ;
68
62
}
69
63
70
- // Now let's check the horizontal position.
71
- if ( mouseX + TOOLTIP_OFFSET_X + element . offsetWidth >= width ) {
72
- // The tooltip doesn't fit at the right of the mouse cursor (which is
73
- // our default strategy). Therefore we try to position it either at the
74
- // left of the mouse cursor or finally aligned with the window's left
75
- // edge.
76
- if ( mouseX - TOOLTIP_OFFSET_X - element . offsetWidth > 0 ) {
77
- // We position the tooltip at the left of the mouse cursor if it fits
78
- // there.
79
- left = `${ mouseX - element . offsetWidth - TOOLTIP_OFFSET_X } px` ;
64
+ if ( mouseX + TOOLTIP_OFFSET + element . offsetWidth >= width ) {
65
+ if ( mouseX - TOOLTIP_OFFSET - element . offsetWidth > 0 ) {
66
+ left = `${ mouseX - element . offsetWidth - TOOLTIP_OFFSET } px` ;
80
67
} else {
81
- // Otherwise, align the tooltip with the window's left edge.
82
68
left = '0px' ;
83
69
}
84
70
} else {
85
- left = `${ mouseX + TOOLTIP_OFFSET_X * 2 } px` ;
71
+ left = `${ mouseX + TOOLTIP_OFFSET * 2 } px` ;
86
72
}
87
73
88
74
return { left, top} ;
@@ -94,9 +80,19 @@ function getMousePosition(
94
80
mouseEvent : SyntheticMouseEvent < * > ,
95
81
) {
96
82
if ( relativeContainer !== null ) {
97
- const { height , top , width } = relativeContainer . getBoundingClientRect ( ) ;
83
+ // Positon within the nearest position:relative container.
84
+ let targetContainer = relativeContainer ;
85
+ while ( targetContainer . parentElement != null ) {
86
+ if ( targetContainer . style . position === 'relative' ) {
87
+ break ;
88
+ } else {
89
+ targetContainer = targetContainer . parentElement ;
90
+ }
91
+ }
92
+
93
+ const { height , left , top , width } = targetContainer . getBoundingClientRect ( ) ;
98
94
99
- const mouseX = mouseEvent . clientX ;
95
+ const mouseX = mouseEvent . clientX - left ;
100
96
const mouseY = mouseEvent . clientY - top ;
101
97
102
98
return { height, mouseX, mouseY, width} ;
0 commit comments