|
| 1 | +import { Text } from '@/src/common/components'; |
| 2 | +import Icon, { IconName } from '@/src/common/components/Icon'; |
| 3 | +import { |
| 4 | + Atoms, |
| 5 | + BorderRadius, |
| 6 | + PaletteColorToken, |
| 7 | + useTheme, |
| 8 | +} from '@/src/common/theme'; |
| 9 | +import * as ToastPrimitive from '@rn-primitives/toast'; |
| 10 | +import { useCallback, useEffect } from 'react'; |
| 11 | +import { Pressable, View } from 'react-native'; |
| 12 | +import Animated, { |
| 13 | + LinearTransition, |
| 14 | + useAnimatedStyle, |
| 15 | + useSharedValue, |
| 16 | + withTiming, |
| 17 | +} from 'react-native-reanimated'; |
| 18 | +import { runOnJS } from 'react-native-worklets'; |
| 19 | +import { ToastData, ToastVariant, useToastStore } from './useToastStore'; |
| 20 | + |
| 21 | +const ENTER_MS = 220; |
| 22 | +const EXIT_MS = 160; |
| 23 | + |
| 24 | +const VARIANTS: Record< |
| 25 | + ToastVariant, |
| 26 | + { icon: IconName; color: PaletteColorToken } |
| 27 | +> = { |
| 28 | + info: { icon: 'notification', color: 'primary_500' }, |
| 29 | + success: { icon: 'checkmarkCircle', color: 'positive_500' }, |
| 30 | + error: { icon: 'ban', color: 'negative_500' }, |
| 31 | + warning: { icon: 'flag', color: 'warning_500' }, |
| 32 | +}; |
| 33 | + |
| 34 | +const shadow = { |
| 35 | + shadowColor: '#000', |
| 36 | + shadowOpacity: 0.12, |
| 37 | + shadowRadius: 12, |
| 38 | + shadowOffset: { width: 0, height: 4 }, |
| 39 | + elevation: 4, |
| 40 | +}; |
| 41 | + |
| 42 | +export function Toast({ toast }: { toast: ToastData }) { |
| 43 | + const { theme } = useTheme(); |
| 44 | + const dismiss = useToastStore((s) => s.dismiss); |
| 45 | + const progress = useSharedValue(0); |
| 46 | + const variant = VARIANTS[toast.variant]; |
| 47 | + |
| 48 | + const close = useCallback(() => { |
| 49 | + progress.value = withTiming(0, { duration: EXIT_MS }, (finished) => { |
| 50 | + if (finished) runOnJS(dismiss)(toast.id); |
| 51 | + }); |
| 52 | + }, [progress, dismiss, toast.id]); |
| 53 | + |
| 54 | + useEffect(() => { |
| 55 | + progress.value = withTiming(1, { duration: ENTER_MS }); |
| 56 | + if (toast.duration === null) return; |
| 57 | + const timer = setTimeout(close, toast.duration); |
| 58 | + return () => clearTimeout(timer); |
| 59 | + }, [progress, close, toast.duration]); |
| 60 | + |
| 61 | + const animatedStyle = useAnimatedStyle(() => ({ |
| 62 | + opacity: progress.value, |
| 63 | + // Drop down from above on enter, lift back up on exit. |
| 64 | + transform: [{ translateY: (progress.value - 1) * 16 }], |
| 65 | + })); |
| 66 | + |
| 67 | + return ( |
| 68 | + <Animated.View layout={LinearTransition} style={animatedStyle}> |
| 69 | + <ToastPrimitive.Root |
| 70 | + open |
| 71 | + onOpenChange={(next) => { |
| 72 | + if (!next) close(); |
| 73 | + }} |
| 74 | + style={[ |
| 75 | + Atoms.flex_row, |
| 76 | + Atoms.items_center, |
| 77 | + Atoms.gap_sm, |
| 78 | + Atoms.p_lg, |
| 79 | + { |
| 80 | + borderRadius: BorderRadius.full, |
| 81 | + backgroundColor: theme.palette.neutral_0, |
| 82 | + borderWidth: 1, |
| 83 | + borderColor: theme.palette.neutral_50, |
| 84 | + ...shadow, |
| 85 | + }, |
| 86 | + ]} |
| 87 | + > |
| 88 | + <Icon name={variant.icon} color={variant.color} size={20} /> |
| 89 | + <View style={Atoms.flex_1}> |
| 90 | + <ToastPrimitive.Title asChild> |
| 91 | + <Text variant="body" fontWeight="semibold" style={theme.atoms.text}> |
| 92 | + {toast.title} |
| 93 | + </Text> |
| 94 | + </ToastPrimitive.Title> |
| 95 | + {toast.description ? ( |
| 96 | + <ToastPrimitive.Description asChild> |
| 97 | + <Text variant="small" style={theme.atoms.text_neutral_medium}> |
| 98 | + {toast.description} |
| 99 | + </Text> |
| 100 | + </ToastPrimitive.Description> |
| 101 | + ) : null} |
| 102 | + </View> |
| 103 | + <ToastPrimitive.Close asChild> |
| 104 | + <Pressable hitSlop={8}> |
| 105 | + <Icon name="close" color="neutral_500" size={18} /> |
| 106 | + </Pressable> |
| 107 | + </ToastPrimitive.Close> |
| 108 | + </ToastPrimitive.Root> |
| 109 | + </Animated.View> |
| 110 | + ); |
| 111 | +} |
0 commit comments