forked from software-mansion/react-native-gesture-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GestureButtons.js
177 lines (146 loc) · 4.47 KB
/
GestureButtons.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import PropTypes from 'prop-types';
import React from 'react';
import { Animated, Platform, processColor, StyleSheet } from 'react-native';
import createNativeWrapper from './createNativeWrapper';
import GestureHandlerButton from './GestureHandlerButton';
import State from './State';
export const RawButton = createNativeWrapper(GestureHandlerButton, {
shouldCancelWhenOutside: false,
shouldActivateOnStart: false,
});
/* Buttons */
export class BaseButton extends React.Component {
static propTypes = {
...RawButton.propTypes,
onPress: PropTypes.func,
onActiveStateChange: PropTypes.func,
};
constructor(props) {
super(props);
this._lastActive = false;
}
_handleEvent = ({ nativeEvent }) => {
const { state, oldState, pointerInside } = nativeEvent;
const active = pointerInside && state === State.ACTIVE;
if (active !== this._lastActive && this.props.onActiveStateChange) {
this.props.onActiveStateChange(active);
}
if (
oldState === State.ACTIVE &&
state !== State.CANCELLED &&
this._lastActive &&
this.props.onPress
) {
this.props.onPress(active);
}
this._lastActive = active;
};
// Normally, the parent would execute it's handler first,
// then forward the event to listeners. However, here our handler
// is virtually only forwarding events to listeners, so we reverse the order
// to keep the proper order of the callbacks (from "raw" ones to "processed").
_onHandlerStateChange = e => {
this.props.onHandlerStateChange && this.props.onHandlerStateChange(e);
this._handleEvent(e);
};
_onGestureEvent = e => {
this.props.onGestureEvent && this.props.onGestureEvent(e);
this._handleEvent(e);
};
render() {
const { rippleColor, ...rest } = this.props;
return (
<RawButton
rippleColor={processColor(rippleColor)}
{...rest}
onGestureEvent={this._onGestureEvent}
onHandlerStateChange={this._onHandlerStateChange}
/>
);
}
}
const AnimatedBaseButton = Animated.createAnimatedComponent(BaseButton);
const btnStyles = StyleSheet.create({
underlay: {
position: 'absolute',
left: 0,
right: 0,
bottom: 0,
top: 0,
},
});
export class RectButton extends React.Component {
static propTypes = BaseButton.propTypes;
static defaultProps = {
activeOpacity: 0.105,
underlayColor: 'black',
};
constructor(props) {
super(props);
this._opacity = new Animated.Value(0);
}
_onActiveStateChange = active => {
if (Platform.OS !== 'android') {
this._opacity.setValue(active ? this.props.activeOpacity : 0);
}
this.props.onActiveStateChange && this.props.onActiveStateChange(active);
};
render() {
const { children, style, ...rest } = this.props;
const resolvedStyle = StyleSheet.flatten(style ?? {});
return (
<BaseButton
{...rest}
style={resolvedStyle}
onActiveStateChange={this._onActiveStateChange}>
<Animated.View
style={[
btnStyles.underlay,
{
opacity: this._opacity,
backgroundColor: this.props.underlayColor,
borderRadius: resolvedStyle.borderRadius,
borderTopLeftRadius: resolvedStyle.borderTopLeftRadius,
borderTopRightRadius: resolvedStyle.borderTopRightRadius,
borderBottomLeftRadius: resolvedStyle.borderBottomLeftRadius,
borderBottomRightRadius: resolvedStyle.borderBottomRightRadius,
},
]}
/>
{children}
</BaseButton>
);
}
}
export class BorderlessButton extends React.Component {
static propTypes = {
...BaseButton.propTypes,
borderless: PropTypes.bool,
};
static defaultProps = {
activeOpacity: 0.3,
borderless: true,
};
constructor(props) {
super(props);
this._opacity = new Animated.Value(1);
}
_onActiveStateChange = active => {
if (Platform.OS !== 'android') {
this._opacity.setValue(active ? this.props.activeOpacity : 1);
}
this.props.onActiveStateChange && this.props.onActiveStateChange(active);
};
render() {
const { children, style, ...rest } = this.props;
return (
<AnimatedBaseButton
{...rest}
onActiveStateChange={this._onActiveStateChange}
style={[style, Platform.OS === 'ios' && { opacity: this._opacity }]}>
{children}
</AnimatedBaseButton>
);
}
}
export { default as PureNativeButton } from './GestureHandlerButton';