import React, { useEffect, useRef } from 'react'; import { View, Text, StyleSheet, Animated, TouchableOpacity, } from 'react-native'; import { Colors } from '../../constants/Colors'; interface ToastProps { message: string; type?: 'success' | 'error' | 'info'; duration?: number; onClose?: () => void; } export default function Toast({ message, type = 'info', duration = 3000, onClose, }: ToastProps) { const fadeAnim = useRef(new Animated.Value(0)).current; const slideAnim = useRef(new Animated.Value(-100)).current; useEffect(() => { // Fade in and slide down Animated.parallel([ Animated.timing(fadeAnim, { toValue: 1, duration: 300, useNativeDriver: true, }), Animated.spring(slideAnim, { toValue: 0, useNativeDriver: true, tension: 50, friction: 7, }), ]).start(); // Auto dismiss const timer = setTimeout(() => { dismiss(); }, duration); return () => clearTimeout(timer); }, []); const dismiss = () => { Animated.parallel([ Animated.timing(fadeAnim, { toValue: 0, duration: 200, useNativeDriver: true, }), Animated.timing(slideAnim, { toValue: -100, duration: 200, useNativeDriver: true, }), ]).start(() => { onClose?.(); }); }; const backgroundColor = type === 'success' ? Colors.success : type === 'error' ? Colors.error : Colors.info; return ( {message} ✕ ); } const styles = StyleSheet.create({ container: { position: 'absolute', top: 50, left: 0, right: 0, zIndex: 1000, paddingHorizontal: 16, }, toast: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', padding: 16, borderRadius: 8, shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84, elevation: 5, }, message: { flex: 1, color: Colors.text, fontSize: 14, fontWeight: '500', }, closeButton: { marginLeft: 12, padding: 4, }, closeText: { color: Colors.text, fontSize: 18, fontWeight: 'bold', }, });