1
1
import { memo } from 'react'
2
- import { TransitionMotion , spring } from 'react-motion '
3
- import { interpolateColor } from '@nivo/colors '
4
- import { useMotionConfig } from '@nivo/core '
2
+ import { useTransition } from '@ react-spring/web '
3
+ import { useMotionConfig , useTheme } from '@nivo/core '
4
+ import { useInheritedColor } from '@nivo/colors '
5
5
import { ChordArc } from './ChordArc'
6
- import { ArcDatum , ArcGenerator , ChordCommonProps } from './types'
6
+ import { ArcDatum , ArcGenerator , ChordCommonProps , ArcAnimatedProps } from './types'
7
7
8
8
interface ChordArcsProps {
9
9
arcs : ArcDatum [ ]
10
10
arcGenerator : ArcGenerator
11
11
borderWidth : ChordCommonProps [ 'arcBorderWidth' ]
12
- getBorderColor : ( arc : ArcDatum ) => string
12
+ borderColor : ChordCommonProps [ 'arcBorderColor' ]
13
13
getOpacity : ( arc : ArcDatum ) => number
14
14
setCurrent : ( arc : ArcDatum | null ) => void
15
15
isInteractive : ChordCommonProps [ 'isInteractive' ]
@@ -24,7 +24,7 @@ export const ChordArcs = memo(
24
24
( {
25
25
arcs,
26
26
borderWidth,
27
- getBorderColor ,
27
+ borderColor ,
28
28
getOpacity,
29
29
arcGenerator,
30
30
setCurrent,
@@ -35,82 +35,65 @@ export const ChordArcs = memo(
35
35
onClick,
36
36
tooltip,
37
37
} : ChordArcsProps ) => {
38
- const { animate, springConfig : _springConfig } = useMotionConfig ( )
38
+ const { animate, config : springConfig } = useMotionConfig ( )
39
39
40
- if ( ! animate ) {
41
- return (
42
- < >
43
- { arcs . map ( arc => {
44
- return (
45
- < ChordArc
46
- key = { arc . id }
47
- arc = { arc }
48
- arcGenerator = { arcGenerator }
49
- startAngle = { arc . startAngle }
50
- endAngle = { arc . endAngle }
51
- opacity = { getOpacity ( arc ) }
52
- borderWidth = { borderWidth }
53
- getBorderColor = { getBorderColor }
54
- isInteractive = { isInteractive }
55
- setCurrent = { setCurrent }
56
- onMouseEnter = { onMouseEnter }
57
- onMouseMove = { onMouseMove }
58
- onMouseLeave = { onMouseLeave }
59
- onClick = { onClick }
60
- tooltip = { tooltip }
61
- />
62
- )
63
- } ) }
64
- </ >
65
- )
66
- }
40
+ const theme = useTheme ( )
41
+ const getBorderColor = useInheritedColor ( borderColor , theme )
67
42
68
- const springConfig = {
69
- ..._springConfig ,
70
- precision : 0.001 ,
71
- }
43
+ const transition = useTransition < ArcDatum , ArcAnimatedProps > ( arcs , {
44
+ keys : arc => arc . id ,
45
+ initial : arc => ( {
46
+ startAngle : arc . startAngle ,
47
+ endAngle : arc . endAngle ,
48
+ color : arc . color ,
49
+ opacity : getOpacity ( arc ) ,
50
+ borderColor : getBorderColor ( arc ) ,
51
+ } ) ,
52
+ from : arc => ( {
53
+ startAngle : arc . startAngle ,
54
+ endAngle : arc . endAngle ,
55
+ color : arc . color ,
56
+ opacity : 0 ,
57
+ borderColor : getBorderColor ( arc ) ,
58
+ } ) ,
59
+ update : arc => ( {
60
+ startAngle : arc . startAngle ,
61
+ endAngle : arc . endAngle ,
62
+ color : arc . color ,
63
+ opacity : getOpacity ( arc ) ,
64
+ borderColor : getBorderColor ( arc ) ,
65
+ } ) ,
66
+ leave : arc => ( {
67
+ startAngle : arc . startAngle ,
68
+ endAngle : arc . endAngle ,
69
+ color : arc . color ,
70
+ opacity : 0 ,
71
+ borderColor : getBorderColor ( arc ) ,
72
+ } ) ,
73
+ expires : true ,
74
+ config : springConfig ,
75
+ immediate : ! animate ,
76
+ } )
72
77
73
78
return (
74
- < TransitionMotion
75
- styles = { arcs . map ( arc => {
76
- return {
77
- key : arc . id ,
78
- data : arc ,
79
- style : {
80
- startAngle : spring ( arc . startAngle , springConfig ) ,
81
- endAngle : spring ( arc . endAngle , springConfig ) ,
82
- opacity : spring ( getOpacity ( arc ) , springConfig ) ,
83
- ...interpolateColor ( arc . color , springConfig ) ,
84
- } ,
85
- }
86
- } ) }
87
- >
88
- { interpolatedStyles => (
89
- < >
90
- { interpolatedStyles . map ( ( { key, style, data : arc } ) => {
91
- return (
92
- < ChordArc
93
- key = { key }
94
- arc = { arc }
95
- arcGenerator = { arcGenerator }
96
- startAngle = { style . startAngle }
97
- endAngle = { style . endAngle }
98
- opacity = { style . opacity }
99
- borderWidth = { borderWidth }
100
- getBorderColor = { getBorderColor }
101
- isInteractive = { isInteractive }
102
- setCurrent = { setCurrent }
103
- onMouseEnter = { onMouseEnter }
104
- onMouseMove = { onMouseMove }
105
- onMouseLeave = { onMouseLeave }
106
- onClick = { onClick }
107
- tooltip = { tooltip }
108
- />
109
- )
110
- } ) }
111
- </ >
112
- ) }
113
- </ TransitionMotion >
79
+ < >
80
+ { transition ( ( animatedProps , arc ) => (
81
+ < ChordArc
82
+ key = { arc . id }
83
+ arc = { arc }
84
+ arcGenerator = { arcGenerator }
85
+ animatedProps = { animatedProps }
86
+ borderWidth = { borderWidth }
87
+ setCurrent = { setCurrent }
88
+ isInteractive = { isInteractive }
89
+ tooltip = { tooltip }
90
+ onMouseEnter = { onMouseEnter }
91
+ onMouseMove = { onMouseMove }
92
+ onMouseLeave = { onMouseLeave }
93
+ onClick = { onClick }
94
+ />
95
+ ) ) }
96
+ </ >
114
97
)
115
98
}
116
99
)
0 commit comments