Open njavilas2015 opened 3 years ago
popup.js
import React, { useState } from 'react'; import { View, Text, TouchableOpacity, StyleSheet, Image, Animated, Dimensions, Button } from 'react-native';
import { useTheme } from 'react-native-paper';
const { height, width } = Dimensions.get('screen');
const Popup = ({ title, type, icon, textBody, button, buttonText, callback, background, duration, autoClose }) => { const { colors } = useTheme();
const [positionView, setPositionView] = useState(new Animated.Value(height));
const [opacity, setOpacity] = useState(new Animated.Value(0));
const [positionPopup, setPositionPopup] = useState(new Animated.Value(height));
const [popupHeight, setPopupHeight] = useState(0);
const styles = StyleSheet.create({
Container: {
position: 'absolute',
zIndex: 99999,
width: width,
height: height,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
alignItems: 'center',
top: -70,
left: 0,
},
Message: {
maxWidth: 300,
width: 230,
minHeight: 300,
backgroundColor: '#fff',
borderRadius: 30,
alignItems: 'center',
overflow: 'hidden',
position: 'absolute',
},
Content: {
padding: 20,
alignItems: 'center',
},
Header: {
height: 230,
width: 230,
backgroundColor: '#FBFBFB',
borderRadius: 100,
marginTop: -120,
},
Image: {
width: 150,
height: 80,
position: 'absolute',
top: 20,
},
Title: {
fontWeight: 'bold',
fontSize: 18,
color: '#333',
},
Desc: {
textAlign: 'center',
color: '#666',
marginTop: 10,
},
Button: {
borderRadius: 50,
height: 40,
width: 130,
justifyContent: 'center',
alignItems: 'center',
marginTop: 30,
},
TextButton: {
color: '#fff',
fontWeight: 'bold',
},
Success: {
backgroundColor: '#AAF577',
shadowColor: '#AAF577',
shadowOffset: {
width: 0,
height: 5,
},
shadowOpacity: 0.36,
shadowRadius: 6.68,
elevation: 11,
},
Danger: {
backgroundColor: '#F29091',
shadowColor: '#F29091',
shadowOffset: {
width: 0,
height: 5,
},
shadowOpacity: 0.36,
shadowRadius: 6.68,
elevation: 11,
},
Warning: {
backgroundColor: '#fbd10d',
shadowColor: '#fbd10d',
shadowOffset: {
width: 0,
height: 5,
},
shadowOpacity: 0.36,
shadowRadius: 6.68,
elevation: 11,
},
});
const start = () => {
Animated.sequence([
Animated.timing(positionView, { toValue: 0, duration: 100, useNativeDriver: false }),
Animated.timing(opacity, { toValue: 1, duration: 300, useNativeDriver: false }),
Animated.spring(positionPopup, {
toValue: (height / 2) - (popupHeight / 2),
bounciness: 15, useNativeDriver: true,
}),
]).start();
if (autoClose && duration !== 0) { setTimeout(() => hidePopup(), duration); }
};
const hidePopup = () => Animated.sequence([
Animated.timing(positionPopup, { toValue: height, duration: 250, useNativeDriver: true }),
Animated.timing(opacity, { toValue: 0, duration: 300, useNativeDriver: false }),
Animated.timing(positionView, { toValue: height, duration: 100, useNativeDriver: false }),
]).start();
return (
<>
<Animated.View
style={[styles.Container, {
backgroundColor: background || 'transparent',
opacity: opacity,
transform: [{ translateY: positionView }],
}]}>
<Animated.View
onLayout={event => setPopupHeight(event.nativeEvent.layout.height)}
style={[styles.Message, { transform: [{ translateY: positionPopup }] }]}
>
<View style={styles.Header} />
<Image source={icon} resizeMode="contain" style={styles.Image} />
<View style={styles.Content}>
<Text style={styles.Title}>{title}</Text>
<Text style={styles.Desc}>{textBody}</Text>
{
button ?
<TouchableOpacity style={[styles.Button, styles[type]]} onPress={() => callback()}>
<Text style={styles.TextButton}>{buttonText}</Text>
</TouchableOpacity> : null
}
</View>
</Animated.View>
</Animated.View>
<Button title={'open animated'} onPress={() => start()} />
</>
);
};
export default Popup;
hi sorry for my short contribution, i updated the toast to hooks good night
`import React, { useState, useRef } from 'react'; import { View, Animated, Text, StyleSheet, Dimensions, Image, Button } from 'react-native';
const { width, height } = Dimensions.get('screen');
const Toast = ({ title, text, icon, color, duration }) => {
};
export default Toast; `