1
1
// TODO: support hitSlop
2
2
// TODO: fix issue where passing backgroundColor thru style doesn't work
3
3
// TODO: fix issue with the default value of feedbackColor 'transparent'
4
- import React , { Component } from 'react' ;
4
+ import React , { PureComponent } from 'react' ;
5
5
import { processColor } from 'react-native' ;
6
6
import PropTypes from 'prop-types' ;
7
7
import _ from 'lodash' ;
@@ -14,21 +14,21 @@ const {
14
14
Code,
15
15
cond,
16
16
and,
17
- not,
18
17
eq,
18
+ neq,
19
+ interpolate,
20
+ Extrapolate,
19
21
Value,
20
22
call,
21
23
block,
22
24
event,
23
25
timing,
24
- debug,
25
- clockRunning,
26
26
set,
27
27
startClock,
28
28
stopClock
29
29
} = Reanimated ;
30
30
31
- class TouchableOpacity extends Component {
31
+ class TouchableOpacity extends PureComponent {
32
32
static propTypes = {
33
33
backgroundColor : PropTypes . string ,
34
34
feedbackColor : PropTypes . string ,
@@ -48,13 +48,12 @@ class TouchableOpacity extends Component {
48
48
pressState : new Value ( - 1 )
49
49
} ;
50
50
51
+ _prevPressState = new Value ( - 1 ) ;
51
52
isAnimating = new Value ( 0 ) ;
52
53
clock = new Clock ( ) ;
53
- _scale = new Value ( 1 ) ;
54
- // _color = new Value(1);
55
-
56
- _opacity = block ( [ cond ( eq ( this . pressState , State . BEGAN ) , this . props . activeOpacity , 1 ) ] ) ;
57
54
55
+ _scale = runTiming ( this . clock , this . pressState , this . props . activeScale , 1 ) ;
56
+ _opacity = runTiming ( this . clock , this . pressState , this . props . activeOpacity , 1 ) ;
58
57
_color = cond ( eq ( this . pressState , State . BEGAN ) ,
59
58
processColor ( this . props . feedbackColor || this . backgroundColor ) ,
60
59
processColor ( this . backgroundColor ) ) ;
@@ -91,16 +90,11 @@ class TouchableOpacity extends Component {
91
90
{ useNativeDriver : true } ) ;
92
91
93
92
render ( ) {
94
- const { modifiers, style, activeScale , onPress, forwardedRef, ...others } = this . props ;
93
+ const { modifiers, style, onPress, forwardedRef, ...others } = this . props ;
95
94
const { borderRadius, paddings, margins, alignments, flexStyle, backgroundColor} = modifiers ;
96
95
97
96
return (
98
- < TapGestureHandler
99
- onHandlerStateChange = { this . onStateChange }
100
- shouldCancelWhenOutside
101
- ref = { forwardedRef }
102
- maxDurationMs = { 500 }
103
- >
97
+ < TapGestureHandler onHandlerStateChange = { this . onStateChange } shouldCancelWhenOutside ref = { forwardedRef } >
104
98
< Reanimated . View
105
99
{ ...others }
106
100
style = { [
@@ -112,39 +106,30 @@ class TouchableOpacity extends Component {
112
106
backgroundColor && { backgroundColor} ,
113
107
style ,
114
108
this . animatedStyle
115
- // {backgroundColor: this._color, opacity: this._opacity, transform: [{scale: this._scale}] }
116
109
] }
117
110
>
118
111
{ this . props . children }
119
112
120
113
< Code >
121
- { ( ) =>
122
- block ( [
123
- // trigger onPress callback on END state once
124
- cond ( and ( eq ( this . isAnimating , 0 ) , eq ( this . pressState , State . END ) ) , [
125
- set ( this . isAnimating , 1 ) ,
126
-
114
+ { ( ) => {
115
+ return block ( [
116
+ cond ( and ( eq ( this . pressState , State . END ) , eq ( this . _prevPressState , State . BEGAN ) ) , [
127
117
call ( [ ] , ( ) => onPress ( this . props ) )
128
118
] ) ,
129
- // Active state - scale animation
130
- cond ( eq ( this . pressState , State . BEGAN ) , block ( [ runTiming ( this . clock , this . _scale , 1 , activeScale ) ] ) ) ,
131
- // End state - scale animation
132
- cond ( eq ( this . pressState , State . END ) , block ( [ runTiming ( this . clock , this . _scale , activeScale , 1 ) ] ) ) ,
133
- // Reset isAnimating flag
134
- cond ( and ( eq ( this . pressState , State . END ) , not ( clockRunning ( this . clock ) ) ) , set ( this . isAnimating , 0 ) )
135
- ] )
136
- }
119
+ set ( this . _prevPressState , this . pressState )
120
+ ] ) ;
121
+ } }
137
122
</ Code >
138
123
</ Reanimated . View >
139
124
</ TapGestureHandler >
140
125
) ;
141
126
}
142
127
}
143
128
144
- function runTiming ( clock , position , value , dest ) {
129
+ function runTiming ( clock , gestureState , initialValue , endValue ) {
145
130
const state = {
146
131
finished : new Value ( 0 ) ,
147
- position,
132
+ position : new Value ( 0 ) ,
148
133
time : new Value ( 0 ) ,
149
134
frameTime : new Value ( 0 )
150
135
} ;
@@ -156,26 +141,27 @@ function runTiming(clock, position, value, dest) {
156
141
} ;
157
142
158
143
return block ( [
159
- cond ( clockRunning ( clock ) ,
160
- [
161
- // if the clock is already running we update the toValue, in case a new dest has been passed in
162
- set ( config . toValue , dest )
163
- ] ,
164
- [
165
- // if the clock isn't running we reset all the animation params and start the clock
166
- set ( state . finished , 0 ) ,
167
- set ( state . time , 0 ) ,
168
- set ( state . position , value ) ,
169
- set ( state . frameTime , 0 ) ,
170
- set ( config . toValue , dest ) ,
171
- startClock ( clock )
172
- ] ) ,
173
- // we run the step here that is going to update position
144
+ cond ( and ( eq ( gestureState , State . BEGAN ) , neq ( config . toValue , 1 ) ) , [
145
+ set ( state . finished , 0 ) ,
146
+ set ( state . time , 0 ) ,
147
+ set ( state . frameTime , 0 ) ,
148
+ set ( config . toValue , 1 ) ,
149
+ startClock ( clock )
150
+ ] ) ,
151
+ cond ( and ( eq ( gestureState , State . END ) , neq ( config . toValue , 0 ) ) , [
152
+ set ( state . finished , 0 ) ,
153
+ set ( state . time , 0 ) ,
154
+ set ( state . frameTime , 0 ) ,
155
+ set ( config . toValue , 0 ) ,
156
+ startClock ( clock )
157
+ ] ) ,
174
158
timing ( clock , state , config ) ,
175
- // if the animation is over we stop the clock
176
- cond ( state . finished , debug ( 'stop clock' , stopClock ( clock ) ) ) ,
177
- // we made the block return the updated position
178
- state . position
159
+ cond ( state . finished , stopClock ( clock ) ) ,
160
+ interpolate ( state . position , {
161
+ inputRange : [ 0 , 1 ] ,
162
+ outputRange : [ endValue , initialValue ] ,
163
+ extrapolate : Extrapolate . CLAMP
164
+ } )
179
165
] ) ;
180
166
}
181
167
0 commit comments