|
1 | 1 | import React from 'react'
|
| 2 | +import ReactDOM from 'react-dom' |
2 | 3 | import * as TSTB from 'ts-toolbelt'
|
3 | 4 |
|
4 | 5 | import { animated, config, useSpring } from '@react-spring/web'
|
5 | 6 |
|
| 7 | +// import useIsScrolling from '../hooks/useIsScrolling' |
6 | 8 | import useLatestWhen from '../hooks/useLatestWhen'
|
7 |
| -import { CursorOptions, Datum } from '../types' |
| 9 | +import usePortalElement from '../hooks/usePortalElement' |
| 10 | +import { AxisTime, CursorOptions, Datum } from '../types' |
8 | 11 | import { translate } from '../utils/Utils'
|
9 | 12 | //
|
10 | 13 | import useChartContext from '../utils/chartContext'
|
@@ -75,6 +78,7 @@ function Cursor<TDatum>(props: {
|
75 | 78 | }) {
|
76 | 79 | const {
|
77 | 80 | getOptions,
|
| 81 | + svgRect, |
78 | 82 | gridDimensions,
|
79 | 83 | useFocusedDatumAtom,
|
80 | 84 | primaryAxis,
|
@@ -192,72 +196,84 @@ function Cursor<TDatum>(props: {
|
192 | 196 | }
|
193 | 197 | }
|
194 | 198 |
|
195 |
| - const formattedValue = axis.format(latestValue) |
| 199 | + const formattedValue = (axis as AxisTime<any>).formatters.cursor(latestValue) |
| 200 | + |
| 201 | + // const isScrolling = useIsScrolling(200) |
196 | 202 |
|
197 | 203 | const lineSpring = useSpring({
|
198 | 204 | transform: translate(lineStartX, lineStartY),
|
199 | 205 | width: `${lineWidth}px`,
|
200 | 206 | height: `${lineHeight}px`,
|
201 | 207 | config: config.stiff,
|
| 208 | + // immediate: isScrolling, |
202 | 209 | })
|
203 | 210 |
|
204 | 211 | const bubbleSpring = useSpring({
|
205 | 212 | transform: translate(bubbleX, bubbleY),
|
206 | 213 | config: config.stiff,
|
| 214 | + // immediate: isScrolling, |
207 | 215 | })
|
208 | 216 |
|
209 |
| - return ( |
210 |
| - <div |
211 |
| - style={{ |
212 |
| - pointerEvents: 'none', |
213 |
| - position: 'absolute', |
214 |
| - top: 0, |
215 |
| - left: 0, |
216 |
| - transform: translate(gridDimensions.gridX, gridDimensions.gridY), |
217 |
| - opacity: show ? 1 : 0, |
218 |
| - transition: 'all .3s ease', |
219 |
| - }} |
220 |
| - className="Cursor" |
221 |
| - > |
222 |
| - {/* Render the cursor line */} |
223 |
| - {props.options.showLine ? ( |
224 |
| - <animated.div |
225 |
| - style={{ |
226 |
| - ...lineSpring, |
227 |
| - position: 'absolute', |
228 |
| - top: 0, |
229 |
| - left: 0, |
230 |
| - background: getLineBackgroundColor(getOptions().dark), |
231 |
| - }} |
232 |
| - /> |
233 |
| - ) : null} |
234 |
| - {/* Render the cursor bubble */} |
235 |
| - {props.options.showLabel ? ( |
236 |
| - <animated.div |
| 217 | + const portalEl = usePortalElement() |
| 218 | + |
| 219 | + return portalEl |
| 220 | + ? ReactDOM.createPortal( |
| 221 | + <div |
237 | 222 | style={{
|
238 |
| - ...bubbleSpring, |
| 223 | + pointerEvents: 'none', |
239 | 224 | position: 'absolute',
|
240 | 225 | top: 0,
|
241 | 226 | left: 0,
|
| 227 | + transform: translate( |
| 228 | + svgRect.left + gridDimensions.gridX, |
| 229 | + svgRect.top + gridDimensions.gridY |
| 230 | + ), |
| 231 | + opacity: show ? 1 : 0, |
| 232 | + transition: 'all .3s ease', |
242 | 233 | }}
|
| 234 | + className="Cursor" |
243 | 235 | >
|
244 |
| - {/* Render the cursor label */} |
245 |
| - <div |
246 |
| - style={{ |
247 |
| - padding: '5px', |
248 |
| - fontSize: '10px', |
249 |
| - background: getBackgroundColor(getOptions().dark), |
250 |
| - color: getBackgroundColor(!getOptions().dark), |
251 |
| - borderRadius: '3px', |
252 |
| - position: 'relative', |
253 |
| - transform: `translate3d(${alignPctX}%, ${alignPctY}%, 0)`, |
254 |
| - whiteSpace: 'nowrap', |
255 |
| - }} |
256 |
| - > |
257 |
| - {formattedValue} |
258 |
| - </div> |
259 |
| - </animated.div> |
260 |
| - ) : null} |
261 |
| - </div> |
262 |
| - ) |
| 236 | + {/* Render the cursor line */} |
| 237 | + {props.options.showLine ? ( |
| 238 | + <animated.div |
| 239 | + style={{ |
| 240 | + ...lineSpring, |
| 241 | + position: 'absolute', |
| 242 | + top: 0, |
| 243 | + left: 0, |
| 244 | + background: getLineBackgroundColor(getOptions().dark), |
| 245 | + }} |
| 246 | + /> |
| 247 | + ) : null} |
| 248 | + {/* Render the cursor bubble */} |
| 249 | + {props.options.showLabel ? ( |
| 250 | + <animated.div |
| 251 | + style={{ |
| 252 | + ...bubbleSpring, |
| 253 | + position: 'absolute', |
| 254 | + top: 0, |
| 255 | + left: 0, |
| 256 | + }} |
| 257 | + > |
| 258 | + {/* Render the cursor label */} |
| 259 | + <div |
| 260 | + style={{ |
| 261 | + padding: '5px', |
| 262 | + fontSize: '10px', |
| 263 | + background: getBackgroundColor(getOptions().dark), |
| 264 | + color: getBackgroundColor(!getOptions().dark), |
| 265 | + borderRadius: '3px', |
| 266 | + position: 'relative', |
| 267 | + transform: `translate3d(${alignPctX}%, ${alignPctY}%, 0)`, |
| 268 | + whiteSpace: 'nowrap', |
| 269 | + }} |
| 270 | + > |
| 271 | + {formattedValue} |
| 272 | + </div> |
| 273 | + </animated.div> |
| 274 | + ) : null} |
| 275 | + </div>, |
| 276 | + portalEl |
| 277 | + ) |
| 278 | + : null |
263 | 279 | }
|
0 commit comments