forked from plouc/nivo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArcShape.tsx
More file actions
66 lines (59 loc) · 2.03 KB
/
Copy pathArcShape.tsx
File metadata and controls
66 lines (59 loc) · 2.03 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
import React, { useCallback } from 'react'
import { SpringValue, Interpolation, animated } from '@react-spring/web'
import { DatumWithArcAndColor } from './types'
export type ArcMouseHandler<Datum extends DatumWithArcAndColor> = (
datum: Datum,
event: React.MouseEvent<SVGPathElement>
) => void
export interface ArcShapeProps<Datum extends DatumWithArcAndColor> {
datum: Datum
style: {
opacity: SpringValue<number>
color: SpringValue<string>
borderWidth: number
borderColor: SpringValue<string>
path: Interpolation<string>
}
onClick?: ArcMouseHandler<Datum>
onMouseEnter?: ArcMouseHandler<Datum>
onMouseMove?: ArcMouseHandler<Datum>
onMouseLeave?: ArcMouseHandler<Datum>
}
/**
* A simple arc component to be used typically with an `ArcsLayer`.
*
* Please note that the component accepts `SpringValue`s instead of
* regular values to support animations.
*/
export const ArcShape = <Datum extends DatumWithArcAndColor>({
datum,
style,
onClick,
onMouseEnter,
onMouseMove,
onMouseLeave,
}: ArcShapeProps<Datum>) => {
const handleClick = useCallback(event => onClick?.(datum, event), [onClick, datum])
const handleMouseEnter = useCallback(event => onMouseEnter?.(datum, event), [
onMouseEnter,
datum,
])
const handleMouseMove = useCallback(event => onMouseMove?.(datum, event), [onMouseMove, datum])
const handleMouseLeave = useCallback(event => onMouseLeave?.(datum, event), [
onMouseLeave,
datum,
])
return (
<animated.path
d={style.path}
opacity={style.opacity}
fill={datum.fill || style.color}
stroke={style.borderColor}
strokeWidth={style.borderWidth}
onClick={onClick ? handleClick : undefined}
onMouseEnter={onMouseEnter ? handleMouseEnter : undefined}
onMouseMove={onMouseMove ? handleMouseMove : undefined}
onMouseLeave={onMouseLeave ? handleMouseLeave : undefined}
/>
)
}