forked from TrustUp-app/TrustUp-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathToast.tsx
More file actions
74 lines (65 loc) · 2.03 KB
/
Copy pathToast.tsx
File metadata and controls
74 lines (65 loc) · 2.03 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
import React, { useEffect, useRef } from 'react';
import { Text, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { Ionicons } from '@expo/vector-icons';
const colors = require('../../theme/colors.json');
export type ToastType = 'success' | 'error';
interface ToastProps {
visible: boolean;
message: string;
type?: ToastType;
/** Auto-dismiss delay in ms. */
duration?: number;
onHide: () => void;
}
/**
* Lightweight self-dismissing toast (no extra dependencies). Anchored to the
* bottom above the safe-area inset and pointer-transparent so it never blocks
* the UI underneath. Dismissal is timer-driven for reliability across targets.
*/
export const Toast = ({
visible,
message,
type = 'success',
duration = 2500,
onHide,
}: ToastProps) => {
const insets = useSafeAreaInsets();
// Keep the latest onHide without making it an effect dependency, otherwise a
// new onHide identity on each parent render would restart the dismiss timer.
const onHideRef = useRef(onHide);
useEffect(() => {
onHideRef.current = onHide;
}, [onHide]);
useEffect(() => {
if (!visible) return;
const timer = setTimeout(() => onHideRef.current(), duration);
return () => clearTimeout(timer);
}, [visible, duration]);
if (!visible) return null;
const isSuccess = type === 'success';
return (
<View
pointerEvents="none"
accessibilityRole="alert"
accessibilityLabel={message}
style={{
position: 'absolute',
left: 16,
right: 16,
bottom: insets.bottom + 24,
zIndex: 100,
}}>
<View
className="flex-row items-center gap-2 rounded-xl px-4 py-3 shadow-lg"
style={{ backgroundColor: isSuccess ? colors.successDeep : colors.error }}>
<Ionicons
name={isSuccess ? 'checkmark-circle' : 'alert-circle'}
size={18}
color={colors.white}
/>
<Text className="flex-1 text-sm font-medium text-white">{message}</Text>
</View>
</View>
);
};