import React, { useRef, useEffect, useState } from 'react'; import { View, Pressable, TouchableWithoutFeedback, Animated, ViewStyle } from 'react-native'; import Node from '../node'; import Mask from '../mask'; import Icon from '../icon'; import SafeFilling from '../safe-filling'; import styles from './style'; const prefixCls = 'popup'; export interface PopupProps { open?: boolean; title?: React.ReactNode; closeable?: boolean; transparent?: boolean; position?: string; style?: ViewStyle; safeArea?: boolean; children?: React.ReactNode; onClose?: () => void; } const Popup: React.FC = props => { const { open = false, title, closeable, position, transparent, style, safeArea, children, onClose, } = props; const distance = useRef(new Animated.Value(0)).current; const size = useRef(0); const [showMask, setShowMask] = useState(false); useEffect(() => { if (open) { setShowMask(true); Animated.timing(distance, { toValue: -size.current, duration: 300, useNativeDriver: true, }).start(() => {}); } else { Animated.timing(distance, { toValue: 0, duration: 300, useNativeDriver: true, }).start(() => { setShowMask(false); }); } }, [open]); const handleClose = () => { onClose?.(); }; const handLayout = (e: any) => { let isFirst = false; if (size.current) { isFirst = false; } else { isFirst = true; } if (position === 'top' || position === 'bottom') { size.current = e.nativeEvent.layout.height; } if (position === 'left' || position === 'right') { size.current = e.nativeEvent.layout.width; } if (isFirst) { Animated.timing(distance, { toValue: -size.current, duration: 200, useNativeDriver: true, }).start(); } }; let transformStyle = {}; if (position === 'top') { transformStyle = { transform: [{ translateY: distance }] }; } if (position === 'bottom') { transformStyle = { transform: [{ translateY: distance }] }; } if (position === 'left') { transformStyle = { transform: [{ translateX: distance }] }; } if (position === 'right') { transformStyle = { transform: [{ translateX: distance }] }; } return ( {title ? {title} : null} {closeable ? ( ) : null} {children} {safeArea ? : null} ); }; export default Popup;