Skip to content

Commit

Permalink
feat(line): delegate the mesh current element selection to the vorono…
Browse files Browse the repository at this point in the history
…i package
  • Loading branch information
plouc committed May 7, 2024
1 parent 5d8a0e8 commit 1f5ec63
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import * as React from 'react'
import { Interpolation, SpringConfig } from '@react-spring/web'
import { CurveFactory } from 'd3-shape'
import { ComponentType } from 'react'
import { extendDefaultTheme } from './src'

export type DatumValue = string | number | Date

Expand All @@ -24,6 +23,7 @@ export type Margin = {
right: number
top: number
}
export type Padding = Margin

export type Box = Partial<Margin>
export type BoxAlign =
Expand Down
19 changes: 7 additions & 12 deletions packages/line/src/Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ const Mesh = ({
[point.x + margin.left, point.y + margin.top],
'top'
)
setCurrent(point)
onMouseEnter && onMouseEnter(point, event)
},
[setCurrent, showTooltipAt, tooltip, onMouseEnter, margin]
[showTooltipAt, tooltip, onMouseEnter, margin]
)

const handleMouseMove = useCallback(
Expand All @@ -41,19 +40,17 @@ const Mesh = ({
[point.x + margin.left, point.y + margin.top],
'top'
)
setCurrent(point)
onMouseMove && onMouseMove(point, event)
},
[showTooltipAt, tooltip, margin.left, margin.top, setCurrent, onMouseMove]
[showTooltipAt, tooltip, margin.left, margin.top, onMouseMove]
)

const handleMouseLeave = useCallback(
(point, event) => {
hideTooltip()
setCurrent(null)
onMouseLeave && onMouseLeave(point, event)
},
[hideTooltip, setCurrent, onMouseLeave]
[hideTooltip, onMouseLeave]
)

const handleClick = useCallback(
Expand All @@ -70,10 +67,9 @@ const Mesh = ({
[point.x + margin.left, point.y + margin.top],
'top'
)
setCurrent(point)
onTouchStart && onTouchStart(point, event)
},
[margin.left, margin.top, onTouchStart, setCurrent, showTooltipAt, tooltip]
[margin.left, margin.top, onTouchStart, showTooltipAt, tooltip]
)

const handleTouchMove = useCallback(
Expand All @@ -83,26 +79,25 @@ const Mesh = ({
[point.x + margin.left, point.y + margin.top],
'top'
)
setCurrent(point)
onTouchMove && onTouchMove(point, event)
},
[margin.left, margin.top, onTouchMove, setCurrent, showTooltipAt, tooltip]
[margin.left, margin.top, onTouchMove, showTooltipAt, tooltip]
)

const handleTouchEnd = useCallback(
(point, event) => {
hideTooltip()
setCurrent(null)
onTouchEnd && onTouchEnd(point, event)
},
[onTouchEnd, hideTooltip, setCurrent]
[onTouchEnd, hideTooltip]
)

return (
<BaseMesh
nodes={points}
width={width}
height={height}
setCurrent={setCurrent}
onMouseEnter={handleMouseEnter}
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
Expand Down
21 changes: 15 additions & 6 deletions packages/voronoi/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,33 +316,42 @@ export const useMeshEvents = <Node, ElementType extends Element>({
(event: TouchEvent<ElementType>) => {
const match = findNode(event)

enableTouchCrosshair && setCurrent(match)
if (enableTouchCrosshair) {
setCurrent(match)
setCurrentNode?.(match ? match[1] : null)
}

match && onTouchStart?.(match[1], event)
},
[findNode, setCurrent, enableTouchCrosshair, onTouchStart]
[findNode, setCurrent, setCurrentNode, enableTouchCrosshair, onTouchStart]
)

const handleTouchMove = useCallback(
(event: TouchEvent<ElementType>) => {
const match = findNode(event)

enableTouchCrosshair && setCurrent(match)
if (enableTouchCrosshair) {
setCurrent(match)
setCurrentNode?.(match ? match[1] : null)
}

match && onTouchMove?.(match[1], event)
},
[findNode, setCurrent, enableTouchCrosshair, onTouchMove]
[findNode, setCurrent, setCurrentNode, enableTouchCrosshair, onTouchMove]
)

const handleTouchEnd = useCallback(
(event: TouchEvent<SVGRectElement>) => {
enableTouchCrosshair && setCurrent(null)
if (enableTouchCrosshair) {
setCurrent(null)
setCurrentNode?.(null)
}

if (onTouchEnd && previous.current) {
onTouchEnd(previous.current[1], event)
}
},
[enableTouchCrosshair, setCurrent, onTouchEnd, previous]
[enableTouchCrosshair, setCurrent, setCurrentNode, onTouchEnd, previous]
)

return {
Expand Down

0 comments on commit 1f5ec63

Please sign in to comment.