This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathoffcanvasReveal.js
208 lines (182 loc) · 5.32 KB
/
offcanvasReveal.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
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import {
Dimensions,
Text,
View,
StyleSheet,
Animated,
TouchableWithoutFeedback,
ScrollView,
BackAndroid
} from 'react-native'
class OffCanvasReveal extends Component {
constructor(props) {
super(props)
this._hardwareBackHandler = this._hardwareBackHandler.bind(this)
this.state = {
activityLeftPos : new Animated.Value(0),
animationDuration: 400,
stagArr: [],
animatedStagArr: [],
menuItems: this.props.menuItems,
activeMenu: 0
}
}
// staggered animation configuration for menu items
componentDidMount() {
let stagArrNew = []
for (let i = 0; i < this.state.menuItems.length; i++) stagArrNew.push(i)
this.setState({ stagArr: stagArrNew })
let animatedStagArrNew = []
stagArrNew.forEach((value) => {
animatedStagArrNew[value] = new Animated.Value(0)
})
this.setState({ animatedStagArr: animatedStagArrNew })
}
// any update to component will fire the animation
componentDidUpdate() {
this._animateStuffs()
if(this.props.handleBackPress && this.props.active) {
BackAndroid.addEventListener('hardwareBackPress', this._hardwareBackHandler)
}
if(this.props.handleBackPress && !this.props.active) {
BackAndroid.removeEventListener('hardwareBackPress', this._hardwareBackHandler)
}
}
render() {
const staggeredAnimatedMenus = this.state.stagArr.map((index) => {
return (
<TouchableWithoutFeedback key={index} onPress={this._handlePress.bind(this, index)} style={{backgroundColor: 'red'}}>
<Animated.View
style={{ transform: [{ translateX: this.state.animatedStagArr[index] }] }}>
<View style={styles.menuItemContainer}>
{this.state.menuItems[index].icon}
<Text style={[styles.menuItem, { ...this.props.menuTextStyles }]}>
{this.state.menuItems[index].title}
</Text>
</View>
</Animated.View>
</TouchableWithoutFeedback>
)
})
return (
<View style={[styles.offCanvasContainer, {
flex: 1,
backgroundColor: this.props.backgroundColor
}]}>
<ScrollView
showsVerticalScrollIndicator={false}
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0
}}>
<Animated.View style={styles.menuItemsContainer}>
{staggeredAnimatedMenus}
</Animated.View>
</ScrollView>
<Animated.View
onStartShouldSetResponder={() => true}
onResponderTerminationRequest={() => true}
onResponderRelease={(evt) => this._gestureControl(evt)}
style={[styles.activityContainer, {
flex: 1,
backgroundColor: this.props.backgroundColor,
transform: [
{ translateX: this.state.activityLeftPos }
]
}]}>
{this.state.menuItems[this.state.activeMenu].renderScene}
</Animated.View>
</View>
)
}
// press on any menu item, render the respective scene
_handlePress(index) {
this.setState({ activeMenu: index })
this.props.onMenuPress()
}
_hardwareBackHandler() {
this.props.onMenuPress()
return true
}
// control swipe left or right reveal for menu
_gestureControl(evt) {
const {locationX, pageX} = evt.nativeEvent
if (!this.props.active) {
if (locationX < 40 && pageX > 100) this.props.onMenuPress()
} else {
if (pageX) this.props.onMenuPress()
}
}
// animate stuffs with hard coded values for fine tuning
_animateStuffs() {
const activityLeftPos = this.props.active ? 250 : 0
const menuTranslateX = this.props.active? 0 : -150
Animated.parallel([
Animated.timing(this.state.activityLeftPos, { toValue: activityLeftPos, duration: this.state.animationDuration }),
Animated.stagger(50, this.state.stagArr.map((item) => {
if (this.props.active) {
return Animated.timing(
this.state.animatedStagArr[item],
{
toValue: menuTranslateX,
duration: this.state.animationDuration,
delay: 250
}
)
} else {
return Animated.timing(
this.state.animatedStagArr[item],
{
toValue: menuTranslateX,
duration: this.state.animationDuration,
delay: 400
}
)
}
}))
])
.start()
}
}
// validate props
OffCanvasReveal.propTypes = {
active: PropTypes.bool.isRequired,
onMenuPress: PropTypes.func.isRequired,
menuItems: PropTypes.array.isRequired,
backgroundColor: PropTypes.string,
menuTextStyles: PropTypes.object,
handleBackPress: PropTypes.bool
}
// set default props
OffCanvasReveal.defaultProps = {
backgroundColor: '#222222',
menuTextStyles: { color: 'white' },
handleBackPress: true
}
export default OffCanvasReveal
// structure stylesheet
const styles = StyleSheet.create({
offCanvasContainer: {
},
menuItemsContainer: {
paddingTop: 30
},
menuItemContainer: {
paddingLeft: 20,
flexDirection: 'row',
alignItems: 'center'
},
menuItem: {
fontWeight: 'bold',
paddingLeft: 12,
paddingTop: 15,
paddingBottom: 15
},
activityContainer: {
}
})