Skip to content

Commit 6d26918

Browse files
committed
add support for edge swipe gestures
Closes #38
1 parent 80684d7 commit 6d26918

File tree

7 files changed

+95
-20
lines changed

7 files changed

+95
-20
lines changed

modules/ReducerUtils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ export function createPartialState(
302302
let type = currentRoute.routeType;
303303
let routeParams = getRouteParams(currentRoute, params);
304304
const transition = currentRoute.transition;
305+
const onSwipeBack = currentRoute.onSwipeBack;
306+
const onSwipeForward = currentRoute.onSwipeForward;
305307
const reducer = currentRoute.reducer;
306308

307309
const indexRoute = getIndexRoute(currentRoute);
@@ -352,6 +354,8 @@ export function createPartialState(
352354
location,
353355
transition,
354356
reducer,
357+
onSwipeBack,
358+
onSwipeForward,
355359
};
356360

357361
if (prevState) {

modules/Route.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type Props = {
1919
reducer: Function,
2020
routeType: string,
2121
transition: ?string,
22+
onSwipeBack: Function,
23+
onSwipeForward: Function,
2224
};
2325

2426
const { ROUTE } = RouteTypes;
@@ -38,11 +40,17 @@ class Route extends Component<any, Props, any> {
3840
reducer: PropTypes.func.isRequired,
3941
routeType: PropTypes.string.isRequired,
4042
transition: PropTypes.string,
43+
onSwipeBack: PropTypes.func,
44+
onSwipeForward: PropTypes.func,
4145
};
4246

4347
static defaultProps = {
4448
routeType: ROUTE,
4549
reducer: defaultRouteReducer,
50+
onSwipeBack: (router) => {
51+
router.pop();
52+
},
53+
onSwipeForward: () => {},
4654
};
4755

4856
props: Props;

modules/RouteView.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class RouteView extends Component<any, Props, any> {
4040
(this: any).renderScene = this.renderScene.bind(this);
4141
}
4242

43-
// $FlowFixMe NavigationSceneRendererProps
44-
renderScene(props): ?ReactElement {
43+
renderScene(props: Object): ?ReactElement<any> {
4544
const { scene } = props;
4645

4746
const { navigationSubtree } = this.props;

modules/StackRouteView.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { warnOutOfSync } from './warningUtil';
66
import transitionRegistry from './transitionRegistry';
77
import { globalStyles as styles } from './styles';
88

9+
import withRouter from './withRouter';
10+
911
import type {
1012
EnhancedNavigationRoute,
1113
PseudoElement,
@@ -25,6 +27,7 @@ type Props = {
2527
navigationSubtree: ?Array<PseudoElement>,
2628
navigationState: EnhancedNavigationRoute,
2729
createElement: Function,
30+
router: Object,
2831
};
2932

3033
class StackRouteView extends Component<any, Props, any> {
@@ -37,6 +40,7 @@ class StackRouteView extends Component<any, Props, any> {
3740
navigationSubtree: PropTypes.arrayOf(PropTypes.object),
3841
navigationState: PropTypes.object,
3942
createElement: PropTypes.func.isRequired,
43+
router: PropTypes.object.isRequired,
4044
};
4145

4246
componentWillMount(): void {
@@ -46,8 +50,7 @@ class StackRouteView extends Component<any, Props, any> {
4650
(this: any).renderCardScene = this.renderCardScene.bind(this);
4751
}
4852

49-
// $FlowFixMe NavigationSceneRendererProps
50-
renderOverlay(props): ?ReactElement<any> {
53+
renderOverlay(props: Object): ?ReactElement<any> {
5154
const { scene } = props;
5255

5356
const { navigationSubtree, createElement } = this.props;
@@ -80,16 +83,19 @@ class StackRouteView extends Component<any, Props, any> {
8083
return null;
8184
}
8285

83-
// $FlowFixMe NavigationSceneRendererProps
84-
renderScene(props): ?ReactElement<any> {
86+
renderScene(props: Object): ?ReactElement<any> {
8587
const { scene } = props;
8688

8789
if (!scene.route) {
8890
return null;
8991
}
9092

9193
const { transition: parentTransition } = props.navigationState;
92-
const { transition: sceneTransition } = scene.route;
94+
const {
95+
transition: sceneTransition,
96+
onSwipeBack,
97+
onSwipeForward,
98+
} = scene.route;
9399

94100
const transition = sceneTransition || parentTransition;
95101

@@ -98,8 +104,16 @@ class StackRouteView extends Component<any, Props, any> {
98104
panResponder,
99105
} = transitionRegistry[transition];
100106

107+
const {
108+
router,
109+
} = this.props;
110+
101111
const viewStyle = styleInterpolator(props);
102-
const panHandlers = panResponder(props);
112+
const panHandlers = panResponder({
113+
...props,
114+
onNavigateBack: () => onSwipeBack && onSwipeBack(router),
115+
onNavigateForward: () => onSwipeForward && onSwipeForward(router),
116+
});
103117

104118
const navigationCardProps = {
105119
key: scene.route.key,
@@ -112,8 +126,7 @@ class StackRouteView extends Component<any, Props, any> {
112126
return React.createElement(NavigationCard, navigationCardProps);
113127
}
114128

115-
// $FlowFixMe NavigationSceneRendererProps
116-
renderCardScene(props): ?ReactElement {
129+
renderCardScene(props: Object): ?ReactElement<any> {
117130
const { scene } = props;
118131

119132
const { navigationSubtree } = this.props;
@@ -213,4 +226,4 @@ class StackRouteView extends Component<any, Props, any> {
213226
}
214227
}
215228

216-
export default StackRouteView;
229+
export default withRouter(StackRouteView);

modules/TabsRouteView.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { NavigationExperimental, View } from 'react-native';
55
import { warnOutOfSync } from './warningUtil';
66
import transitionRegistry from './transitionRegistry';
77
import { globalStyles as styles } from './styles';
8+
import withRouter from './withRouter';
89

910
import type {
1011
EnhancedNavigationRoute,
@@ -25,6 +26,7 @@ type Props = {
2526
navigationSubtree: ?Array<PseudoElement>,
2627
navigationState: EnhancedNavigationRoute,
2728
createElement: Function,
29+
router: Object,
2830
};
2931

3032
class TabsRouteView extends Component<any, Props, any> {
@@ -37,6 +39,7 @@ class TabsRouteView extends Component<any, Props, any> {
3739
navigationSubtree: PropTypes.arrayOf(PropTypes.object),
3840
navigationState: PropTypes.object,
3941
createElement: PropTypes.func.isRequired,
42+
router: PropTypes.object.isRequired,
4043
};
4144

4245
componentWillMount(): void {
@@ -46,8 +49,7 @@ class TabsRouteView extends Component<any, Props, any> {
4649
(this: any).renderCardScene = this.renderCardScene.bind(this);
4750
}
4851

49-
// $FlowFixMe NavigationSceneRendererProps
50-
renderOverlay(props): ?ReactElement {
52+
renderOverlay(props: Object): ?ReactElement<any> {
5153
const { scene } = props;
5254

5355
const { navigationSubtree, createElement } = this.props;
@@ -80,16 +82,19 @@ class TabsRouteView extends Component<any, Props, any> {
8082
return null;
8183
}
8284

83-
// $FlowFixMe NavigationSceneRendererProps
84-
renderScene(props): ?ReactElement {
85+
renderScene(props: Object): ?ReactElement<any> {
8586
const { scene } = props;
8687

8788
if (!scene.route) {
8889
return null;
8990
}
9091

9192
const { transition: parentTransition } = props.navigationState;
92-
const { transition: sceneTransition } = scene.route;
93+
const {
94+
transition: sceneTransition,
95+
onSwipeBack,
96+
onSwipeForward,
97+
} = scene.route;
9398

9499
const transition = sceneTransition || parentTransition;
95100

@@ -98,8 +103,16 @@ class TabsRouteView extends Component<any, Props, any> {
98103
panResponder,
99104
} = transitionRegistry[transition];
100105

106+
const {
107+
router,
108+
} = this.props;
109+
101110
const viewStyle = styleInterpolator(props);
102-
const panHandlers = panResponder(props);
111+
const panHandlers = panResponder({
112+
...props,
113+
onNavigateBack: () => onSwipeBack && onSwipeBack(router),
114+
onNavigateForward: () => onSwipeForward && onSwipeForward(router),
115+
});
103116

104117
const navigationCardProps = {
105118
key: scene.route.key,
@@ -112,8 +125,7 @@ class TabsRouteView extends Component<any, Props, any> {
112125
return React.createElement(NavigationCard, navigationCardProps);
113126
}
114127

115-
// $FlowFixMe NavigationSceneRendererProps
116-
renderCardScene(props): ?ReactElement {
128+
renderCardScene(props: Object): ?ReactElement<any> {
117129
const { scene } = props;
118130

119131
const { navigationSubtree } = this.props;
@@ -213,4 +225,4 @@ class TabsRouteView extends Component<any, Props, any> {
213225
}
214226
}
215227

216-
export default TabsRouteView;
228+
export default withRouter(TabsRouteView);

modules/TypeDefinition.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ export type EnhancedNavigationRoute = {
3131
routeParams: Object,
3232
transition: ?string,
3333
reducer: Function,
34+
onSwipeBack: ?Function,
35+
onSwipeForward: ?Function,
3436
};
3537

3638
export type RouteDef = {
@@ -41,6 +43,8 @@ export type RouteDef = {
4143
routeType: ?RouteType,
4244
transition: ?string,
4345
reducer: Function,
46+
onSwipeBack: ?Function,
47+
onSwipeForward: ?Function,
4448
};
4549

4650
export type IndexRouteDef = {

modules/withRouter.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* @flow */
2+
3+
import React, { PropTypes, Component } from 'react';
4+
5+
type Props = {};
6+
7+
type Context = {
8+
router: Object,
9+
};
10+
11+
export default (WrappedComponent: ReactClass<any>) => (
12+
class extends Component<any, Props, any> {
13+
14+
static contextTypes = {
15+
router: PropTypes.object,
16+
};
17+
18+
constructor(props: Props, context: Context) {
19+
super(props);
20+
this.router = context.router;
21+
}
22+
23+
router: Object;
24+
context: Context;
25+
props: Props;
26+
27+
render() {
28+
const props = this.props;
29+
30+
return (
31+
<WrappedComponent router={this.router} {...props} />
32+
);
33+
}
34+
}
35+
);

0 commit comments

Comments
 (0)