@@ -12,22 +12,32 @@ import {useLayoutEffect, useRef} from 'react';
12
12
const TOOLTIP_OFFSET = 4 ;
13
13
14
14
export default function useSmartTooltip ( {
15
+ canvasRef,
15
16
mouseX,
16
17
mouseY,
17
18
} : {
19
+ canvasRef : { | current : HTMLCanvasElement | null | } ,
18
20
mouseX : number ,
19
21
mouseY : number ,
20
22
} ) {
21
23
const ref = useRef < HTMLElement | null > ( null ) ;
22
24
25
+ // HACK: Browser extension reports window.innerHeight of 0,
26
+ // so we fallback to using the tooltip target element.
27
+ let height = window . innerHeight ;
28
+ let width = window . innerWidth ;
29
+ const target = canvasRef . current ;
30
+ if ( target !== null ) {
31
+ const rect = target . getBoundingClientRect ( ) ;
32
+ height = rect . top + rect . height ;
33
+ width = rect . left + rect . width ;
34
+ }
35
+
23
36
useLayoutEffect ( ( ) => {
24
37
const element = ref . current ;
25
38
if ( element !== null ) {
26
39
// Let's check the vertical position.
27
- if (
28
- mouseY + TOOLTIP_OFFSET + element . offsetHeight >=
29
- window . innerHeight
30
- ) {
40
+ if ( mouseY + TOOLTIP_OFFSET + element . offsetHeight >= height ) {
31
41
// The tooltip doesn't fit below the mouse cursor (which is our
32
42
// default strategy). Therefore we try to position it either above the
33
43
// mouse cursor or finally aligned with the window's top edge.
@@ -45,7 +55,7 @@ export default function useSmartTooltip({
45
55
}
46
56
47
57
// Now let's check the horizontal position.
48
- if ( mouseX + TOOLTIP_OFFSET + element . offsetWidth >= window . innerWidth ) {
58
+ if ( mouseX + TOOLTIP_OFFSET + element . offsetWidth >= width ) {
49
59
// The tooltip doesn't fit at the right of the mouse cursor (which is
50
60
// our default strategy). Therefore we try to position it either at the
51
61
// left of the mouse cursor or finally aligned with the window's left
0 commit comments