Skip to content

Commit 111e74a

Browse files
author
Luke Brandon Farrell
authored
Merge pull request #37 from aspect-apps/deniseng/ch1987/improve-api-of-rnn-drawer
Improve API of SideMenuView
2 parents a0e1ab7 + 77aae39 commit 111e74a

File tree

4 files changed

+106
-53
lines changed

4 files changed

+106
-53
lines changed

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,24 @@ import { SideMenuView } from "react-native-navigation-drawer-extension";
114114

115115
<SideMenuView
116116
style={{ flex: 1 }}
117-
right={() => RNNDrawer.showDrawer({
118-
component: {
119-
name: "CustomDrawer",
120-
passProps: {
121-
direction: "right"
122-
}
117+
drawerName={'CustomDrawer'}
118+
direction={'right'}
119+
passProps={{
120+
animationOpenTime: 300,
121+
animationCloseTime: 300,
122+
dismissWhenTouchOutside: true,
123+
fadeOpacity: 0.6,
124+
drawerScreenWidth: '75%',
125+
drawerScreenHeight: '100%',
126+
parentComponentId: props.componentId,
127+
style: {
128+
backgroundColor: 'white',
129+
},
130+
}}
131+
options={{
132+
layout: {
133+
componentBackgroundColor: 'transparent',
123134
}
124-
})}
125135
>
126136
{...}
127137
</SideMenuView>
@@ -132,8 +142,11 @@ import { SideMenuView } from "react-native-navigation-drawer-extension";
132142
133143
| Prop | Type | Optional | Default | Description |
134144
| ------------------- | ------------- | --------- | ------- | --------------------------------------------------------------------------------------- |
135-
| left | func | Yes | | Function which is executed when the left gutter is swiped. |
136-
| right | func | Yes | | Function which is executed when the right gutter is swiped. |
145+
| style | StyleProp<ViewStyle> | Yes | | The style of the drawer container. |
146+
| drawerName | string | No | | The name of the drawer component.
147+
| direction | string | Yes | left | The direction to open the drawer, one of: ["left", "right"].
148+
| passProps | object | Yes | | The values passed to the drawer. See props in RNNDrawer above.
149+
| options | Options | Yes | | The options to configure properties of the React Native Navigation native screen. Refer to React Native Navigation's options object.
137150
| swipeSensitivity | number | Yes | 0.2 | The sensitivity of the swipe to invoke each function. |
138151
| sideMargin | number | Yes | 15 | The size of the gutter for both sides. |
139152
| sideMarginLeft | number | Yes | | The size of the gutter for the left side. |

example/src/pages/AnotherPage.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
import React from 'react';
2-
import {StyleSheet, Text, View} from 'react-native';
3-
import {
4-
RNNDrawer,
5-
SideMenuView,
6-
} from 'react-native-navigation-drawer-extension';
2+
import { StyleSheet, Text } from 'react-native';
3+
import { SideMenuView } from 'react-native-navigation-drawer-extension';
74

85
const AnotherPage = (props) => {
96
console.log(props);
107
return (
118
<SideMenuView
129
style={styles.mainContainer}
13-
right={() =>
14-
RNNDrawer.showDrawer({
15-
component: {
16-
name: 'CustomDrawer',
17-
passProps: {
18-
direction: 'right',
19-
parentComponentId: props.componentId,
20-
},
21-
},
22-
})
23-
}>
10+
drawerName={'CustomDrawer'}
11+
direction={'right'}
12+
passProps={{
13+
parentComponentId: props.componentId,
14+
}}
15+
>
2416
<Text style={styles.bodyText}>
2517
In this page, you can try the SideMenuView component, by simply swiping
2618
from the right to the left.

example/src/pages/HomePage.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,60 @@ import {
66
TouchableOpacity,
77
View,
88
} from 'react-native';
9-
import { RNNDrawer, SideMenuView } from 'react-native-navigation-drawer-extension';
9+
import {
10+
RNNDrawer,
11+
SideMenuView,
12+
} from 'react-native-navigation-drawer-extension';
1013

1114
const HomePage = (props) => {
1215
const onPress = () => {
1316
RNNDrawer.showDrawer({
1417
component: {
1518
name: 'CustomDrawer',
1619
passProps: {
20+
animationOpenTime: 300,
21+
animationCloseTime: 300,
1722
direction: 'left',
1823
dismissWhenTouchOutside: true,
1924
fadeOpacity: 0.6,
2025
drawerScreenWidth: '75%' || 445,
2126
drawerScreenHeight: '100%' || 700,
2227
parentComponentId: props.componentId,
28+
style: {
29+
backgroundColor: 'white',
30+
},
31+
},
32+
options: {
33+
layout: {
34+
componentBackgroundColor: 'black',
35+
},
2336
},
2437
},
2538
});
2639
};
2740

2841
return (
2942
<SideMenuView
30-
style={{ flex: 1 }}
31-
left={() => RNNDrawer.showDrawer({
32-
component: {
33-
name: 'CustomDrawer',
34-
passProps: {
35-
direction: 'left',
36-
parentComponentId: props.componentId,
37-
},
43+
style={{ flex: 1 }} // Styles the view
44+
drawerName={'CustomDrawer'}
45+
direction={'left'}
46+
passProps={{
47+
animationOpenTime: 300,
48+
animationCloseTime: 300,
49+
dismissWhenTouchOutside: true,
50+
fadeOpacity: 0.6,
51+
drawerScreenWidth: '75%' || 445,
52+
drawerScreenHeight: '100%' || 700,
53+
parentComponentId: props.componentId,
54+
style: {
55+
backgroundColor: 'white', // Styles the drawer container
56+
},
57+
}}
58+
options={{
59+
layout: {
60+
componentBackgroundColor: 'transparent',
3861
},
39-
}
40-
)}
62+
}}
4163
>
4264
<SafeAreaView>
4365
<View style={styles.mainContainer}>

src/SideMenuView.tsx

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@ import {
1717
StyleProp,
1818
ViewStyle,
1919
} from 'react-native';
20+
import { Options } from 'react-native-navigation';
21+
2022
/* Utils - Project Utilities */
23+
import RNNDrawer from './RNNDrawer';
2124
import { listen, dispatch } from './events';
2225

2326
const screenHeight: number = Dimensions.get('screen').height;
2427

25-
type SwipeFunctionType = () => void;
26-
2728
interface IProps {
2829
swipeSensitivity?: number;
29-
left?: SwipeFunctionType;
30-
right?: SwipeFunctionType;
3130
sideMargin?: number;
3231
sideMarginLeft?: number;
3332
sideMarginRight?: number;
3433
style?: StyleProp<ViewStyle>;
34+
drawerName: string;
35+
direction: 'left' | 'right';
36+
passProps?: object;
37+
options?: Options;
3538
}
3639

3740
class SideMenuView extends React.Component<IProps, {}> {
@@ -44,6 +47,7 @@ class SideMenuView extends React.Component<IProps, {}> {
4447
static defaultProps = {
4548
sideMargin: 15,
4649
swipeSensitivity: 0.2,
50+
direction: 'left',
4751
};
4852

4953
/**
@@ -58,8 +62,14 @@ class SideMenuView extends React.Component<IProps, {}> {
5862

5963
this.isOpened = false;
6064

61-
const { swipeSensitivity, left, right } = props;
62-
65+
const {
66+
swipeSensitivity,
67+
drawerName,
68+
direction,
69+
passProps,
70+
options,
71+
} = props;
72+
const directionIsLeft = direction ? direction == 'left' : true;
6373
this._panResponderMethods = {
6474
// Ask to be the responder:
6575
onStartShouldSetPanResponder: (
@@ -117,9 +127,18 @@ class SideMenuView extends React.Component<IProps, {}> {
117127

118128
// Left Swipe
119129
if (typeof swipeSensitivity !== 'undefined') {
120-
if (vx > swipeSensitivity && !this.isOpened && left) {
130+
if (vx > swipeSensitivity && !this.isOpened && directionIsLeft) {
121131
this.isOpened = true;
122-
left();
132+
RNNDrawer.showDrawer({
133+
component: {
134+
name: drawerName,
135+
passProps: {
136+
direction: 'left',
137+
...passProps,
138+
},
139+
options: { ...options },
140+
},
141+
});
123142
}
124143
}
125144
},
@@ -139,9 +158,18 @@ class SideMenuView extends React.Component<IProps, {}> {
139158

140159
// Right Swipe
141160
if (typeof swipeSensitivity !== 'undefined') {
142-
if (vx > -swipeSensitivity && !this.isOpened && right) {
161+
if (vx > -swipeSensitivity && !this.isOpened && !directionIsLeft) {
143162
this.isOpened = true;
144-
right();
163+
RNNDrawer.showDrawer({
164+
component: {
165+
name: drawerName,
166+
passProps: {
167+
direction: 'right',
168+
...passProps,
169+
},
170+
options: { ...options },
171+
},
172+
});
145173
}
146174
}
147175
},
@@ -186,19 +214,19 @@ class SideMenuView extends React.Component<IProps, {}> {
186214
/** Props */
187215
const {
188216
children,
189-
left,
190-
right,
217+
direction,
191218
sideMargin,
192219
sideMarginLeft,
193220
sideMarginRight,
194221
...props
195222
} = this.props;
196223

224+
const directionIsLeft = direction ? direction == 'left' : true;
197225
return (
198226
<View {...props}>
199227
{children}
200228

201-
{left ? (
229+
{directionIsLeft ? (
202230
<View
203231
style={{
204232
left: 0,
@@ -209,9 +237,7 @@ class SideMenuView extends React.Component<IProps, {}> {
209237
}}
210238
{...this._leftPanResponder.panHandlers}
211239
/>
212-
) : null}
213-
214-
{right ? (
240+
) : (
215241
<View
216242
style={{
217243
position: 'absolute',
@@ -222,7 +248,7 @@ class SideMenuView extends React.Component<IProps, {}> {
222248
}}
223249
{...this._rightPanResponder.panHandlers}
224250
/>
225-
) : null}
251+
)}
226252
</View>
227253
);
228254
}

0 commit comments

Comments
 (0)