All files / components/dialogs Toast.tsx

100% Statements 18/18
83.33% Branches 10/12
100% Functions 7/7
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                                                                          5x 5x 5x     5x 5x   5x   5x                             5x 1x     5x     5x 2x                         2x       5x                 5x                                                                                                                 1x 1x                                                          
/**
 * Toast - 轻量级通知组件
 * 用于显示成功、错误、警告等非阻塞式通知
 */
 
import React, { useEffect } from 'react';
import { Animated, TouchableOpacity } from 'react-native';
import { Text, XStack, YStack } from 'tamagui';
import { IconSymbol, type SymbolName } from '@/src/components/ui/IconSymbol';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import {
  DIALOG_COLORS,
  BORDER_RADIUS,
  SPACING,
  FONT_SIZE,
  SHADOWS,
  SF_SYMBOLS,
  TYPE_COLORS,
  TOAST_DURATION,
} from './constants';
import type { ToastConfig } from './types';
 
interface ToastProps extends ToastConfig {
  onDismiss: () => void;
  position?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left';
}
 
export function Toast({
  type,
  message,
  description,
  duration = TOAST_DURATION.medium,
  icon,
  action,
  onDismiss,
  position = 'bottom-right',
}: ToastProps) {
  const insets = useSafeAreaInsets();
  const colors = TYPE_COLORS[type];
  const defaultSfSymbol = SF_SYMBOLS[type];
 
  // 动画值
  const [translateY] = React.useState(new Animated.Value(100));
  const [opacity] = React.useState(new Animated.Value(0));
 
  useEffect(() => {
    // 进入动画
    Animated.parallel([
      Animated.spring(translateY, {
        toValue: 0,
        useNativeDriver: true,
        damping: 20,
        stiffness: 300,
      }),
      Animated.timing(opacity, {
        toValue: 1,
        duration: 200,
        useNativeDriver: true,
      }),
    ]).start();
 
    // 自动关闭
    const timer = setTimeout(() => {
      handleDismiss();
    }, duration);
 
    return () => clearTimeout(timer);
  }, []);
 
  const handleDismiss = () => {
    Animated.parallel([
      Animated.spring(translateY, {
        toValue: 100,
        useNativeDriver: true,
        damping: 20,
        stiffness: 300,
      }),
      Animated.timing(opacity, {
        toValue: 0,
        duration: 150,
        useNativeDriver: true,
      }),
    ]).start(() => {
      onDismiss();
    });
  };
 
  const positionStyles = {
    position: 'absolute' as const,
    ...(position.includes('bottom')
      ? { bottom: Math.max(insets.bottom, SPACING.md) + SPACING.md }
      : { top: Math.max(insets.top, SPACING.md) + SPACING.md }),
    ...(position.includes('right') ? { right: SPACING.md } : { left: SPACING.md }),
    zIndex: 9999,
  };
 
  return (
    <Animated.View
      style={[
        positionStyles,
        {
          transform: [{ translateY }],
          opacity,
        },
      ]}
    >
      <XStack
        backgroundColor={DIALOG_COLORS.background as any}
        borderRadius={BORDER_RADIUS.medium}
        padding={SPACING.md}
        gap={SPACING.md}
        alignItems="flex-start"
        minWidth={320}
        maxWidth={400}
        borderLeftWidth={4}
        borderLeftColor={colors.border as any}
        {...SHADOWS.lg}
      >
        {/* 图标 */}
        <YStack
          width={40}
          height={40}
          borderRadius={BORDER_RADIUS.medium}
          backgroundColor={colors.bg as any}
          alignItems="center"
          justifyContent="center"
          flexShrink={0}
        >
          {icon ? (
            <Text fontSize={20}>{icon}</Text>
          ) : (
            <IconSymbol name={defaultSfSymbol} size={20} color={colors.icon as any} />
          )}
        </YStack>
 
        {/* 内容 */}
        <YStack flex={1} gap={SPACING.xs}>
          <Text
            fontSize={FONT_SIZE.base}
            fontWeight="700"
            color={DIALOG_COLORS.text as any}
            lineHeight={20}
          >
            {message}
          </Text>
          {description && (
            <Text fontSize={FONT_SIZE.sm} color={DIALOG_COLORS.textLight as any} lineHeight={18}>
              {description}
            </Text>
          )}
          {action && (
            <TouchableOpacity
              onPress={() => {
                action.onPress();
                handleDismiss();
              }}
              activeOpacity={0.7}
              style={{ marginTop: SPACING.xs }}
            >
              <Text fontSize={FONT_SIZE.sm} fontWeight="700" color={colors.icon as any}>
                {action.label}
              </Text>
            </TouchableOpacity>
          )}
        </YStack>
 
        {/* 关闭按钮 */}
        <TouchableOpacity onPress={handleDismiss} activeOpacity={0.7}>
          <YStack
            width={24}
            height={24}
            borderRadius={BORDER_RADIUS.full}
            backgroundColor={DIALOG_COLORS.neutral[100] as any}
            alignItems="center"
            justifyContent="center"
          >
            <IconSymbol name="xmark" size={12} color={DIALOG_COLORS.neutral[600] as any} />
          </YStack>
        </TouchableOpacity>
      </XStack>
    </Animated.View>
  );
}