Experiment with passing the pan gesture ref down through context

This commit is contained in:
Brent Vatne 2018-09-24 17:53:22 -07:00
parent 413bfe3ea8
commit 5eac729fc3
6 changed files with 77 additions and 5 deletions

View File

@ -8,6 +8,7 @@ import { ListSection, Divider } from 'react-native-paper';
import SimpleStack from './src/SimpleStack'; import SimpleStack from './src/SimpleStack';
import TransparentStack from './src/TransparentStack'; import TransparentStack from './src/TransparentStack';
import ModalStack from './src/ModalStack'; import ModalStack from './src/ModalStack';
import GestureInteraction from './src/GestureInteraction';
// Comment the following two lines to stop using react-native-screens // Comment the following two lines to stop using react-native-screens
import { useScreens } from 'react-native-screens'; import { useScreens } from 'react-native-screens';
@ -21,6 +22,7 @@ const data = [
title: 'Transparent', title: 'Transparent',
routeName: 'TransparentStack', routeName: 'TransparentStack',
}, },
{ component: GestureInteraction, title: 'Gesture Interaction', routeName: 'GestureInteraction' },
]; ];
class Home extends React.Component { class Home extends React.Component {

View File

@ -0,0 +1,58 @@
import React from 'react';
import { Button, WebView, View, StyleSheet } from 'react-native';
import { MapView } from 'expo';
import { createStackNavigator } from 'react-navigation';
import { GestureContext } from 'react-navigation-stack';
import {
PanGestureHandler,
NativeViewGestureHandler,
} from 'react-native-gesture-handler';
const IndexScreen = ({ navigation }) => (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Button title="Go to MapView" onPress={() => navigation.navigate('Map')} />
<Button title="Go to WebView" onPress={() => navigation.navigate('Web')} />
</View>
);
class MapScreen extends React.Component {
render() {
return (
<GestureContext.Consumer>
{ref => (
<NativeViewGestureHandler waitFor={ref}>
<MapView style={{ flex: 1 }} />
</NativeViewGestureHandler>
)}
</GestureContext.Consumer>
);
}
}
MapScreen.navigationOptions = {
title: 'MapView',
};
const WebViewScreen = () => (
<NativeViewGestureHandler>
<WebView style={{ flex: 1 }} source={{ uri: 'https://news.google.com' }} />
</NativeViewGestureHandler>
);
WebViewScreen.navigationOptions = {
title: 'WebView',
};
const DrawerExample = createStackNavigator(
{
Index: IndexScreen,
Map: MapScreen,
Web: WebViewScreen,
},
{
contentOptions: {
activeTintColor: '#e91e63',
},
}
);
export default DrawerExample;

View File

@ -1,6 +1,6 @@
{ {
"name": "react-navigation-stack", "name": "react-navigation-stack",
"version": "0.6.0", "version": "1.0.0-alpha.0",
"description": "Stack navigator component for React Navigation", "description": "Stack navigator component for React Navigation",
"main": "dist/index.js", "main": "dist/index.js",
"files": [ "files": [

View File

@ -52,4 +52,7 @@ module.exports = {
get ScenesReducer() { get ScenesReducer() {
return require('./views/ScenesReducer').default; return require('./views/ScenesReducer').default;
}, },
get GestureContext() {
return require('./utils/GestureContext').default;
}
}; };

View File

@ -0,0 +1,4 @@
import React from 'react';
const GestureContext = React.createContext(null);
export default GestureContext;

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import clamp from '../../utils/clamp'; import clamp from '../../utils/clamp';
import GestureContext from '../../utils/GestureContext';
import { import {
Animated, Animated,
StyleSheet, StyleSheet,
@ -120,6 +121,7 @@ class StackViewLayout extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.panGestureRef = React.createRef();
this.state = { this.state = {
// Used when card's header is null and mode is float to make transition // Used when card's header is null and mode is float to make transition
@ -295,15 +297,18 @@ class StackViewLayout extends React.Component {
return ( return (
<PanGestureHandler <PanGestureHandler
{...this._gestureActivationCriteria()} {...this._gestureActivationCriteria()}
ref={this.panGestureRef}
onGestureEvent={this._handlePanGestureEvent} onGestureEvent={this._handlePanGestureEvent}
onHandlerStateChange={this._handlePanGestureStateChange} onHandlerStateChange={this._handlePanGestureStateChange}
enabled={index > 0 && gesturesEnabled} enabled={index > 0 && gesturesEnabled}
> >
<View style={containerStyle}> <View style={containerStyle}>
<GestureContext.Provider value={this.panGestureRef}>
<ScreenContainer style={styles.scenes}> <ScreenContainer style={styles.scenes}>
{scenes.map(s => this._renderCard(s))} {scenes.map(s => this._renderCard(s))}
</ScreenContainer> </ScreenContainer>
{floatingHeader} {floatingHeader}
</GestureContext.Provider>
</View> </View>
</PanGestureHandler> </PanGestureHandler>
); );