Skip to content

Commit 3ea4911

Browse files
committed
feat(line): delegate the mesh current element selection to the voronoi package
1 parent 664647a commit 3ea4911

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

packages/core/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as React from 'react'
22
import { Interpolation, SpringConfig } from '@react-spring/web'
33
import { CurveFactory } from 'd3-shape'
44
import { ComponentType } from 'react'
5-
import { extendDefaultTheme } from './src'
65

76
export type DatumValue = string | number | Date
87

@@ -24,6 +23,7 @@ export type Margin = {
2423
right: number
2524
top: number
2625
}
26+
export type Padding = Margin
2727

2828
export type Box = Partial<Margin>
2929
export type BoxAlign =

packages/line/src/Mesh.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ const Mesh = ({
2828
[point.x + margin.left, point.y + margin.top],
2929
'top'
3030
)
31-
setCurrent(point)
3231
onMouseEnter && onMouseEnter(point, event)
3332
},
34-
[setCurrent, showTooltipAt, tooltip, onMouseEnter, margin]
33+
[showTooltipAt, tooltip, onMouseEnter, margin]
3534
)
3635

3736
const handleMouseMove = useCallback(
@@ -41,19 +40,17 @@ const Mesh = ({
4140
[point.x + margin.left, point.y + margin.top],
4241
'top'
4342
)
44-
setCurrent(point)
4543
onMouseMove && onMouseMove(point, event)
4644
},
47-
[showTooltipAt, tooltip, margin.left, margin.top, setCurrent, onMouseMove]
45+
[showTooltipAt, tooltip, margin.left, margin.top, onMouseMove]
4846
)
4947

5048
const handleMouseLeave = useCallback(
5149
(point, event) => {
5250
hideTooltip()
53-
setCurrent(null)
5451
onMouseLeave && onMouseLeave(point, event)
5552
},
56-
[hideTooltip, setCurrent, onMouseLeave]
53+
[hideTooltip, onMouseLeave]
5754
)
5855

5956
const handleClick = useCallback(
@@ -70,10 +67,9 @@ const Mesh = ({
7067
[point.x + margin.left, point.y + margin.top],
7168
'top'
7269
)
73-
setCurrent(point)
7470
onTouchStart && onTouchStart(point, event)
7571
},
76-
[margin.left, margin.top, onTouchStart, setCurrent, showTooltipAt, tooltip]
72+
[margin.left, margin.top, onTouchStart, showTooltipAt, tooltip]
7773
)
7874

7975
const handleTouchMove = useCallback(
@@ -83,26 +79,25 @@ const Mesh = ({
8379
[point.x + margin.left, point.y + margin.top],
8480
'top'
8581
)
86-
setCurrent(point)
8782
onTouchMove && onTouchMove(point, event)
8883
},
89-
[margin.left, margin.top, onTouchMove, setCurrent, showTooltipAt, tooltip]
84+
[margin.left, margin.top, onTouchMove, showTooltipAt, tooltip]
9085
)
9186

9287
const handleTouchEnd = useCallback(
9388
(point, event) => {
9489
hideTooltip()
95-
setCurrent(null)
9690
onTouchEnd && onTouchEnd(point, event)
9791
},
98-
[onTouchEnd, hideTooltip, setCurrent]
92+
[onTouchEnd, hideTooltip]
9993
)
10094

10195
return (
10296
<BaseMesh
10397
nodes={points}
10498
width={width}
10599
height={height}
100+
setCurrent={setCurrent}
106101
onMouseEnter={handleMouseEnter}
107102
onMouseMove={handleMouseMove}
108103
onMouseLeave={handleMouseLeave}

packages/voronoi/src/hooks.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,33 +316,42 @@ export const useMeshEvents = <Node, ElementType extends Element>({
316316
(event: TouchEvent<ElementType>) => {
317317
const match = findNode(event)
318318

319-
enableTouchCrosshair && setCurrent(match)
319+
if (enableTouchCrosshair) {
320+
setCurrent(match)
321+
setCurrentNode?.(match ? match[1] : null)
322+
}
320323

321324
match && onTouchStart?.(match[1], event)
322325
},
323-
[findNode, setCurrent, enableTouchCrosshair, onTouchStart]
326+
[findNode, setCurrent, setCurrentNode, enableTouchCrosshair, onTouchStart]
324327
)
325328

326329
const handleTouchMove = useCallback(
327330
(event: TouchEvent<ElementType>) => {
328331
const match = findNode(event)
329332

330-
enableTouchCrosshair && setCurrent(match)
333+
if (enableTouchCrosshair) {
334+
setCurrent(match)
335+
setCurrentNode?.(match ? match[1] : null)
336+
}
331337

332338
match && onTouchMove?.(match[1], event)
333339
},
334-
[findNode, setCurrent, enableTouchCrosshair, onTouchMove]
340+
[findNode, setCurrent, setCurrentNode, enableTouchCrosshair, onTouchMove]
335341
)
336342

337343
const handleTouchEnd = useCallback(
338344
(event: TouchEvent<SVGRectElement>) => {
339-
enableTouchCrosshair && setCurrent(null)
345+
if (enableTouchCrosshair) {
346+
setCurrent(null)
347+
setCurrentNode?.(null)
348+
}
340349

341350
if (onTouchEnd && previous.current) {
342351
onTouchEnd(previous.current[1], event)
343352
}
344353
},
345-
[enableTouchCrosshair, setCurrent, onTouchEnd, previous]
354+
[enableTouchCrosshair, setCurrent, setCurrentNode, onTouchEnd, previous]
346355
)
347356

348357
return {

0 commit comments

Comments
 (0)