import * as React from 'react'; import { Animated, BackHandler, Easing, NativeEventSubscription, StyleProp, StyleSheet, TouchableWithoutFeedback, View, ViewStyle, } from 'react-native'; import { getBottomSpace, getStatusBarHeight, } from 'react-native-iphone-x-helper'; import { useTheme } from '../core/theming'; import { addEventListener } from '../utils/addEventListener'; import isNativeAnimationSupported from '../utils/isNativeAnimationSupported'; import useAnimatedValue from '../utils/useAnimatedValue'; import Surface from './Surface'; type Props = { /** * Determines whether clicking outside the modal dismiss it. */ dismissable?: 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; /** * Content of the `Modal`. */ children: React.ReactNode; /** * Style for the content of the modal */ contentContainerStyle?: StyleProp; /** * 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; }; const DEFAULT_DURATION = 220; const TOP_INSET = getStatusBarHeight(true); const BOTTOM_INSET = getBottomSpace(); /** * 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'; * * const MyComponent = () => { * const [visible, setVisible] = React.useState(false); * * const showModal = () => setVisible(true); * const hideModal = () => setVisible(false); * const containerStyle = {backgroundColor: 'white', padding: 20}; * * return ( * * * * Example Modal. Click outside this area to dismiss. * * * * * ); * }; * * export default MyComponent; * ``` */ export default function Modal({ dismissable = true, visible = false, overlayAccessibilityLabel = 'Close modal', onDismiss, children, contentContainerStyle, style, }: Props) { const visibleRef = React.useRef(visible); React.useEffect(() => { visibleRef.current = visible; }); const { colors, animation } = useTheme(); const opacity = useAnimatedValue(visible ? 1 : 0); const [rendered, setRendered] = React.useState(visible); if (visible && !rendered) { setRendered(true); } const handleBack = () => { if (dismissable) { hideModal(); } return true; }; const subscription = React.useRef( undefined ); const showModal = () => { subscription.current?.remove(); subscription.current = addEventListener( BackHandler, 'hardwareBackPress', handleBack ); const { scale } = animation; Animated.timing(opacity, { toValue: 1, duration: scale * DEFAULT_DURATION, easing: Easing.out(Easing.cubic), useNativeDriver: isNativeAnimationSupported(), }).start(); }; const removeListeners = () => { if (subscription.current?.remove) { subscription.current?.remove(); } else { BackHandler.removeEventListener('hardwareBackPress', handleBack); } }; const hideModal = () => { removeListeners(); const { scale } = animation; Animated.timing(opacity, { toValue: 0, duration: scale * DEFAULT_DURATION, easing: Easing.out(Easing.cubic), useNativeDriver: isNativeAnimationSupported(), }).start(({ finished }) => { if (!finished) { return; } if (visible && onDismiss) { onDismiss(); } if (visibleRef.current) { showModal(); } else { setRendered(false); } }); }; const prevVisible = React.useRef(null); React.useEffect(() => { if (prevVisible.current !== visible) { if (visible) { showModal(); } else { hideModal(); } } prevVisible.current = visible; }); React.useEffect(() => { return removeListeners; }, []); if (!rendered) return null; return ( } > {children} ); } const styles = StyleSheet.create({ backdrop: { flex: 1, }, wrapper: { ...StyleSheet.absoluteFillObject, justifyContent: 'center', }, content: { backgroundColor: 'transparent', justifyContent: 'center', }, });