Skip to content

Commit ec8b3e6

Browse files
committed
Center tooltip
1 parent 5421a68 commit ec8b3e6

File tree

2 files changed

+5
-39
lines changed

2 files changed

+5
-39
lines changed

assets/js/dashboard/components/graph-tooltip.tsx

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

assets/js/dashboard/stats/graph/main-graph.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,6 @@ const MainGraphTooltip = ({
319319
y={y}
320320
minWidth={200}
321321
maxX={maxX}
322-
bucketIndex={bucketIndex}
323-
totalBuckets={totalBuckets}
324322
isTouchDevice={isTouchDevice}
325323
className={
326324
'absolute select-none pointer-events-none bg-gray-800 dark:bg-gray-950 py-3 px-4 rounded-md z-[100] shadow shadow-gray-200 dark:shadow-gray-850'

0 commit comments

Comments
 (0)