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