-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathModal.tsx
More file actions
285 lines (264 loc) · 7.47 KB
/
Modal.tsx
File metadata and controls
285 lines (264 loc) · 7.47 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import * as React from 'react';
import {
Animated,
Easing,
StyleProp,
StyleSheet,
Platform,
Pressable,
View,
ViewStyle,
} from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import useLatestCallback from 'use-latest-callback';
import Surface from './Surface';
import { useInternalTheme } from '../core/theming';
import type { ThemeProp } from '../types';
import { addEventListener } from '../utils/addEventListener';
import { BackHandler } from '../utils/BackHandler/BackHandler';
import useAnimatedValue from '../utils/useAnimatedValue';
export type Props = {
/**
* Determines whether clicking outside the modal dismisses it.
*/
dismissable?: boolean;
/**
* Determines whether clicking Android hardware back button dismisses the dialog.
*/
dismissableBackButton?: boolean;
/**
* Callback that is called when the user dismisses the modal.
*/
onDismiss?: () => void;
/**
* Accessibility label for the overlay. This is read by the screen reader when the user taps outside the modal.
*/
overlayAccessibilityLabel?: string;
/**
* Determines Whether the modal is visible.
*/
visible: boolean;
/**
* Determines whether the modal uses animations.
*/
disableAnimations?: boolean;
/**
* Determines whether the modal closes on Escape on web.
*/
handleEscape?: boolean;
/**
* Content of the `Modal`.
*/
children: React.ReactNode;
/**
* Style for the content of the modal
*/
contentContainerStyle?: Animated.WithAnimatedValue<StyleProp<ViewStyle>>;
/**
* Style for the wrapper of the modal.
* Use this prop to change the default wrapper style or to override safe area insets with marginTop and marginBottom.
*/
style?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme?: ThemeProp;
/**
* testID to be used on tests.
*/
testID?: string;
};
const DEFAULT_DURATION = 220;
const AnimatedPressable = Animated.createAnimatedComponent(Pressable);
/**
* 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) component.
* Note that this modal is NOT accessible by default; if you need an accessible modal, please use the React Native Modal.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Modal, Portal, Text, Button, PaperProvider } from 'react-native-paper';
*
* const MyComponent = () => {
* const [visible, setVisible] = React.useState(false);
*
* const showModal = () => setVisible(true);
* const hideModal = () => setVisible(false);
* const containerStyle = {backgroundColor: 'white', padding: 20};
*
* return (
* <PaperProvider>
* <Portal>
* <Modal visible={visible} onDismiss={hideModal} contentContainerStyle={containerStyle}>
* <Text>Example Modal. Click outside this area to dismiss.</Text>
* </Modal>
* </Portal>
* <Button style={{marginTop: 30}} onPress={showModal}>
* Show
* </Button>
* </PaperProvider>
* );
* };
*
* export default MyComponent;
* ```
*/
function Modal({
dismissable = true,
dismissableBackButton = dismissable,
visible = false,
disableAnimations = false,
handleEscape = false,
overlayAccessibilityLabel = 'Close modal',
onDismiss = () => {},
children,
contentContainerStyle,
style,
theme: themeOverrides,
testID = 'modal',
}: Props) {
const theme = useInternalTheme(themeOverrides);
const onDismissCallback = useLatestCallback(onDismiss);
const { scale } = theme.animation;
const { top, bottom } = useSafeAreaInsets();
const opacity = useAnimatedValue(visible ? 1 : 0);
const [visibleInternal, setVisibleInternal] = React.useState(visible);
const showModalAnimation = React.useCallback(() => {
if (!disableAnimations) {
Animated.timing(opacity, {
toValue: 1,
duration: scale * DEFAULT_DURATION,
easing: Easing.out(Easing.cubic),
useNativeDriver: true,
}).start();
}
}, [opacity, scale, disableAnimations]);
const hideModalAnimation = React.useCallback(() => {
if (!disableAnimations) {
Animated.timing(opacity, {
toValue: 0,
duration: scale * DEFAULT_DURATION,
easing: Easing.out(Easing.cubic),
useNativeDriver: true,
}).start(({ finished }) => {
if (!finished) {
return;
}
setVisibleInternal(false);
});
} else {
setVisibleInternal(false);
}
}, [opacity, scale, disableAnimations]);
React.useEffect(() => {
if (visibleInternal === visible) {
return;
}
if (!visibleInternal && visible) {
setVisibleInternal(true);
return showModalAnimation();
}
if (visibleInternal && !visible) {
return hideModalAnimation();
}
}, [visible, showModalAnimation, hideModalAnimation, visibleInternal]);
React.useEffect(() => {
if (!visible) {
return undefined;
}
const onHardwareBackPress = () => {
if (dismissable || dismissableBackButton) {
onDismissCallback();
}
return true;
};
const subscription = addEventListener(
BackHandler,
'hardwareBackPress',
onHardwareBackPress
);
return () => subscription.remove();
}, [dismissable, dismissableBackButton, onDismissCallback, visible]);
React.useEffect(() => {
if (!visible || !handleEscape || Platform.OS !== 'web') {
return undefined;
}
const closeOnEscape = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
e.stopPropagation();
if (dismissable) {
onDismissCallback();
}
}
};
document.addEventListener('keyup', closeOnEscape, false);
return () => document.removeEventListener('keyup', closeOnEscape, false);
}, [dismissable, onDismissCallback, visible, handleEscape]);
if (!visibleInternal) {
return null;
}
return (
<Animated.View
pointerEvents={visible ? 'auto' : 'none'}
accessibilityViewIsModal
accessibilityLiveRegion="polite"
style={StyleSheet.absoluteFill}
onAccessibilityEscape={onDismissCallback}
testID={testID}
>
<AnimatedPressable
accessibilityLabel={overlayAccessibilityLabel}
accessibilityRole="button"
disabled={!dismissable}
onPress={dismissable ? onDismissCallback : undefined}
importantForAccessibility="no"
style={[
styles.backdrop,
{
backgroundColor: theme.colors?.backdrop,
},
...(!disableAnimations ? [{ opacity }] : []),
]}
testID={`${testID}-backdrop`}
/>
<View
style={[
styles.wrapper,
{ marginTop: top, marginBottom: bottom },
style,
]}
pointerEvents="box-none"
testID={`${testID}-wrapper`}
>
<Surface
testID={`${testID}-surface`}
theme={theme}
style={[
...(!disableAnimations ? [{ opacity }] : []),
styles.content,
contentContainerStyle,
]}
container
>
{children}
</Surface>
</View>
</Animated.View>
);
}
export default Modal;
const styles = StyleSheet.create({
backdrop: {
flex: 1,
},
wrapper: {
...StyleSheet.absoluteFillObject,
justifyContent: 'center',
},
// eslint-disable-next-line react-native/no-color-literals
content: {
backgroundColor: 'transparent',
justifyContent: 'center',
},
});