Skip to content

Commit f9ae30b

Browse files
committed
Merge pull request software-mansion#406 from jaulz/fix/web-compatibility
fix: web compatibility
2 parents 78b6e85 + 97236fd commit f9ae30b

9 files changed

+174
-7
lines changed

AnimatedEvent.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import AnimatedEvent from 'react-native/Libraries/Animated/src/AnimatedEvent';
2+
3+
export default AnimatedEvent;

AnimatedEvent.web.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import AnimatedEvent from 'react-native-web/dist/vendor/react-native/Animated/AnimatedEvent';
2+
3+
export default AnimatedEvent;

Directions.js

+6
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
StatusBar,
1919
I18nManager,
2020
} from 'react-native';
21-
import { AnimatedEvent } from 'react-native/Libraries/Animated/src/AnimatedEvent';
21+
import { AnimatedEvent } from './AnimatedEvent';
2222

2323
import { PanGestureHandler, TapGestureHandler, State } from './GestureHandler';
2424

@@ -509,7 +509,7 @@ export default class DrawerLayout extends Component<PropType, StateType> {
509509
}
510510

511511
const styles = StyleSheet.create({
512-
drawer: { flex: 0 },
512+
drawer: {},
513513
drawerContainer: {
514514
...StyleSheet.absoluteFillObject,
515515
zIndex: 1001,

GestureHandler.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import deepEqual from 'fbjs/lib/areEqual';
2222
import PropTypes from 'prop-types';
2323

2424
import gestureHandlerRootHOC from './gestureHandlerRootHOC';
25+
import Directions from './Directions';
26+
import State from './State';
2527

2628
const RNGestureHandlerModule = NativeModules.RNGestureHandlerModule;
2729

@@ -51,10 +53,6 @@ UIManager.RCTView.directEventTypes = {
5153
},
5254
};
5355

54-
const State = RNGestureHandlerModule.State;
55-
56-
const Directions = RNGestureHandlerModule.Direction;
57-
5856
let handlerTag = 1;
5957
const handlerIDToTag = {};
6058

GestureHandler.web.js

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import React from 'react';
2+
import {
3+
ScrollView,
4+
Slider,
5+
Switch,
6+
TextInput,
7+
ToolbarAndroid,
8+
ViewPagerAndroid,
9+
DrawerLayoutAndroid,
10+
WebView,
11+
FlatList,
12+
TouchableWithoutFeedback,
13+
findNodeHandle,
14+
} from 'react-native';
15+
import gestureHandlerRootHOC from './gestureHandlerRootHOC';
16+
import Directions from './Directions';
17+
import State from './State';
18+
19+
// Factory for Handler components
20+
function createHandler(name, attachNativeHandler) {
21+
return class Handler extends React.Component {
22+
static displayName = name;
23+
24+
container = React.createRef();
25+
detachNativeHandler = null;
26+
27+
componentDidMount() {
28+
// Get current DOM node
29+
const node = findNodeHandle(this.container.current);
30+
31+
// Attach handler to DOM node
32+
const { enabled } = this.props;
33+
if (node && attachNativeHandler && enabled !== false) {
34+
this.detachNativeHandler = attachNativeHandler(node, this.props);
35+
}
36+
}
37+
38+
componentDidUpdate() {
39+
// Get current DOM node
40+
const node = findNodeHandle(this.container.current);
41+
42+
if (node && attachNativeHandler) {
43+
// Detach existing handlers
44+
if (this.detachNativeHandler) {
45+
this.detachNativeHandler(node);
46+
}
47+
48+
// Attach handler to DOM node again
49+
const { enabled } = this.props;
50+
if (enabled !== false) {
51+
attachNativeHandler(node, this.props);
52+
}
53+
}
54+
}
55+
56+
setNativeProps() {
57+
// No implementation so far but is needed to avoid null calls
58+
}
59+
60+
render() {
61+
const { children, ...rest } = this.props;
62+
63+
// We don't want to create another layer, so instead we clone it only but keep the reference
64+
const child = React.Children.only(children);
65+
return React.cloneElement(child, {
66+
ref: this.container,
67+
...rest,
68+
});
69+
}
70+
};
71+
}
72+
73+
// Create all Handler components with their respective handler functions
74+
// (at the moment only TapGestureHandler is properly supported)
75+
const NativeViewGestureHandler = createHandler('NativeViewGestureHandler');
76+
const TapGestureHandler = createHandler('TapGestureHandler', (node, props) => {
77+
const { onHandlerStateChange = () => {} } = props;
78+
const clickHandler = ({ x }) =>
79+
onHandlerStateChange({
80+
nativeEvent: {
81+
oldState: State.ACTIVE,
82+
state: State.UNDETERMINED,
83+
x,
84+
},
85+
});
86+
node.addEventListener('click', clickHandler);
87+
88+
// Detach event listeners
89+
return () => {
90+
node.removeEventListener('click', clickHandler);
91+
};
92+
});
93+
const FlingGestureHandler = createHandler('FlingGestureHandler');
94+
const ForceTouchGestureHandler = createHandler('ForceTouchGestureHandler');
95+
const LongPressGestureHandler = createHandler('LongPressGestureHandler');
96+
const PanGestureHandler = createHandler('PanGestureHandler');
97+
const PinchGestureHandler = createHandler('PinchGestureHandler');
98+
const RotationGestureHandler = createHandler('RotationGestureHandler');
99+
100+
// Factory for Button component
101+
// (at the moment this is a plain TouchableWithoutFeedback)
102+
function createButton(name) {
103+
return class Button extends React.Component {
104+
static displayName = name;
105+
106+
render() {
107+
return <TouchableWithoutFeedback {...this.props} />;
108+
}
109+
};
110+
}
111+
112+
const RawButton = createButton('RawButton');
113+
const BaseButton = createButton('BaseButton');
114+
const RectButton = createButton('RectButton');
115+
const BorderlessButton = createButton('BorderlessButton');
116+
117+
// Export same components as in GestureHandler.js
118+
export {
119+
ScrollView,
120+
Slider,
121+
Switch,
122+
TextInput,
123+
ToolbarAndroid,
124+
ViewPagerAndroid,
125+
DrawerLayoutAndroid,
126+
WebView,
127+
NativeViewGestureHandler,
128+
TapGestureHandler,
129+
FlingGestureHandler,
130+
ForceTouchGestureHandler,
131+
LongPressGestureHandler,
132+
PanGestureHandler,
133+
PinchGestureHandler,
134+
RotationGestureHandler,
135+
State,
136+
/* Buttons */
137+
RawButton,
138+
BaseButton,
139+
RectButton,
140+
BorderlessButton,
141+
/* Other */
142+
FlatList,
143+
gestureHandlerRootHOC,
144+
Directions,
145+
};

State.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default {
2+
UNDETERMINED: 0,
3+
FAILED: 1,
4+
BEGAN: 2,
5+
CANCELLED: 3,
6+
ACTIVE: 4,
7+
END: 5,
8+
};

Swipeable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import React, { Component } from 'react';
88
import { Animated, StyleSheet, View } from 'react-native';
9-
import { AnimatedEvent } from 'react-native/Libraries/Animated/src/AnimatedEvent';
9+
import { AnimatedEvent } from './AnimatedEvent';
1010

1111
import { PanGestureHandler, TapGestureHandler, State } from './GestureHandler';
1212

Swipeable.web.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// At the moment no swipe is supported
2+
export default function Swipeable({ children }) {
3+
return children;
4+
}

0 commit comments

Comments
 (0)