Skip to content

Commit c9f7c46

Browse files
jaulzkmagiera
authored andcommitted
fix: web compatibility (#406)
The motivation for me was to use `react-navigation@^3` and I stumbled across quite a few missing imports. This PR will introduce some shims so it shouldn't be a problem anymore.
1 parent 60fec9c commit c9f7c46

31 files changed

Lines changed: 4708 additions & 617 deletions

Directions.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { NativeModules } from 'react-native';
2+
3+
const { RNGestureHandlerModule } = NativeModules;
4+
5+
export default RNGestureHandlerModule.Direction;

Directions.web.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
RIGHT: 1,
3+
LEFT: 2,
4+
UP: 4,
5+
DOWN: 8,
6+
};

DrawerLayout.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
StatusBar,
1919
I18nManager,
2020
} from 'react-native';
21-
import { AnimatedEvent } from 'react-native/Libraries/Animated/src/AnimatedEvent';
2221

2322
import { PanGestureHandler, TapGestureHandler, State } from './GestureHandler';
2423

@@ -88,7 +87,7 @@ export default class DrawerLayout extends Component<PropType, StateType> {
8887
Right: 'right',
8988
};
9089
_openValue: ?Animated.Interpolation;
91-
_onGestureEvent: ?AnimatedEvent;
90+
_onGestureEvent: ?Animated.Event;
9291
_accessibilityIsModalView = React.createRef();
9392
_pointerEventsView = React.createRef();
9493
_panGestureHandler = React.createRef();
@@ -353,10 +352,16 @@ export default class DrawerLayout extends Component<PropType, StateType> {
353352
this.props.drawerWidth,
354353
options.velocity ? options.velocity : 0
355354
);
355+
356+
// We need to force the update, otherwise the overlay is not rerendered and it would not be clickable
357+
this.forceUpdate();
356358
};
357359

358360
closeDrawer = (options: DrawerMovementOptionType = {}) => {
359361
this._animateDrawer(undefined, 0, options.velocity ? options.velocity : 0);
362+
363+
// We need to force the update, otherwise the overlay is not rerendered and it would be still clickable
364+
this.forceUpdate();
360365
};
361366

362367
_renderOverlay = () => {
@@ -371,6 +376,7 @@ export default class DrawerLayout extends Component<PropType, StateType> {
371376
opacity: overlayOpacity,
372377
backgroundColor: this.props.overlayColor,
373378
};
379+
374380
return (
375381
<TapGestureHandler onHandlerStateChange={this._onTapHandlerStateChange}>
376382
<Animated.View
@@ -455,7 +461,7 @@ export default class DrawerLayout extends Component<PropType, StateType> {
455461
ref={this._accessibilityIsModalView}
456462
accessibilityViewIsModal={this._drawerShown}
457463
style={[styles.drawerContainer, drawerStyles, drawerContainerStyle]}>
458-
<View style={[styles.drawer, dynamicDrawerStyles]}>
464+
<View style={dynamicDrawerStyles}>
459465
{this.props.renderNavigationView(this._openValue)}
460466
</View>
461467
</Animated.View>
@@ -509,7 +515,6 @@ export default class DrawerLayout extends Component<PropType, StateType> {
509515
}
510516

511517
const styles = StyleSheet.create({
512-
drawer: { flex: 0 },
513518
drawerContainer: {
514519
...StyleSheet.absoluteFillObject,
515520
zIndex: 1001,

Example/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { Text, View, FlatList, StyleSheet, YellowBox } from 'react-native';
3-
import { createStackNavigator } from 'react-navigation';
3+
import { createAppContainer, createStackNavigator } from 'react-navigation';
44
import { RectButton, ScrollView } from 'react-native-gesture-handler';
55

66
import SwipeableTable from './swipeable';
@@ -156,4 +156,4 @@ const styles = StyleSheet.create({
156156
},
157157
});
158158

159-
export default ExampleApp;
159+
export default createAppContainer(ExampleApp);

Example/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
ext {
55
buildToolsVersion = "27.0.3"
66
minSdkVersion = 16
7-
compileSdkVersion = 27
7+
compileSdkVersion = 28
88
targetSdkVersion = 26
99
supportLibVersion = "27.1.1"
1010
}

Example/bottomSheet/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
PanGestureHandler,
55
NativeViewGestureHandler,
66
State,
7-
TapGestureHandler
7+
TapGestureHandler,
88
} from 'react-native-gesture-handler';
99

1010
import { LoremIpsum } from '../common';

Example/combo/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { Component } from 'react';
22
import {
3-
Alert,
43
StyleSheet,
54
ScrollView as RNScroll,
65
Switch,
@@ -107,7 +106,7 @@ class ControlledSwitch extends React.Component {
107106

108107
class Combo extends Component {
109108
_onClick = () => {
110-
Alert.alert("I'm so touched");
109+
alert("I'm so touched");
111110
this._scrollView.scrollTo({ y: 200, animated: true });
112111
};
113112
render() {
@@ -140,7 +139,7 @@ class Combo extends Component {
140139
<Swipeable>
141140
<RectButton
142141
style={styles.rectButton}
143-
onPress={() => Alert.alert('First row clicked')}>
142+
onPress={() => alert('First row clicked')}>
144143
<Text style={styles.buttonText}>
145144
Swipe this row & observe highlight delay
146145
</Text>
@@ -155,7 +154,7 @@ class Combo extends Component {
155154
<View style={styles.buttonDelimiter} />
156155
<RectButton
157156
style={styles.rectButton}
158-
onPress={() => Alert.alert('Second row clicked')}>
157+
onPress={() => alert('Second row clicked')}>
159158
<Text style={styles.buttonText}>
160159
Second info icon will block scrolling
161160
</Text>
@@ -167,7 +166,7 @@ class Combo extends Component {
167166
<View style={styles.buttonDelimiter} />
168167
<RectButton
169168
style={styles.rectButton}
170-
onPress={() => Alert.alert('Third row clicked')}>
169+
onPress={() => alert('Third row clicked')}>
171170
<Text style={styles.buttonText}>
172171
This one will cancel when you drag outside
173172
</Text>

Example/common.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@ export class LoremIpsum extends React.Component {
1313
style: styles.lipsum,
1414
};
1515
loremIpsum() {
16-
return LOREM_IPSUM.split(' ').slice(0, this.props.words).join(' ');
16+
return LOREM_IPSUM.split(' ')
17+
.slice(0, this.props.words)
18+
.join(' ');
1719
}
1820
render() {
19-
return (
20-
<Text style={this.props.style}>
21-
{this.loremIpsum()}
22-
</Text>
23-
);
21+
return <Text style={this.props.style}>{this.loremIpsum()}</Text>;
2422
}
2523
}
2624

Example/index.web.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { AppRegistry } from 'react-native';
2+
3+
import ExampleApp from './App';
4+
5+
AppRegistry.registerComponent('App', () => ExampleApp);
6+
AppRegistry.runApplication('App', { rootTag: document.getElementById('root') });

Example/metro.config.js

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
11
const blacklist = require('metro-config/src/defaults/blacklist');
22
const path = require('path');
3+
const pkg = require('./package.json');
34

45
const glob = require('glob-to-regexp');
56

67
function getBlacklist() {
78
const nodeModuleDirs = [
89
glob(`${path.resolve(__dirname, '..')}/node_modules/*`),
9-
glob(`${path.resolve(__dirname)}/node_modules/metro/node_modules/fbjs/*`),
10+
glob(`${path.resolve(__dirname)}/node_modules/*/node_modules/fbjs/*`),
1011
glob(
1112
`${path.resolve(
1213
__dirname
13-
)}/node_modules/create-react-context/node_modules/fbjs/*`
14-
),
15-
glob(
16-
`${path.resolve(
17-
__dirname
18-
)}/node_modules/create-react-class/node_modules/fbjs/*`
19-
),
20-
glob(
21-
`${path.resolve(
22-
__dirname
23-
)}/node_modules/react-navigation/node_modules/hoist-non-react-statics/*`
24-
),
25-
glob(
26-
`${path.resolve(
27-
__dirname
28-
)}/node_modules/react-navigation-tabs/node_modules/hoist-non-react-statics/*`
29-
),
30-
glob(
31-
`${path.resolve(
32-
__dirname
33-
)}/node_modules/react-native-safe-area-view/node_modules/hoist-non-react-statics/*`
14+
)}/node_modules/*/node_modules/hoist-non-react-statics/*`
3415
),
3516
];
3617
return blacklist(nodeModuleDirs);
@@ -39,14 +20,7 @@ function getBlacklist() {
3920
module.exports = {
4021
resolver: {
4122
blacklistRE: getBlacklist(),
42-
providesModuleNodeModules: [
43-
'react-native',
44-
'react',
45-
'invariant',
46-
'fbjs',
47-
'prop-types',
48-
'hoist-non-react-statics',
49-
],
23+
providesModuleNodeModules: Object.keys(pkg.dependencies),
5024
},
5125
watchFolders: [path.resolve(__dirname, '..')],
5226
};

0 commit comments

Comments
 (0)