1
- import { useMemo , memo } from 'react'
1
+ import { useMemo , memo , useCallback } from 'react'
2
2
import * as React from 'react'
3
3
import { useSpring , useTransition , animated } from '@react-spring/web'
4
4
import { useTheme , useMotionConfig } from '@nivo/core'
@@ -7,7 +7,7 @@ import { computeCartesianTicks, getFormatter } from '../compute'
7
7
import { AxisTick } from './AxisTick'
8
8
import { AxisProps } from '../types'
9
9
10
- const Axis = < Value extends ScaleValue > ( {
10
+ export const NonMemoizedAxis = < Value extends ScaleValue > ( {
11
11
axis,
12
12
scale,
13
13
x = 0 ,
@@ -34,6 +34,7 @@ const Axis = <Value extends ScaleValue>({
34
34
onClick ?: ( event : React . MouseEvent < SVGGElement , MouseEvent > , value : Value | string ) => void
35
35
} ) => {
36
36
const theme = useTheme ( )
37
+ const legendTextStyle = theme . axis . legend . text
37
38
38
39
const formatValue = useMemo ( ( ) => getFormatter ( format , scale ) , [ format , scale ] )
39
40
@@ -80,16 +81,33 @@ const Axis = <Value extends ScaleValue>({
80
81
}
81
82
82
83
legendNode = (
83
- < text
84
- transform = { `translate(${ legendX } , ${ legendY } ) rotate(${ legendRotation } )` }
85
- textAnchor = { textAnchor }
86
- style = { {
87
- dominantBaseline : 'central' ,
88
- ...theme . axis . legend . text ,
89
- } }
90
- >
91
- { legend }
92
- </ text >
84
+ < >
85
+ { legendTextStyle . outlineWidth > 0 && (
86
+ < text
87
+ transform = { `translate(${ legendX } , ${ legendY } ) rotate(${ legendRotation } )` }
88
+ textAnchor = { textAnchor }
89
+ style = { {
90
+ dominantBaseline : 'central' ,
91
+ ...legendTextStyle ,
92
+ } }
93
+ strokeWidth = { legendTextStyle . outlineWidth * 2 }
94
+ stroke = { legendTextStyle . outlineColor }
95
+ strokeLinejoin = "round"
96
+ >
97
+ { legend }
98
+ </ text >
99
+ ) }
100
+ < text
101
+ transform = { `translate(${ legendX } , ${ legendY } ) rotate(${ legendRotation } )` }
102
+ textAnchor = { textAnchor }
103
+ style = { {
104
+ dominantBaseline : 'central' ,
105
+ ...legendTextStyle ,
106
+ } }
107
+ >
108
+ { legend }
109
+ </ text >
110
+ </ >
93
111
)
94
112
}
95
113
@@ -103,31 +121,32 @@ const Axis = <Value extends ScaleValue>({
103
121
immediate : ! animate ,
104
122
} )
105
123
106
- const transition = useTransition <
107
- ( typeof ticks ) [ 0 ] ,
108
- { opacity : number ; transform : string ; textTransform : string }
109
- > ( ticks , {
110
- keys : tick => tick . key ,
111
- initial : tick => ( {
124
+ const getAnimatedProps = useCallback (
125
+ ( tick : ( typeof ticks ) [ 0 ] ) => ( {
112
126
opacity : 1 ,
113
127
transform : `translate(${ tick . x } ,${ tick . y } )` ,
114
128
textTransform : `translate(${ tick . textX } ,${ tick . textY } ) rotate(${ tickRotation } )` ,
115
129
} ) ,
116
- from : tick => ( {
130
+ [ tickRotation ]
131
+ )
132
+ const getFromAnimatedProps = useCallback (
133
+ ( tick : ( typeof ticks ) [ 0 ] ) => ( {
117
134
opacity : 0 ,
118
135
transform : `translate(${ tick . x } ,${ tick . y } )` ,
119
136
textTransform : `translate(${ tick . textX } ,${ tick . textY } ) rotate(${ tickRotation } )` ,
120
137
} ) ,
121
- enter : tick => ( {
122
- opacity : 1 ,
123
- transform : `translate(${ tick . x } ,${ tick . y } )` ,
124
- textTransform : `translate(${ tick . textX } ,${ tick . textY } ) rotate(${ tickRotation } )` ,
125
- } ) ,
126
- update : tick => ( {
127
- opacity : 1 ,
128
- transform : `translate(${ tick . x } ,${ tick . y } )` ,
129
- textTransform : `translate(${ tick . textX } ,${ tick . textY } ) rotate(${ tickRotation } )` ,
130
- } ) ,
138
+ [ tickRotation ]
139
+ )
140
+
141
+ const transition = useTransition <
142
+ ( typeof ticks ) [ 0 ] ,
143
+ { opacity : number ; transform : string ; textTransform : string }
144
+ > ( ticks , {
145
+ keys : tick => tick . key ,
146
+ initial : getAnimatedProps ,
147
+ from : getFromAnimatedProps ,
148
+ enter : getAnimatedProps ,
149
+ update : getAnimatedProps ,
131
150
leave : {
132
151
opacity : 0 ,
133
152
} ,
@@ -161,6 +180,4 @@ const Axis = <Value extends ScaleValue>({
161
180
)
162
181
}
163
182
164
- const memoizedAxis = memo ( Axis ) as typeof Axis
165
-
166
- export { memoizedAxis as Axis }
183
+ export const Axis = memo ( NonMemoizedAxis ) as typeof NonMemoizedAxis
0 commit comments