1- import { useEffect , useRef } from "react" ;
1+ import { useEffect , useRef , useState } from "react" ;
22
33export type TooltipContentRenderer = ( payload ?: any ) => React . ReactNode ;
44
@@ -23,18 +23,22 @@ export const OdontogramTooltip: React.FC<OdontogramTooltipProps> = ({
2323 content,
2424} ) => {
2525 const ref = useRef < HTMLDivElement > ( null ) ;
26+ const [ coords , setCoords ] = useState ( { left : 0 , top : 0 } ) ;
2627
2728 useEffect ( ( ) => {
28- if ( ref . current && position ) {
29- const { x, y } = position ;
30- ref . current . style . left = `${ x + 10 } px` ;
31- ref . current . style . top = `${ y + 10 } px` ;
32- }
33- } , [ position ] ) ;
29+ if ( ! ref . current || ! position ) return ;
3430
35- if ( ! ( active && payload ) ) {
36- return null ;
37- }
31+ const tooltipBox = ref . current . getBoundingClientRect ( ) ;
32+ const { x, y } = position ;
33+
34+ // place tooltip above the hover point
35+ const left = x - tooltipBox . width / 2 ;
36+ const top = y - tooltipBox . height - 12 ; // space for arrow
37+
38+ setCoords ( { left, top } ) ;
39+ } , [ position , content , payload ] ) ;
40+
41+ if ( ! ( active && payload ) ) return null ;
3842
3943 return (
4044 < div
@@ -43,18 +47,23 @@ export const OdontogramTooltip: React.FC<OdontogramTooltipProps> = ({
4347 style = { {
4448 position : "fixed" ,
4549 pointerEvents : "none" ,
46- background : "rgba(0, 0, 0, 0.75 )" ,
50+ background : "rgba(0,0,0,0.85 )" ,
4751 color : "#fff" ,
4852 padding : "6px 10px" ,
4953 borderRadius : "6px" ,
5054 fontSize : "12px" ,
5155 lineHeight : 1.3 ,
5256 whiteSpace : "nowrap" ,
5357 zIndex : 1000 ,
54- transform : "translate(-50%, -100%)" ,
58+
59+ left : coords . left ,
60+ top : coords . top ,
61+
62+ opacity : active ? 1 : 0 ,
5563 transition : "opacity 0.15s ease" ,
5664 } }
5765 >
66+ { /* tooltip content */ }
5867 { getContent ( content , payload ) ?? (
5968 < >
6069 < div > Tooth: { payload ?. notations ?. fdi } </ div >
@@ -65,6 +74,22 @@ export const OdontogramTooltip: React.FC<OdontogramTooltipProps> = ({
6574 </ div >
6675 </ >
6776 ) }
77+
78+ { /* ARROW */ }
79+ < div
80+ className = "odontogram-tooltip-arrow"
81+ style = { {
82+ position : "absolute" ,
83+ bottom : "-6px" ,
84+ left : "50%" ,
85+ transform : "translateX(-50%)" ,
86+ width : 0 ,
87+ height : 0 ,
88+ borderLeft : "6px solid transparent" ,
89+ borderRight : "6px solid transparent" ,
90+ borderTop : "6px solid rgba(0, 0, 0, 0.85)" , // arrow color
91+ } }
92+ />
6893 </ div >
6994 ) ;
7095} ;
0 commit comments