-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathTooltip.tsx
More file actions
195 lines (167 loc) · 5.01 KB
/
Tooltip.tsx
File metadata and controls
195 lines (167 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React from 'react'
import { useAnchor } from '../hooks/useAnchor'
import useLatestWhen from '../hooks/useLatestWhen'
import usePortalElement from '../hooks/usePortalElement'
import usePrevious from '../hooks/usePrevious'
import { Datum, ResolvedTooltipOptions, TooltipOptions } from '../types'
//
import useChartContext from '../utils/chartContext'
import TooltipRenderer from './TooltipRenderer'
import useRect from '../hooks/useRect'
import { useSpring } from '../hooks/useSpring'
import Portal from './Portal';
//
export function defaultTooltip<TDatum>(
options: boolean | TooltipOptions<TDatum> = {}
): ResolvedTooltipOptions<TDatum> {
if (options === true) {
options = { show: true }
} else if (options === false) {
options = { show: false }
}
return {
show: true,
...options,
align: options.align ?? 'auto',
alignPriority: options.alignPriority ?? [
'right',
'topRight',
'bottomRight',
'left',
'topLeft',
'bottomLeft',
'top',
'bottom',
],
padding: options.padding ?? 5,
arrowPadding: options.arrowPadding ?? 7,
// anchor: options.anchor ?? 'closest',
render: options.render ?? TooltipRenderer,
}
}
export default function Tooltip<TDatum>(): JSX.Element | null {
const {
focusedDatumState,
getOptions,
primaryAxis,
secondaryAxes,
getDatumStatusStyle,
svgRef,
} = useChartContext<TDatum>()
const [focusedDatum] = focusedDatumState
const latestFocusedDatum = useLatestWhen(focusedDatum, !!focusedDatum)
const secondaryAxis =
secondaryAxes.find(d => d.id === latestFocusedDatum?.secondaryAxisId) ??
secondaryAxes[0]
const portalEl = usePortalElement()
const tooltipRef = React.useRef<HTMLDivElement | null>(null)
const tooltipInnerRef = React.useRef<HTMLDivElement | null>(null)
const svgRect = useRect(svgRef.current, !!focusedDatum?.element)
const anchorEl = React.useMemo(() => {
const anchorRect =
latestFocusedDatum?.element?.getBoundingClientRect() ?? null
if (!anchorRect) {
return null
}
if (!svgRect) return
const translateX = anchorRect.left ?? 0
const translateY = anchorRect.top ?? 0
const width = anchorRect.width ?? 0
const height = anchorRect.height ?? 0
const el = document.createElement('div')
el.getBoundingClientRect = () =>
({
x: translateX,
y: translateY,
width: width,
height: height,
top: translateY,
left: translateX,
bottom: translateY + width,
right: translateX + height,
} as any)
return el
}, [latestFocusedDatum?.element, svgRect])
const anchor = useAnchor({
show: !!focusedDatum,
portalEl,
anchorEl,
tooltipEl: tooltipInnerRef.current,
side: ['right', 'left', 'top', 'bottom'],
})
const previousAnchor = usePrevious(anchor)
const latestStableAnchor = useLatestWhen(anchor, !!anchor.fit) ?? anchor
const { visibility, ...anchorStyle } = latestStableAnchor.style
const immediate = Number.isNaN(previousAnchor?.style.left)
const tooltipXSpring = useSpring(
anchorStyle.left || 0,
[1, 210, 30],
() => {
if (tooltipRef.current) {
tooltipRef.current.style.transform = `translate(${tooltipXSpring.x()}px, ${tooltipYSpring.x()}px)`
}
},
immediate
)
const tooltipYSpring = useSpring(
anchorStyle.top || 0,
[1, 210, 30],
() => {
if (tooltipRef.current) {
tooltipRef.current.style.transform = `translate(${tooltipXSpring.x()}px, ${tooltipYSpring.x()}px)`
}
},
immediate
)
// const springProps = useSpring({
// ...anchorStyle,
// left: anchorStyle.left || 0,
// top: anchorStyle.top || 0,
// config: { mass: 1, tension: 210, friction: 30 },
// immediate: key => {
// if (['left', 'top'].includes(key)) {
// return Number.isNaN(previousAnchor?.style.left)
// }
// return false
// },
// })
const show = getOptions().tooltip.show
const latestFit = useLatestWhen(anchor.fit, !!anchor.fit)
return show && portalEl
? (<Portal>
<div
ref={tooltipRef}
style={{
position: anchorStyle.position,
opacity: !!focusedDatum ? 1 : 0,
transition: 'opacity .3s ease',
}}
>
<div
ref={() => tooltipInnerRef}
style={{
fontFamily: 'sans-serif',
...(latestFit?.startKey === 'left'
? {
padding: '0 10px',
}
: {
padding: '10px 0',
}),
}}
>
{getOptions().tooltip.render({
getOptions,
focusedDatum: latestFocusedDatum,
primaryAxis,
secondaryAxes,
secondaryAxis,
getDatumStyle: (datum: Datum<TDatum>) =>
getDatumStatusStyle(datum, focusedDatum),
anchor,
})}
</div>
</div>
</Portal>)
: null
}