-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathripple.feedback.ios.tsx
268 lines (241 loc) · 8.41 KB
/
ripple.feedback.ios.tsx
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import * as React from 'react';
import {
Animated,
Easing,
View,
TouchableWithoutFeedback,
StyleSheet,
} from 'react-native';
/* eslint-enable import/no-unresolved, import/extensions */
const styles = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,
overflow: 'hidden',
},
});
/**
* Usualy we use width and height to compute this. In case, the width of container is too big
* we use this constant as a width of ripple effect.
*/
const MAX_DIAMETER = 200;
const ELEVATION_ZINDEX = 1;
interface Props {
onPress: () => any;
}
interface State {
scaleValue: Animated.Value;
opacityRippleValue: Animated.Value;
opacityBackgroundValue: Animated.Value;
diameter: number;
maxOpacity: number;
pressX: number;
pressY: number;
}
class RippleFeedbackIOS extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
// https://material.google.com/components/buttons.html#buttons-toggle-buttons
this.state = {
scaleValue: new Animated.Value(0),
opacityRippleValue: new Animated.Value(1),
opacityBackgroundValue: new Animated.Value(0),
diameter: MAX_DIAMETER,
maxOpacity: 1,
pressX: 0,
pressY: 0,
};
}
private onLayoutChanged = (event: any) => {
try {
// get width and height of wrapper
const {
nativeEvent: {
layout: { width, height },
},
} = event;
const diameter = Math.ceil(
Math.sqrt(width * width + height * height)
);
this.setState({
diameter: Math.min(diameter, MAX_DIAMETER),
});
} catch (e) {
this.setState({
diameter: MAX_DIAMETER,
});
}
};
private onLongPress = () => {
const { maxOpacity, opacityBackgroundValue } = this.state;
// Long press has to be indicated like this because we need to animate containers back to
// default values in onPressOut function
// Animation of long press is slightly different than onPress animation
Animated.timing(opacityBackgroundValue, {
toValue: maxOpacity / 2,
duration: 700,
useNativeDriver: true,
}).start();
};
private onPress = () => {
const {
maxOpacity,
diameter,
opacityBackgroundValue,
opacityRippleValue,
scaleValue,
} = this.state;
Animated.parallel([
// Display background layer thru whole over the view
Animated.timing(opacityBackgroundValue, {
toValue: maxOpacity / 2,
duration: 125 + diameter,
easing: Easing.in(Easing.quad),
useNativeDriver: true,
}),
// Opacity of ripple effect starts on maxOpacity and goes to 0
Animated.timing(opacityRippleValue, {
toValue: 0,
duration: 125 + diameter,
useNativeDriver: true,
}),
// Scale of ripple effect starts at 0 and goes to 1
Animated.timing(scaleValue, {
toValue: 1,
duration: 125 + diameter,
easing: Easing.out(Easing.quad),
useNativeDriver: true,
}),
]).start(() => {
// After the effect is fully displayed we need background to be animated back to default
Animated.timing(opacityBackgroundValue, {
toValue: 0,
duration: 225,
easing: Easing.out(Easing.quad),
useNativeDriver: true,
}).start();
this.setDefaultAnimatedValues();
});
};
private onPressIn = (event: any) => {
// because we need ripple effect to be displayed exactly from press point
this.setState({
pressX: event.nativeEvent.locationX,
pressY: event.nativeEvent.locationY,
});
};
private onPressOut = () => {
const { diameter } = this.state;
const {
opacityBackgroundValue,
opacityRippleValue,
scaleValue,
} = this.state;
// When user use onPress all animation happens in onPress method. But when user use long
// press. We displaye background layer in onLongPress and then we need to animate ripple
// effect that is done here.
Animated.parallel([
// Hide opacity background layer, slowly. It has to be done later than ripple
// effect
Animated.timing(opacityBackgroundValue, {
toValue: 0,
duration: 500 + diameter,
useNativeDriver: true,
}),
// Opacity of ripple effect starts on maxOpacity and goes to 0
Animated.timing(opacityRippleValue, {
toValue: 0,
duration: 125 + diameter,
useNativeDriver: true,
}),
// Scale of ripple effect starts at 0 and goes to 1
Animated.timing(scaleValue, {
toValue: 1,
duration: 125 + diameter,
easing: Easing.out(Easing.quad),
useNativeDriver: true,
}),
]).start(this.setDefaultAnimatedValues);
};
private setDefaultAnimatedValues = () => {
const { maxOpacity, scaleValue, opacityRippleValue } = this.state;
// We can set up scale to 0 and opacity back to maxOpacity
scaleValue.setValue(0);
opacityRippleValue.setValue(maxOpacity);
};
private renderRippleView = () => {
const {
scaleValue,
opacityRippleValue,
diameter,
pressX,
pressY,
} = this.state;
return (
// we need set zindex for iOS, because the components with elevation have the
// zindex set as well, thus, there could be displayed backgroundColor of
// component with bigger zindex - and that's not good
<Animated.View
key="ripple-view"
pointerEvents="none"
style={[
{
position: 'absolute',
top: (pressY || 0) - diameter / 2,
left: (pressX || 0) - diameter / 2,
width: diameter,
height: diameter,
borderRadius: diameter / 2,
transform: [{ scale: scaleValue }],
opacity: opacityRippleValue,
backgroundColor: '#ffffff',
zIndex: ELEVATION_ZINDEX,
},
]}
/>
);
};
private renderOpacityBackground = () => {
const { opacityBackgroundValue /*rippleColor*/ } = this.state;
return (
// we need set zindex for iOS, because the components with elevation have the
// zindex set as well, thus, there could be displayed backgroundColor of
// component with bigger zindex - and that's not good
<Animated.View
key="ripple-opacity"
pointerEvents="none"
style={[
{
...StyleSheet.absoluteFillObject,
opacity: opacityBackgroundValue,
backgroundColor: '#ffffff',
zIndex: ELEVATION_ZINDEX,
},
]}
/>
);
};
render() {
const { children } = this.props;
const ripple = (
<View key="ripple-feedback-layer" pointerEvents="none">
{this.renderOpacityBackground()}
{this.renderRippleView()}
</View>
);
return (
<TouchableWithoutFeedback
onLayout={this.onLayoutChanged}
onPressIn={this.onPressIn}
onLongPress={this.onLongPress}
onPressOut={this.onPressOut}
onPress={() => {
this.onPress();
this.props.onPress();
}}
>
{this.props.children}
</TouchableWithoutFeedback>
);
}
}
export default RippleFeedbackIOS;