-
Notifications
You must be signed in to change notification settings - Fork 111
/
Modal.tsx
226 lines (207 loc) · 5.41 KB
/
Modal.tsx
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import * as React from "react";
import { Component } from "react";
import {
Animated,
Easing,
Modal as ReactNativeModal,
ModalProps as ReactNativeModalProps,
Platform,
StyleProp,
StyleSheet,
TouchableWithoutFeedback,
ViewStyle,
} from "react-native";
const MODAL_ANIM_DURATION = 300;
const MODAL_BACKDROP_OPACITY = 0.3;
const CONTENT_ANIMATION_IN = Platform.select({
ios: {
opacity: {
inputRange: [0, 1],
outputRange: [0, 1],
},
scale: {
inputRange: [0, 0.5, 1],
outputRange: [1.2, 1.1, 1],
},
},
android: {
opacity: {
inputRange: [0, 0.5, 1],
outputRange: [0, 1, 1],
},
scale: {
inputRange: [0, 1],
outputRange: [0.3, 1],
},
},
default: {
opacity: {
inputRange: [0, 0.5, 1],
outputRange: [0, 1, 1],
},
scale: {
inputRange: [0, 1],
outputRange: [0.3, 1],
},
},
});
const CONTENT_ANIMATION_OUT = Platform.select({
default: {
opacity: {
inputRange: [0, 1],
outputRange: [0, 1],
},
},
});
export interface ModalProps extends ReactNativeModalProps {
onBackdropPress?: () => void;
onHide?: () => void;
visible?: boolean;
contentStyle?: StyleProp<ViewStyle>;
useNativeDriver?: boolean;
}
interface ModalState {
visible: boolean;
currentAnimation: "none" | "in" | "out";
}
export class Modal extends Component<ModalProps, ModalState> {
static defaultProps: Partial<ModalProps> = {
onBackdropPress: () => null,
onHide: () => null,
visible: false,
useNativeDriver: false,
};
state: ModalState = {
visible: Boolean(this.props.visible),
currentAnimation: "none",
};
animVal = new Animated.Value(0);
_isMounted = false;
componentDidMount() {
this._isMounted = true;
if (this.state.visible) {
this.show();
}
}
componentWillUnmount() {
this._isMounted = false;
}
componentDidUpdate(prevProps: ModalProps) {
if (this.props.visible && !prevProps.visible) {
this.show();
} else if (!this.props.visible && prevProps.visible) {
this.hide();
}
}
show = () => {
this.setState({ visible: true, currentAnimation: "in" }, () => {
Animated.timing(this.animVal, {
easing: Easing.inOut(Easing.quad),
useNativeDriver: Boolean(this.props.useNativeDriver),
duration: MODAL_ANIM_DURATION,
toValue: 1,
}).start(() => {
this.setState({ currentAnimation: "none" });
});
});
};
hide = () => {
this.setState({ currentAnimation: "out" }, () => {
Animated.timing(this.animVal, {
easing: Easing.inOut(Easing.quad),
useNativeDriver: Boolean(this.props.useNativeDriver),
duration: MODAL_ANIM_DURATION,
toValue: 0,
}).start(() => {
if (this._isMounted) {
this.setState({ currentAnimation: "none" });
this.setState({ visible: false }, this.props.onHide);
}
});
});
};
render() {
const {
children,
onBackdropPress,
contentStyle,
...otherProps
} = this.props;
const { currentAnimation, visible } = this.state;
const backdropAnimatedStyle = {
opacity: this.animVal.interpolate({
inputRange: [0, 1],
outputRange: [0, MODAL_BACKDROP_OPACITY],
}),
};
const contentAnimatedStyle =
currentAnimation === "in"
? {
opacity: this.animVal.interpolate({
inputRange: CONTENT_ANIMATION_IN.opacity.inputRange,
outputRange: CONTENT_ANIMATION_IN.opacity.outputRange,
extrapolate: "clamp",
}),
transform: [
{
scale: this.animVal.interpolate({
inputRange: CONTENT_ANIMATION_IN.scale.inputRange,
outputRange: CONTENT_ANIMATION_IN.scale.outputRange,
extrapolate: "clamp",
}),
},
],
}
: {
opacity: this.animVal.interpolate({
inputRange: CONTENT_ANIMATION_OUT.opacity.inputRange,
outputRange: CONTENT_ANIMATION_OUT.opacity.outputRange,
extrapolate: "clamp",
}),
};
return (
<ReactNativeModal
transparent
animationType="none"
{...otherProps}
visible={visible}
>
<TouchableWithoutFeedback onPress={onBackdropPress}>
<Animated.View style={[styles.backdrop, backdropAnimatedStyle]} />
</TouchableWithoutFeedback>
{visible && (
<Animated.View
style={[styles.content, contentAnimatedStyle, contentStyle]}
pointerEvents="box-none"
// Setting "needsOffscreenAlphaCompositing" solves a janky elevation
// animation on android. We should set it only while animation
// to avoid using more memory than needed.
// See: https://github.com/facebook/react-native/issues/23090
needsOffscreenAlphaCompositing={["in", "out"].includes(
currentAnimation
)}
>
{children}
</Animated.View>
)}
</ReactNativeModal>
);
}
}
const styles = StyleSheet.create({
backdrop: {
position: "absolute",
top: 0,
bottom: 0,
left: 0,
right: 0,
backgroundColor: "black",
opacity: 0,
},
content: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
});
export default Modal;