forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModal.tsx
231 lines (207 loc) · 5.3 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
227
228
229
230
231
import * as React from 'react';
import {
Animated,
BackHandler,
Easing,
StyleProp,
StyleSheet,
TouchableWithoutFeedback,
ViewStyle,
} from 'react-native';
import SafeAreaView from 'react-native-safe-area-view';
import Surface from './Surface';
import { withTheme } from '../core/theming';
import { Theme } from '../types';
type Props = {
/**
* Determines whether clicking outside the modal dismiss it.
*/
dismissable?: boolean;
/**
* Callback that is called when the user dismisses the modal.
*/
onDismiss?: () => void;
/**
* Determines Whether the modal is visible.
*/
visible: boolean;
/**
* Content of the `Modal`.
*/
children: React.ReactNode;
/**
* Style for the content of the modal
*/
contentContainerStyle?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme: Theme;
};
type State = {
opacity: Animated.Value;
rendered: boolean;
};
const DEFAULT_DURATION = 220;
/**
* The Modal component is a simple way to present content above an enclosing view.
* To render the `Modal` above other components, you'll need to wrap it with the [`Portal`](portal.html) component.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Modal, Portal, Text, Button, Provider } from 'react-native-paper';
*
* export default class MyComponent extends React.Component {
* state = {
* visible: false,
* };
*
* _showModal = () => this.setState({ visible: true });
* _hideModal = () => this.setState({ visible: false });
*
* render() {
* const { visible } = this.state;
* return (
* <Provider>
* <Portal>
* <Modal visible={visible} onDismiss={this._hideModal}>
* <Text>Example Modal</Text>
* </Modal>
* <Button
* style={{ marginTop: 30 }}
* onPress={this._showModal}
* >
* Show
* </Button>
* </Portal>
* </Provider>
* );
* }
* }
* ```
*/
class Modal extends React.Component<Props, State> {
static defaultProps = {
dismissable: true,
visible: false,
};
static getDerivedStateFromProps(nextProps: Props, prevState: State) {
if (nextProps.visible && !prevState.rendered) {
return {
rendered: true,
};
}
return null;
}
state = {
opacity: new Animated.Value(this.props.visible ? 1 : 0),
rendered: this.props.visible,
};
componentDidUpdate(prevProps: Props) {
if (prevProps.visible !== this.props.visible) {
if (this.props.visible) {
this.showModal();
} else {
this.hideModal();
}
}
}
private handleBack = () => {
if (this.props.dismissable) {
this.hideModal();
}
return true;
};
private showModal = () => {
BackHandler.removeEventListener('hardwareBackPress', this.handleBack);
BackHandler.addEventListener('hardwareBackPress', this.handleBack);
const { opacity } = this.state;
const { scale } = this.props.theme.animation;
Animated.timing(opacity, {
toValue: 1,
duration: scale * DEFAULT_DURATION,
easing: Easing.out(Easing.cubic),
useNativeDriver: true,
}).start();
};
private hideModal = () => {
BackHandler.removeEventListener('hardwareBackPress', this.handleBack);
const { opacity } = this.state;
const { scale } = this.props.theme.animation;
Animated.timing(opacity, {
toValue: 0,
duration: scale * DEFAULT_DURATION,
easing: Easing.out(Easing.cubic),
useNativeDriver: true,
}).start(({ finished }) => {
if (!finished) {
return;
}
if (this.props.visible && this.props.onDismiss) {
this.props.onDismiss();
}
if (this.props.visible) {
this.showModal();
} else {
this.setState({
rendered: false,
});
}
});
};
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBack);
}
render() {
const { rendered, opacity } = this.state;
if (!rendered) return null;
const { children, dismissable, theme, contentContainerStyle } = this.props;
const { colors } = theme;
return (
<Animated.View
pointerEvents={this.props.visible ? 'auto' : 'none'}
accessibilityViewIsModal
accessibilityLiveRegion="polite"
style={StyleSheet.absoluteFill}
>
<TouchableWithoutFeedback
disabled={!dismissable}
onPress={dismissable ? this.hideModal : undefined}
>
<Animated.View
style={[
styles.backdrop,
{ backgroundColor: colors.backdrop, opacity },
]}
/>
</TouchableWithoutFeedback>
<SafeAreaView style={styles.wrapper} pointerEvents="box-none">
<Surface
style={
[{ opacity }, styles.content, contentContainerStyle] as StyleProp<
ViewStyle
>
}
>
{children}
</Surface>
</SafeAreaView>
</Animated.View>
);
}
}
export default withTheme(Modal);
const styles = StyleSheet.create({
backdrop: {
flex: 1,
},
wrapper: {
...StyleSheet.absoluteFillObject,
justifyContent: 'center',
},
content: {
backgroundColor: 'transparent',
justifyContent: 'center',
},
});