-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFullSwipeComponent.js
281 lines (243 loc) · 8.45 KB
/
FullSwipeComponent.js
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
269
270
271
272
273
274
275
276
277
278
279
280
281
'use strict';
const Dimensions = require('Dimensions');
const window = Dimensions.get('window');
const React = require('react-native');
const {
StyleSheet,
View,
PanResponder,
LayoutAnimation,
Text,
Image,
TouchableOpacity
} = React;
/*
FullSwipe takes these attributes:
text (title)
menuItems(array of objects...)
onPress (action when pressed)
fontFamily
fontSize,
backgroundColor,
menuBackgroundColor (background color of underlying menu)
menuItem Objects have these attributes:
title (displayed below icon, if icon - must be 14 or less characters)
icon (link to image)
onPress (action when pressed)
menuItems will only show the first items four in an array. Not able to scroll yet.
If it has more than four, it errors and shows 0.
TODO: Add unit tests
TODO: Bug, fix swipe open and close functions to run when swiped fully open.
TODO: Optimize for horizontal view.
*/
const menuWidth = window.width * .9;
const forceMin = menuWidth * .6;
const forceMinClose = menuWidth * .3;
let lastState = false;
import IconComponent from './components/IconComponent'
export default React.createClass({
propTypes: {
title: React.PropTypes.string,
height: React.PropTypes.number,
menuItems: React.PropTypes.arrayOf(React.PropTypes.object),
menuBackgroundColor: React.PropTypes.string,
fontSize: React.PropTypes.number,
fontFamily: React.PropTypes.string,
fontWeight: React.PropTypes.string,
backgroundColor: React.PropTypes.string,
backgroundImage: React.PropTypes.string,
icon: React.PropTypes.string,
borderRadiusLeft: React.PropTypes.number,
borderRadiusRight: React.PropTypes.number,
onPress: React.PropTypes.func,
onSwipeOpenAction: React.PropTypes.func,
onSwipeCloseAction: React.PropTypes.func,
},
getDefaultProps: function () {
return {
height: 100,
menuBackgroundColor: '#F2F2F2',
fontSize: 17,
fontFamily: 'Avenir Next',
backgroundColor: '#FFF',
borderRadiusLeft: 0,
borderRadiusRight: 0,
letterSpacing: 2,
onPress: function () {
console.log("No onPress defined")
},
onSwipeOpenAction: function() {
console.log("No onSwipeOpenAction defined")
},
onSwipeCloseAction: function() {
console.log("No onSwipeCloseAction defined")
}
}
},
getInitialState: function () {
return {
open: false
}
},
componentWillMount: function () {
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: this.handleStart,
onPanResponderGrant: this.handleGrant,
onMoveShouldSetPanResponder: this.handleStart,
onPanResponderMove: this.handleMove,
onPanResponderRelease: this.handleEnd
});
this.prevLeft = 0;
this.boxStyle = {
left: this.prevLeft
};
},
componentDidMount: function () {
this.updatePosition();
},
handleStart: function (e, gestureState) {
return Math.abs(gestureState.dx) > Math.abs(gestureState.dy) && Math.abs(gestureState.dx) > 10
},
handleGrant: function () {
},
handleEnd: function (e, gestureState) {
this.boxStyle.left = this.state.open ? menuWidth : 0;
this.updatePosition();
this.prevLeft += this.boxStyle.left;
},
updatePosition: function () {
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
this.refs.box.setNativeProps({
style: this.boxStyle
});
console.log(`Open: ${this.state.open}`);
},
handleMove: function (e, gestureState) {
const panningRight = gestureState.dx > 10;
if (panningRight && this.state.open) {
return;
}
if (!panningRight && !this.state.open) {
return;
}
if (panningRight && !this.state.open) {
//If opening
this.boxStyle.left = this.prevLeft + gestureState.dx;
if (Math.abs(gestureState.dx) > forceMin) {
let open = !this.state.open;
this.setState({open});
this.boxStyle.left = open ? menuWidth : 0;
}
}
//If closing
if (Math.abs(gestureState.dx) > forceMinClose) {
this.boxStyle.left = this.prevLeft + gestureState.dx;
let open = !this.state.open;
this.setState({open});
this.boxStyle.left = open ? menuWidth : 0;
}
this.updatePosition();
//This is to make sure the onSwipeOpen and onSwipeClose actions run
// when you've encountered a FULL swipe, not partial.
if(this.state.open && lastState != this.state.open) {
this.props.onSwipeOpenAction();
lastState = !lastState
} else if (!this.state.open && lastState === true) {
this.props.onSwipeCloseAction();
lastState = !lastState
}
},
render: function () {
const {
title,height,onPress,fontFamily,icon,
fontSize,backgroundColor,menuBackgroundColor,letterSpacing, fontWeight,
borderRadiusLeft, borderRadiusRight, iconLeft
} = this.props;
const borderTopLeftRadius = borderRadiusLeft;
const borderBottomLeftRadius = borderRadiusLeft;
const borderTopRightRadius = borderRadiusRight;
const borderBottomRightRadius = borderRadiusRight;
//TODO: BackgroundImage not working. {/* <Image source={{uri:backgroundImage}} style={[{resizeMode: 'cover'}]}/> */}
const menuItems = (() => {
if (!this.props.menuItems) {
return [];
} else if (this.props.menuItems.length > 4) {
return this.props.menuItems.filter((item, it) => it <= 3)
} else {
return this.props.menuItems
}
})();
const itemWidth = menuWidth / menuItems.length;
function getDistance(width) {
let i = 0;
return function () {
return width * i++
}
}
const leftDistance = getDistance(itemWidth);
return (
<View style={[styles.underMenu, {backgroundColor: this.state.open ? menuBackgroundColor : 'rgba(0,0,0,0)', borderTopLeftRadius, borderBottomLeftRadius, borderTopRightRadius, borderBottomRightRadius}]}>
{/* Container component for menu items UNDER the cover */}
{ menuItems.map((item, it) => {
let distance = leftDistance();
//Items under cover
return (
<IconComponent key={it}
onPress={item.onPress}
itemWidth={itemWidth}
height={height}
icon={item.icon}
title={item.title}
titleColor={item.titleColor}
backgroundColor={menuBackgroundColor}
distance={distance}
borderTopLeftRadius={borderTopLeftRadius}
borderBottomLeftRadius={borderBottomLeftRadius}
borderTopRightRadius={borderTopRightRadius}
borderBottomRightRadius={borderBottomRightRadius}
opacity={this.state.open ? 1 : 0}
open={this.state.open}
fontSize={item.fontSize}
/>
)
}
)}
<View ref="box"
style={[styles.viewStyle, {height, borderTopLeftRadius, borderBottomLeftRadius, borderTopRightRadius, borderBottomRightRadius, width: window.width, backgroundColor: this.state.open ? 'rgba(255,255,255,0.5)' : backgroundColor}]} {...this.panResponder.panHandlers}>
{/* Container for cover */}
<TouchableOpacity
style={[styles.backdroppable, styles.menuClickable,{height, borderTopLeftRadius, borderBottomLeftRadius, borderTopRightRadius, borderBottomRightRadius}]}
onPress={() => {
if (!this.state.open) {
onPress();
}
}}>
<View style={[{width: menuWidth, borderTopLeftRadius, borderBottomLeftRadius, borderTopRightRadius, borderBottomRightRadius}]}>
{icon ?
(<Image style={[styles.icon, {height: height * .9, paddingLeft: menuWidth}]} source={{uri:icon}}/>)
:
(<Text style={[{fontFamily, fontWeight, fontSize, letterSpacing, textAlign: 'center'}]}>{title}</Text>)
}
</View>
</TouchableOpacity>
</View>
</View>
);
}
});
const styles = StyleSheet.create({
menuClickable: {
width: window.width,
alignItems: 'center',
justifyContent: 'center',
},
icon: {
resizeMode: 'contain',
},
underMenu: {
flexDirection: 'row',
},
backdroppable: {
backgroundColor: 'rgba(0,0,0,0)',
}
});