-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
197 lines (177 loc) · 4.95 KB
/
index.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
import React, { useEffect, useState, useRef } from "react"
import {
Animated,
useWindowDimensions,
StyleSheet,
View,
Text,
Platform,
TouchableOpacity,
Dimensions,
} from "react-native"
import PropTypes from "prop-types"
const MenuDrawer = props => {
const fadeAnim = useRef(new Animated.Value(0)).current
const window = useWindowDimensions()
const screenHeight = props.fullSize ? Dimensions.get('screen').height : window.height;
const screenWidth = props.fullSize? Dimensions.get('screen').width: window.width;
const initialDrawerWidth = screenWidth * (props.drawerPercentage / 100)
const drawerWidthRef = useRef(initialDrawerWidth)
const leftOffsetRef = useRef(new Animated.Value(0))
useEffect(() => {
const newDrawerWidth = screenWidth * (props.drawerPercentage / 100)
drawerWidthRef.current = newDrawerWidth
if (props.open) {
leftOffsetRef.current = new Animated.Value(determineNewLeftOffset(drawerWidthRef.current))
} else {
leftOffsetRef.current = new Animated.Value(0)
}
}, [screenWidth])
const determineNewLeftOffset = drawerWidth => {
return props.position === "left" ? drawerWidth : -drawerWidth
}
useEffect(() => {
props.open ? openDrawer() : closeDrawer()
}, [props.open])
const openDrawer = () => {
const { drawerPercentage, animationTime, opacity } = props
const drawerWidth = drawerWidthRef.current
const leftOffset = leftOffsetRef.current
Animated.parallel([
Animated.timing(leftOffset, {
toValue: determineNewLeftOffset(drawerWidth),
duration: animationTime,
useNativeDriver: true
}),
Animated.timing(fadeAnim, {
toValue: opacity,
duration: animationTime,
useNativeDriver: true
})
]).start()
}
const closeDrawer = () => {
const { drawerPercentage, animationTime } = props
const drawerWidth = drawerWidthRef.current
const leftOffset = leftOffsetRef.current
Animated.parallel([
Animated.timing(leftOffset, {
toValue: 0,
duration: animationTime,
useNativeDriver: true
}),
Animated.timing(fadeAnim, {
toValue: 1,
duration: animationTime,
useNativeDriver: true
})
]).start()
}
const drawerFallback = () => {
return (
<TouchableOpacity onPress={closeDrawer}>
<Text>Close</Text>
</TouchableOpacity>
)
}
const renderPush = () => {
const { children, drawerContent, position, drawerPercentage } = props
const drawerWidth = drawerWidthRef.current
const leftOffset = leftOffsetRef.current
const animated = { transform: [{ translateX: leftOffset }] }
return (
<Animated.View
style={[
animated,
styles.pushMain,
{
flexDirection: position === "left" ? "row" : "row-reverse",
left: position === "left" ? -drawerWidth : 0,
height: screenHeight,
width: drawerWidth + screenWidth
}
]}
>
<View
style={[
{
height: screenHeight,
width: drawerWidth
}
]}
>
{drawerContent ? drawerContent : drawerFallback()}
</View>
<Animated.View
style={[
{
height: screenHeight,
width: screenWidth,
opacity: fadeAnim
}
]}
>
{children}
</Animated.View>
</Animated.View>
)
}
const renderOverlay = () => {
const { children, position, drawerContent, drawerPercentage } = props
const drawerWidth = drawerWidthRef.current
const leftOffset = leftOffsetRef.current
const animated = { transform: [{ translateX: leftOffset }] }
return (
<>
<Animated.View
style={[
animated,
styles.drawerOverlay,
{ height: screenHeight, width: drawerWidth, [position]: -drawerWidth }
]}
>
{drawerContent ? drawerContent : drawerFallback()}
</Animated.View>
<Animated.View style={[styles.containerOverlay, { opacity: fadeAnim }]}>
{children}
</Animated.View>
</>
)
}
return props.overlay ? renderOverlay() : renderPush()
}
MenuDrawer.defaultProps = {
open: false,
drawerPercentage: 45,
animationTime: 200,
overlay: true,
opacity: 0.4,
position: "left",
fullSize: false
}
MenuDrawer.propTypes = {
open: PropTypes.bool,
drawerPercentage: PropTypes.number,
animationTime: PropTypes.number,
overlay: PropTypes.bool,
opacity: PropTypes.number,
position: PropTypes.oneOf(["left", "right"]),
fullSize: PropTypes.bool,
}
const styles = StyleSheet.create({
mainPush: {
position: "absolute",
flex: 1
},
containerOverlay: {
flex: 1,
elevation: 0, // better for android
zIndex: 0
},
drawerOverlay: {
position: "absolute",
elevation: 1, // better for android
zIndex: 1
}
})
export default MenuDrawer