@@ -20,6 +20,13 @@ type Event = Readonly<{
20
20
} ;
21
21
} > ;
22
22
23
+ type AnimationValues = {
24
+ val : Animated . Value ;
25
+ min : Animated . Value ;
26
+ max : Animated . Value ;
27
+ diff : Animated . Value ;
28
+ } ;
29
+
23
30
export interface Props {
24
31
value : number ;
25
32
minimumValue : number ;
@@ -72,15 +79,29 @@ const RCTSliderWebComponent = React.forwardRef(
72
79
const hasBeenResized = React . useRef ( false ) ;
73
80
const [ value , setValue ] = React . useState ( initialValue || minimumValue ) ;
74
81
const lastInitialValue = React . useRef < number > ( ) ;
82
+ const animationValues = React . useRef < AnimationValues > ( {
83
+ val : new Animated . Value ( value ) ,
84
+ min : new Animated . Value ( minimumValue ) ,
85
+ max : new Animated . Value ( maximumValue ) ,
86
+ // make sure we never divide by 0
87
+ diff : new Animated . Value ( maximumValue - minimumValue || 1 ) ,
88
+ } ) . current ;
89
+
90
+ // update minimumValue & maximumValue animations
91
+ React . useEffect ( ( ) => {
92
+ animationValues . min . setValue ( minimumValue ) ;
93
+ animationValues . max . setValue ( maximumValue ) ;
94
+ // make sure we never divide by 0
95
+ animationValues . diff . setValue ( maximumValue - minimumValue || 1 ) ;
96
+ } , [ animationValues , minimumValue , maximumValue ] ) ;
75
97
76
98
// compute animated slider position based on animated value
77
- const animatedValue = React . useRef ( new Animated . Value ( value ) ) . current ;
78
99
const minPercent = React . useRef (
79
100
Animated . multiply (
80
101
new Animated . Value ( 100 ) ,
81
102
Animated . divide (
82
- Animated . subtract ( animatedValue , new Animated . Value ( minimumValue ) ) ,
83
- new Animated . Value ( maximumValue - minimumValue ) ,
103
+ Animated . subtract ( animationValues . val , animationValues . min ) ,
104
+ animationValues . diff ,
84
105
) ,
85
106
) ,
86
107
) . current ;
@@ -141,9 +162,9 @@ const RCTSliderWebComponent = React.forwardRef(
141
162
if ( initialValue !== lastInitialValue . current ) {
142
163
lastInitialValue . current = initialValue ;
143
164
const newValue = updateValue ( initialValue ) ;
144
- animatedValue . setValue ( newValue ) ;
165
+ animationValues . val . setValue ( newValue ) ;
145
166
}
146
- } , [ initialValue , updateValue , animatedValue ] ) ;
167
+ } , [ initialValue , updateValue , animationValues ] ) ;
147
168
148
169
const onResize = ( ) => {
149
170
hasBeenResized . current = true ;
@@ -243,13 +264,13 @@ const RCTSliderWebComponent = React.forwardRef(
243
264
244
265
const onTouchEnd = ( { nativeEvent} : GestureResponderEvent ) => {
245
266
const newValue = updateValue ( getValueFromNativeEvent ( nativeEvent . pageX ) ) ;
246
- animatedValue . setValue ( newValue ) ;
267
+ animationValues . val . setValue ( newValue ) ;
247
268
onSlidingComplete ( newValue ) ;
248
269
} ;
249
270
250
271
const onMove = ( { nativeEvent} : GestureResponderEvent ) => {
251
272
const newValue = getValueFromNativeEvent ( nativeEvent . pageX ) ;
252
- animatedValue . setValue ( newValue ) ;
273
+ animationValues . val . setValue ( newValue ) ;
253
274
updateValue ( newValue ) ;
254
275
} ;
255
276
0 commit comments