@@ -6,8 +6,6 @@ export const GraphTooltipWrapper = ({
66 y,
77 maxX,
88 minWidth,
9- bucketIndex,
10- totalBuckets,
119 children,
1210 className,
1311 onClick,
@@ -17,55 +15,25 @@ export const GraphTooltipWrapper = ({
1715 y : number
1816 maxX : number
1917 minWidth : number
20- bucketIndex : number
21- totalBuckets : number
2218 children : ReactNode
2319 className ?: string
2420 onClick ?: ( ) => void
2521 isTouchDevice ?: boolean
2622} ) => {
2723 const ref = useRef < HTMLDivElement > ( null )
2824 // distance from cursor to tooltip edge
29- const offsetFromCursor = 4
30- // flip the tooltip to left of cursor if it would overflow on the right
31- // and keep it flipped to the left for all subsequent buckets
32- // even if they'd fit on the right (prevents excessive flips)
33- const [ firstFlippedToLeftOn , setFirstFlippedToLeftOn ] = useState <
34- number | null
35- > ( null )
25+ const offsetFromCursor = 32
3626 const [ measuredWidth , setMeasuredWidth ] = useState ( minWidth )
37- const position =
38- firstFlippedToLeftOn !== null && bucketIndex >= firstFlippedToLeftOn
39- ? 'leftOfCursor'
40- : 'rightOfCursor'
41- const rawLeft =
42- position === 'leftOfCursor'
43- ? x - offsetFromCursor - measuredWidth
44- : x + offsetFromCursor
45- // prevent tooltip from oveflowing on the left when flipped to left of cursor on smaller screens
27+ // center tooltip above the cursor, clamped to prevent left/right overflow
28+ const rawLeft = x - measuredWidth / 2
4629 const tooltipLeft = Math . max ( 0 , Math . min ( rawLeft , maxX - measuredWidth ) )
4730
48- useLayoutEffect ( ( ) => {
49- setFirstFlippedToLeftOn ( null )
50- } , [ totalBuckets ] )
51-
5231 useLayoutEffect ( ( ) => {
5332 if ( ! ref . current ) {
5433 return
5534 }
56- const w = ref . current . offsetWidth
57- setMeasuredWidth ( w )
58- const wouldOverflow = x + offsetFromCursor + w > maxX
59- setFirstFlippedToLeftOn ( ( prev ) => {
60- if ( wouldOverflow ) {
61- return prev === null ? bucketIndex : Math . min ( prev , bucketIndex )
62- }
63- if ( prev !== null && bucketIndex < prev ) {
64- return null
65- }
66- return prev
67- } )
68- } , [ x , maxX , bucketIndex ] )
35+ setMeasuredWidth ( ref . current . offsetWidth )
36+ } , [ children , className , minWidth ] )
6937
7038 return (
7139 < Transition
0 commit comments